feat: inbox screen

This commit is contained in:
Rongjian Zhang 2019-02-05 20:57:05 +08:00
parent 56645910e4
commit 8e678a37d4
12 changed files with 194 additions and 46 deletions

View File

@ -4,6 +4,7 @@ import 'package:flutter/cupertino.dart';
import 'providers/providers.dart'; import 'providers/providers.dart';
import 'providers/settings.dart'; import 'providers/settings.dart';
import 'screens/screens.dart'; import 'screens/screens.dart';
import 'screens/inbox.dart';
class Home extends StatefulWidget { class Home extends StatefulWidget {
@override @override
@ -51,6 +52,10 @@ class _HomeState extends State<Home> {
List<BottomNavigationBarItem> _buildNavigationItems() { List<BottomNavigationBarItem> _buildNavigationItems() {
return [ return [
BottomNavigationBarItem(
icon: Icon(Icons.inbox),
title: Text('Inbox'),
),
BottomNavigationBarItem( BottomNavigationBarItem(
icon: Icon(Icons.rss_feed), icon: Icon(Icons.rss_feed),
title: Text('News'), title: Text('News'),
@ -73,12 +78,14 @@ class _HomeState extends State<Home> {
_buildScreen(int index) { _buildScreen(int index) {
switch (index) { switch (index) {
case 0: case 0:
return NewsScreen(); return InboxScreen();
case 1: case 1:
return SearchScreen(); return NewsScreen();
case 2: case 2:
return NotificationScreen(); return SearchScreen();
case 3: case 3:
return NotificationScreen();
case 4:
return ProfileScreen(); return ProfileScreen();
} }
} }

View File

@ -30,7 +30,7 @@ class _SettingsProviderState extends State<SettingsProvider> {
if (Platform.isIOS) { if (Platform.isIOS) {
layout = LayoutMap.cupertino; layout = LayoutMap.cupertino;
} }
// layout = LayoutMap.material; layout = LayoutMap.material;
} }
@override @override

54
lib/screens/inbox.dart Normal file
View File

@ -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]);
},
);
}
}

View File

@ -51,7 +51,7 @@ class _IssueScreenState extends State<IssueScreen> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return ListScaffold( return ListScaffold(
title: _fullName + ' #' + widget.id.toString(), title: Text(_fullName + ' #' + widget.id.toString()),
header: payload == null ? null : _buildHeader(), header: payload == null ? null : _buildHeader(),
itemCount: _items.length, itemCount: _items.length,
itemBuilder: (context, index) => TimelineItem(_items[index], payload), itemBuilder: (context, index) => TimelineItem(_items[index], payload),

View File

@ -16,7 +16,7 @@ class NewsScreenState extends State<NewsScreen> {
@override @override
Widget build(context) { Widget build(context) {
return ListScaffold( return ListScaffold(
title: 'News', title: Text('News'),
itemCount: _events.length, itemCount: _events.length,
itemBuilder: (context, index) => EventItem(_events[index]), itemBuilder: (context, index) => EventItem(_events[index]),
onRefresh: () async { onRefresh: () async {

View File

@ -45,7 +45,7 @@ class NotificationScreenState extends State<NotificationScreen> {
style: TextStyle(color: Colors.black, fontSize: 15), style: TextStyle(color: Colors.black, fontSize: 15),
), ),
items: group.items, items: group.items,
itemBuilder: (item) => NotificationItem(item: item), itemBuilder: (item) => NotificationItem(payload: item),
); );
} }

View File

@ -138,7 +138,7 @@ class _PullRequestScreenState extends State<PullRequestScreen> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return ListScaffold( return ListScaffold(
title: _fullName + ' #' + widget.id.toString(), title: Text(_fullName + ' #' + widget.id.toString()),
header: payload == null ? null : _buildHeader(), header: payload == null ? null : _buildHeader(),
itemCount: _items.length, itemCount: _items.length,
itemBuilder: (context, index) => TimelineItem(_items[index], payload), itemBuilder: (context, index) => TimelineItem(_items[index], payload),

View File

@ -18,7 +18,9 @@ class TimeAgo {
double diff = double diff =
(DateTime.now().millisecondsSinceEpoch - time.millisecondsSinceEpoch) / (DateTime.now().millisecondsSinceEpoch - time.millisecondsSinceEpoch) /
1000; 1000;
if (diff < 3600) { if (diff < 0) {
return 'in the future';
} else if (diff < 3600) {
return _pluralize(diff / 60, 'minute'); return _pluralize(diff / 60, 'minute');
} else if (diff < 86400) { } else if (diff < 86400) {
return _pluralize(diff / 3600, 'hour'); return _pluralize(diff / 3600, 'hour');

View File

@ -1,7 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart'; import 'package:flutter/gestures.dart';
import '../providers/settings.dart';
import '../screens/screens.dart'; import '../screens/screens.dart';
export 'github.dart'; export 'github.dart';
export 'octicons.dart'; export 'octicons.dart';
@ -14,6 +14,49 @@ Color convertColor(String cssHex) {
return Color(int.parse('ff' + cssHex, radix: 16)); 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) { TextSpan createLinkSpan(BuildContext context, String text, Function handle) {
return TextSpan( return TextSpan(
text: text, text: text,

View File

@ -142,8 +142,6 @@ class EventItem extends StatelessWidget {
build(BuildContext context) { build(BuildContext context) {
return Container( return Container(
padding: EdgeInsets.all(8), padding: EdgeInsets.all(8),
decoration: BoxDecoration(
border: Border(bottom: BorderSide(color: Colors.black12))),
child: Column( child: Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[ children: <Widget>[

View File

@ -7,7 +7,7 @@ import 'loading.dart';
typedef RefreshCallback = Future<void> Function(); typedef RefreshCallback = Future<void> Function();
class ListScaffold extends StatefulWidget { class ListScaffold extends StatefulWidget {
final String title; final Widget title;
final Widget header; final Widget header;
final int itemCount; final int itemCount;
final IndexedWidgetBuilder itemBuilder; final IndexedWidgetBuilder itemBuilder;
@ -77,11 +77,19 @@ class _ListScaffoldState extends State<ListScaffold> {
} }
Widget _buildItem(BuildContext context, int index) { Widget _buildItem(BuildContext context, int index) {
if (index == widget.itemCount) { if (index == 2 * widget.itemCount) {
return Loading(more: true); 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) { Widget _buildSliver(BuildContext context) {
@ -91,7 +99,7 @@ class _ListScaffoldState extends State<ListScaffold> {
return SliverList( return SliverList(
delegate: SliverChildBuilderDelegate( delegate: SliverChildBuilderDelegate(
_buildItem, _buildItem,
childCount: widget.itemCount + 1, childCount: 2 * widget.itemCount + 1,
), ),
); );
} }
@ -103,7 +111,7 @@ class _ListScaffoldState extends State<ListScaffold> {
} else { } else {
return ListView.builder( return ListView.builder(
controller: _controller, controller: _controller,
itemCount: widget.itemCount + 1, itemCount: 2 * widget.itemCount + 1,
itemBuilder: _buildItem, itemBuilder: _buildItem,
); );
} }
@ -122,7 +130,7 @@ class _ListScaffoldState extends State<ListScaffold> {
slivers.add(_buildSliver(context)); slivers.add(_buildSliver(context));
return CupertinoPageScaffold( return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(middle: Text(widget.title)), navigationBar: CupertinoNavigationBar(middle: widget.title),
child: SafeArea( child: SafeArea(
child: CustomScrollView( child: CustomScrollView(
controller: _controller, controller: _controller,
@ -132,7 +140,7 @@ class _ListScaffoldState extends State<ListScaffold> {
); );
default: default:
return Scaffold( return Scaffold(
appBar: AppBar(title: Text(widget.title)), appBar: AppBar(title: widget.title),
body: RefreshIndicator( body: RefreshIndicator(
onRefresh: widget.onRefresh, onRefresh: widget.onRefresh,
child: _buildBody(context), child: _buildBody(context),

View File

@ -2,30 +2,56 @@ import 'dart:core';
import 'package:flutter/material.dart' hide Notification; import 'package:flutter/material.dart' hide Notification;
import 'package:flutter/cupertino.dart' hide Notification; import 'package:flutter/cupertino.dart' hide Notification;
import '../utils/utils.dart'; import '../utils/utils.dart';
import '../screens/issue.dart';
import '../screens/pull_request.dart';
import 'link.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 { class NotificationItem extends StatelessWidget {
const NotificationItem({ const NotificationItem({
Key key, Key key,
@required this.item, @required this.payload,
}) : super(key: key); }) : super(key: key);
final Notification item; final NotificationPayload payload;
Widget _buildRoute(Notification item) { Widget _buildRoute() {
String type = item.subject.type; switch (payload.type) {
switch (type) {
case 'Issue': case 'Issue':
return IssueScreen(payload.number, payload.owner, payload.name);
case 'PullRequest': case 'PullRequest':
// return IssueScreen(item.repository.); return PullRequestScreen(payload.number, payload.owner, payload.name);
default: default:
// throw new Exception('Unhandled notification type: $type'); // throw new Exception('Unhandled notification type: $type');
return Text('test'); return Text('test');
} }
} }
IconData _buildIconData(String type) { IconData _buildIconData() {
switch (type) { switch (payload.type) {
case 'Issue': case 'Issue':
return Octicons.issue_opened; return Octicons.issue_opened;
// color: Color.fromRGBO(0x28, 0xa7, 0x45, 1), // color: Color.fromRGBO(0x28, 0xa7, 0x45, 1),
@ -42,39 +68,49 @@ class NotificationItem extends StatelessWidget {
return Link( return Link(
onTap: () { onTap: () {
Navigator.of(context).push( Navigator.of(context).push(
CupertinoPageRoute(builder: (context) => _buildRoute(item)), CupertinoPageRoute(builder: (context) => _buildRoute()),
); );
}, },
child: Row( child: Container(
children: <Widget>[ padding: EdgeInsets.all(8),
Container( color: payload.unread ? Colors.white : Colors.black12,
padding: EdgeInsets.symmetric(horizontal: 8), child: Column(
child: Icon( crossAxisAlignment: CrossAxisAlignment.start,
_buildIconData(item.subject.type), children: <Widget>[
color: Colors.black45, Row(
), crossAxisAlignment: CrossAxisAlignment.start,
),
Expanded(
child: Row(
children: <Widget>[ children: <Widget>[
Container(
padding: EdgeInsets.only(right: 8, top: 20),
child: Icon(_buildIconData(), color: Colors.black45),
),
Expanded( Expanded(
child: Container( child: Container(
padding: EdgeInsets.symmetric(vertical: 8),
child: Column( child: Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[ children: <Widget>[
Text( 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), style: TextStyle(fontSize: 15),
maxLines: 3, maxLines: 3,
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
), ),
Padding(padding: EdgeInsets.only(top: 8)), Padding(padding: EdgeInsets.only(top: 6)),
Text( Text(
TimeAgo.format(item.updatedAt), payload.updateAt,
style: TextStyle( style: TextStyle(
fontSize: 12, fontSize: 12,
color: Colors.black87, // fontWeight: FontWeight.w300,
color: Colors.black54,
), ),
) )
], ],
@ -87,8 +123,8 @@ class NotificationItem extends StatelessWidget {
), ),
], ],
), ),
), ],
], ),
), ),
); );
} }