Skip to content

Commit

Permalink
feat: notification mark as read
Browse files Browse the repository at this point in the history
  • Loading branch information
pd4d10 committed Feb 6, 2019
1 parent 8e678a3 commit c0629c5
Show file tree
Hide file tree
Showing 15 changed files with 450 additions and 202 deletions.
18 changes: 6 additions & 12 deletions lib/main.dart
Expand Up @@ -52,22 +52,18 @@ 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'),
),
BottomNavigationBarItem(
icon: Icon(Icons.search),
title: Text('Search'),
),
BottomNavigationBarItem(
icon: _buildNotificationIcon(context),
title: Text('Notification'),
),
BottomNavigationBarItem(
icon: Icon(Icons.search),
title: Text('Search'),
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
title: Text('Me'),
Expand All @@ -78,14 +74,12 @@ class _HomeState extends State<Home> {
_buildScreen(int index) {
switch (index) {
case 0:
return InboxScreen();
case 1:
return NewsScreen();
case 1:
return NotificationScreen();
case 2:
return SearchScreen();
case 3:
return NotificationScreen();
case 4:
return ProfileScreen();
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/providers/settings.dart
Expand Up @@ -30,7 +30,7 @@ class _SettingsProviderState extends State<SettingsProvider> {
if (Platform.isIOS) {
layout = LayoutMap.cupertino;
}
layout = LayoutMap.material;
// layout = LayoutMap.material;
}

@override
Expand Down
153 changes: 146 additions & 7 deletions lib/screens/inbox.dart
@@ -1,15 +1,144 @@
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import '../widgets/list_scaffold.dart';
import '../widgets/notification_item.dart';
import '../utils/utils.dart';
import '../screens/issue.dart';
import '../screens/pull_request.dart';
import '../widgets/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.payload,
}) : super(key: key);

final NotificationPayload payload;

Widget _buildRoute() {
switch (payload.type) {
case 'Issue':
return IssueScreen(payload.number, payload.owner, payload.name);
case 'PullRequest':
return PullRequestScreen(payload.number, payload.owner, payload.name);
default:
// throw new Exception('Unhandled notification type: $type');
return Text('test');
}
}

IconData _buildIconData() {
switch (payload.type) {
case 'Issue':
return Octicons.issue_opened;
// color: Color.fromRGBO(0x28, 0xa7, 0x45, 1),
case 'PullRequest':
return Octicons.git_pull_request;
// color: Color.fromRGBO(0x6f, 0x42, 0xc1, 1),
default:
return Octicons.person;
}
}

@override
Widget build(BuildContext context) {
return Link(
onTap: () {
Navigator.of(context).push(
CupertinoPageRoute(builder: (context) => _buildRoute()),
);
},
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(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
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: 6)),
Text(
payload.updateAt,
style: TextStyle(
fontSize: 12,
// fontWeight: FontWeight.w300,
color: Colors.black54,
),
)
],
),
),
),
Column(
children: <Widget>[
Icon(Octicons.check, color: Colors.black45),
Icon(Octicons.unmute, color: Colors.black45)
],
),
],
),
],
),
),
);
}
}

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();
}

/// [@deprecated]
class InboxScreen extends StatefulWidget {
@override
_InboxScreenState createState() => _InboxScreenState();
Expand All @@ -27,17 +156,27 @@ class _InboxScreenState extends State<InboxScreen> {
2: 'All',
};

Future<void> _refresh() async {
page = 1;
var items = await fetchNotifications(page);
setState(() {
_items = items;
});
}

@override
Widget build(BuildContext context) {
return ListScaffold(
title: Text('Inbox'),
onRefresh: () async {
page = 1;
var items = await fetchNotifications(page);
setState(() {
_items = items;
});
trailingIconData: Octicons.check,
trailingOnTap: () async {
bool answer = await showConfim(context, 'Mark all as read?');
if (answer == true) {
await putWithCredentials('/notifications');
_refresh();
}
},
onRefresh: _refresh,
onLoadMore: () async {
page = page + 1;
var items = await fetchNotifications(page);
Expand Down
21 changes: 20 additions & 1 deletion lib/screens/issue.dart
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter/cupertino.dart';
import '../utils/utils.dart';
import '../widgets/list_scaffold.dart';
import '../widgets/timeline_item.dart';
import '../widgets/comment_item.dart';

Future queryIssue(int id, String owner, String name) async {
var data = await query('''
Expand Down Expand Up @@ -41,7 +42,25 @@ class _IssueScreenState extends State<IssueScreen> {
Map<String, dynamic> payload;

Widget _buildHeader() {
return Text('issue');
return Column(children: <Widget>[
Container(
// padding: EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
payload['title'],
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
height: 1.2,
),
),
CommentItem(payload),
],
),
)
]);
}

get _fullName => widget.owner + '/' + widget.name;
Expand Down

0 comments on commit c0629c5

Please sign in to comment.