feat: inbox screen
This commit is contained in:
parent
56645910e4
commit
8e678a37d4
|
@ -4,6 +4,7 @@ import 'package:flutter/cupertino.dart';
|
|||
import 'providers/providers.dart';
|
||||
import 'providers/settings.dart';
|
||||
import 'screens/screens.dart';
|
||||
import 'screens/inbox.dart';
|
||||
|
||||
class Home extends StatefulWidget {
|
||||
@override
|
||||
|
@ -51,6 +52,10 @@ class _HomeState extends State<Home> {
|
|||
|
||||
List<BottomNavigationBarItem> _buildNavigationItems() {
|
||||
return [
|
||||
BottomNavigationBarItem(
|
||||
icon: Icon(Icons.inbox),
|
||||
title: Text('Inbox'),
|
||||
),
|
||||
BottomNavigationBarItem(
|
||||
icon: Icon(Icons.rss_feed),
|
||||
title: Text('News'),
|
||||
|
@ -73,12 +78,14 @@ class _HomeState extends State<Home> {
|
|||
_buildScreen(int index) {
|
||||
switch (index) {
|
||||
case 0:
|
||||
return NewsScreen();
|
||||
return InboxScreen();
|
||||
case 1:
|
||||
return SearchScreen();
|
||||
return NewsScreen();
|
||||
case 2:
|
||||
return NotificationScreen();
|
||||
return SearchScreen();
|
||||
case 3:
|
||||
return NotificationScreen();
|
||||
case 4:
|
||||
return ProfileScreen();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ class _SettingsProviderState extends State<SettingsProvider> {
|
|||
if (Platform.isIOS) {
|
||||
layout = LayoutMap.cupertino;
|
||||
}
|
||||
// layout = LayoutMap.material;
|
||||
layout = LayoutMap.material;
|
||||
}
|
||||
|
||||
@override
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import '../widgets/list_scaffold.dart';
|
||||
import '../widgets/notification_item.dart';
|
||||
import '../utils/utils.dart';
|
||||
|
||||
Future<List<NotificationPayload>> fetchNotifications(int page) async {
|
||||
List items =
|
||||
await getWithCredentials('/notifications?page=$page&per_page=20');
|
||||
return items.map((item) => NotificationPayload.fromJson(item)).toList();
|
||||
}
|
||||
|
||||
class InboxScreen extends StatefulWidget {
|
||||
@override
|
||||
_InboxScreenState createState() => _InboxScreenState();
|
||||
}
|
||||
|
||||
class _InboxScreenState extends State<InboxScreen> {
|
||||
// int active = 0;
|
||||
int page = 0;
|
||||
var payload;
|
||||
List<NotificationPayload> _items = [];
|
||||
|
||||
final titleMap = {
|
||||
0: 'Unread',
|
||||
1: 'Participating',
|
||||
2: 'All',
|
||||
};
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ListScaffold(
|
||||
title: Text('Inbox'),
|
||||
onRefresh: () async {
|
||||
page = 1;
|
||||
var items = await fetchNotifications(page);
|
||||
setState(() {
|
||||
_items = items;
|
||||
});
|
||||
},
|
||||
onLoadMore: () async {
|
||||
page = page + 1;
|
||||
var items = await fetchNotifications(page);
|
||||
setState(() {
|
||||
_items.addAll(items);
|
||||
});
|
||||
},
|
||||
itemCount: _items.length,
|
||||
itemBuilder: (context, index) {
|
||||
return NotificationItem(payload: _items[index]);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
|
@ -51,7 +51,7 @@ class _IssueScreenState extends State<IssueScreen> {
|
|||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ListScaffold(
|
||||
title: _fullName + ' #' + widget.id.toString(),
|
||||
title: Text(_fullName + ' #' + widget.id.toString()),
|
||||
header: payload == null ? null : _buildHeader(),
|
||||
itemCount: _items.length,
|
||||
itemBuilder: (context, index) => TimelineItem(_items[index], payload),
|
||||
|
|
|
@ -16,7 +16,7 @@ class NewsScreenState extends State<NewsScreen> {
|
|||
@override
|
||||
Widget build(context) {
|
||||
return ListScaffold(
|
||||
title: 'News',
|
||||
title: Text('News'),
|
||||
itemCount: _events.length,
|
||||
itemBuilder: (context, index) => EventItem(_events[index]),
|
||||
onRefresh: () async {
|
||||
|
|
|
@ -45,7 +45,7 @@ class NotificationScreenState extends State<NotificationScreen> {
|
|||
style: TextStyle(color: Colors.black, fontSize: 15),
|
||||
),
|
||||
items: group.items,
|
||||
itemBuilder: (item) => NotificationItem(item: item),
|
||||
itemBuilder: (item) => NotificationItem(payload: item),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -138,7 +138,7 @@ class _PullRequestScreenState extends State<PullRequestScreen> {
|
|||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ListScaffold(
|
||||
title: _fullName + ' #' + widget.id.toString(),
|
||||
title: Text(_fullName + ' #' + widget.id.toString()),
|
||||
header: payload == null ? null : _buildHeader(),
|
||||
itemCount: _items.length,
|
||||
itemBuilder: (context, index) => TimelineItem(_items[index], payload),
|
||||
|
|
|
@ -18,7 +18,9 @@ class TimeAgo {
|
|||
double diff =
|
||||
(DateTime.now().millisecondsSinceEpoch - time.millisecondsSinceEpoch) /
|
||||
1000;
|
||||
if (diff < 3600) {
|
||||
if (diff < 0) {
|
||||
return 'in the future';
|
||||
} else if (diff < 3600) {
|
||||
return _pluralize(diff / 60, 'minute');
|
||||
} else if (diff < 86400) {
|
||||
return _pluralize(diff / 3600, 'hour');
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/gestures.dart';
|
||||
|
||||
import '../providers/settings.dart';
|
||||
import '../screens/screens.dart';
|
||||
export 'github.dart';
|
||||
export 'octicons.dart';
|
||||
|
@ -14,6 +14,49 @@ Color convertColor(String cssHex) {
|
|||
return Color(int.parse('ff' + cssHex, radix: 16));
|
||||
}
|
||||
|
||||
class Option<T> {
|
||||
final T value;
|
||||
final Widget widget;
|
||||
Option({this.value, this.widget});
|
||||
}
|
||||
|
||||
Future<T> showOptions<T>(BuildContext context, List<Option<T>> options) {
|
||||
var builder = (BuildContext context) {
|
||||
return CupertinoAlertDialog(
|
||||
actions: options.map((option) {
|
||||
return CupertinoDialogAction(
|
||||
child: option.widget,
|
||||
onPressed: () {
|
||||
Navigator.pop(context, option.value);
|
||||
},
|
||||
);
|
||||
}).toList(),
|
||||
);
|
||||
};
|
||||
|
||||
switch (SettingsProvider.of(context).layout) {
|
||||
case LayoutMap.cupertino:
|
||||
return showCupertinoDialog<T>(
|
||||
context: context,
|
||||
builder: builder,
|
||||
);
|
||||
default:
|
||||
return showDialog(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
return Dialog(
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
PopupMenuItem(child: Text('a')),
|
||||
PopupMenuItem(child: Text('b')),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
TextSpan createLinkSpan(BuildContext context, String text, Function handle) {
|
||||
return TextSpan(
|
||||
text: text,
|
||||
|
|
|
@ -142,8 +142,6 @@ class EventItem extends StatelessWidget {
|
|||
build(BuildContext context) {
|
||||
return Container(
|
||||
padding: EdgeInsets.all(8),
|
||||
decoration: BoxDecoration(
|
||||
border: Border(bottom: BorderSide(color: Colors.black12))),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
|
|
|
@ -7,7 +7,7 @@ import 'loading.dart';
|
|||
typedef RefreshCallback = Future<void> Function();
|
||||
|
||||
class ListScaffold extends StatefulWidget {
|
||||
final String title;
|
||||
final Widget title;
|
||||
final Widget header;
|
||||
final int itemCount;
|
||||
final IndexedWidgetBuilder itemBuilder;
|
||||
|
@ -77,11 +77,19 @@ class _ListScaffoldState extends State<ListScaffold> {
|
|||
}
|
||||
|
||||
Widget _buildItem(BuildContext context, int index) {
|
||||
if (index == widget.itemCount) {
|
||||
if (index == 2 * widget.itemCount) {
|
||||
return Loading(more: true);
|
||||
}
|
||||
|
||||
return widget.itemBuilder(context, index);
|
||||
if (index % 2 == 1) {
|
||||
return Container(
|
||||
decoration: BoxDecoration(
|
||||
border: Border(bottom: BorderSide(color: Colors.black12)),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return widget.itemBuilder(context, index ~/ 2);
|
||||
}
|
||||
|
||||
Widget _buildSliver(BuildContext context) {
|
||||
|
@ -91,7 +99,7 @@ class _ListScaffoldState extends State<ListScaffold> {
|
|||
return SliverList(
|
||||
delegate: SliverChildBuilderDelegate(
|
||||
_buildItem,
|
||||
childCount: widget.itemCount + 1,
|
||||
childCount: 2 * widget.itemCount + 1,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
@ -103,7 +111,7 @@ class _ListScaffoldState extends State<ListScaffold> {
|
|||
} else {
|
||||
return ListView.builder(
|
||||
controller: _controller,
|
||||
itemCount: widget.itemCount + 1,
|
||||
itemCount: 2 * widget.itemCount + 1,
|
||||
itemBuilder: _buildItem,
|
||||
);
|
||||
}
|
||||
|
@ -122,7 +130,7 @@ class _ListScaffoldState extends State<ListScaffold> {
|
|||
slivers.add(_buildSliver(context));
|
||||
|
||||
return CupertinoPageScaffold(
|
||||
navigationBar: CupertinoNavigationBar(middle: Text(widget.title)),
|
||||
navigationBar: CupertinoNavigationBar(middle: widget.title),
|
||||
child: SafeArea(
|
||||
child: CustomScrollView(
|
||||
controller: _controller,
|
||||
|
@ -132,7 +140,7 @@ class _ListScaffoldState extends State<ListScaffold> {
|
|||
);
|
||||
default:
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: Text(widget.title)),
|
||||
appBar: AppBar(title: widget.title),
|
||||
body: RefreshIndicator(
|
||||
onRefresh: widget.onRefresh,
|
||||
child: _buildBody(context),
|
||||
|
|
|
@ -2,30 +2,56 @@ import 'dart:core';
|
|||
import 'package:flutter/material.dart' hide Notification;
|
||||
import 'package:flutter/cupertino.dart' hide Notification;
|
||||
import '../utils/utils.dart';
|
||||
import '../screens/issue.dart';
|
||||
import '../screens/pull_request.dart';
|
||||
import 'link.dart';
|
||||
|
||||
class NotificationPayload {
|
||||
String type;
|
||||
String owner;
|
||||
String name;
|
||||
int number;
|
||||
String title;
|
||||
String updateAt;
|
||||
bool unread;
|
||||
|
||||
NotificationPayload.fromJson(input) {
|
||||
type = input['subject']['type'];
|
||||
name = input['repository']['name'];
|
||||
owner = input['repository']['owner']['login'];
|
||||
|
||||
String url = input['subject']['url'];
|
||||
String numberStr = url.split('/').lastWhere((_) => true);
|
||||
number = int.parse(numberStr);
|
||||
|
||||
title = input['subject']['title'];
|
||||
updateAt = TimeAgo.formatFromString(input['updated_at']);
|
||||
unread = input['unread'];
|
||||
}
|
||||
}
|
||||
|
||||
class NotificationItem extends StatelessWidget {
|
||||
const NotificationItem({
|
||||
Key key,
|
||||
@required this.item,
|
||||
@required this.payload,
|
||||
}) : super(key: key);
|
||||
|
||||
final Notification item;
|
||||
final NotificationPayload payload;
|
||||
|
||||
Widget _buildRoute(Notification item) {
|
||||
String type = item.subject.type;
|
||||
switch (type) {
|
||||
Widget _buildRoute() {
|
||||
switch (payload.type) {
|
||||
case 'Issue':
|
||||
return IssueScreen(payload.number, payload.owner, payload.name);
|
||||
case 'PullRequest':
|
||||
// return IssueScreen(item.repository.);
|
||||
return PullRequestScreen(payload.number, payload.owner, payload.name);
|
||||
default:
|
||||
// throw new Exception('Unhandled notification type: $type');
|
||||
return Text('test');
|
||||
}
|
||||
}
|
||||
|
||||
IconData _buildIconData(String type) {
|
||||
switch (type) {
|
||||
IconData _buildIconData() {
|
||||
switch (payload.type) {
|
||||
case 'Issue':
|
||||
return Octicons.issue_opened;
|
||||
// color: Color.fromRGBO(0x28, 0xa7, 0x45, 1),
|
||||
|
@ -42,39 +68,49 @@ class NotificationItem extends StatelessWidget {
|
|||
return Link(
|
||||
onTap: () {
|
||||
Navigator.of(context).push(
|
||||
CupertinoPageRoute(builder: (context) => _buildRoute(item)),
|
||||
CupertinoPageRoute(builder: (context) => _buildRoute()),
|
||||
);
|
||||
},
|
||||
child: Row(
|
||||
children: <Widget>[
|
||||
Container(
|
||||
padding: EdgeInsets.symmetric(horizontal: 8),
|
||||
child: Icon(
|
||||
_buildIconData(item.subject.type),
|
||||
color: Colors.black45,
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: Row(
|
||||
child: Container(
|
||||
padding: EdgeInsets.all(8),
|
||||
color: payload.unread ? Colors.white : Colors.black12,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
Container(
|
||||
padding: EdgeInsets.only(right: 8, top: 20),
|
||||
child: Icon(_buildIconData(), color: Colors.black45),
|
||||
),
|
||||
Expanded(
|
||||
child: Container(
|
||||
padding: EdgeInsets.symmetric(vertical: 8),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
Text(
|
||||
item.subject.title,
|
||||
payload.owner +
|
||||
'/' +
|
||||
payload.name +
|
||||
' #' +
|
||||
payload.number.toString(),
|
||||
style: TextStyle(fontSize: 13, color: Colors.black54),
|
||||
),
|
||||
Padding(padding: EdgeInsets.only(top: 4)),
|
||||
Text(
|
||||
payload.title,
|
||||
style: TextStyle(fontSize: 15),
|
||||
maxLines: 3,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
Padding(padding: EdgeInsets.only(top: 8)),
|
||||
Padding(padding: EdgeInsets.only(top: 6)),
|
||||
Text(
|
||||
TimeAgo.format(item.updatedAt),
|
||||
payload.updateAt,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.black87,
|
||||
// fontWeight: FontWeight.w300,
|
||||
color: Colors.black54,
|
||||
),
|
||||
)
|
||||
],
|
||||
|
@ -87,8 +123,8 @@ class NotificationItem extends StatelessWidget {
|
|||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue