diff --git a/lib/models/notification.dart b/lib/models/notification.dart index 20c5804..a4d90a8 100644 --- a/lib/models/notification.dart +++ b/lib/models/notification.dart @@ -1,4 +1,55 @@ import 'package:flutter/material.dart'; +import 'package:timeago/timeago.dart' as timeago; + +class NotificationPayload { + String id; + String type; + String owner; + String name; + int number; + String title; + String updateAt; + bool unread; + + String state; + + String get key => '_' + number.toString(); + + NotificationPayload.fromJson(input) { + id = input['id']; + type = input['subject']['type']; + name = input['repository']['name']; + owner = input['repository']['owner']['login']; + + String url = input['subject']['url']; + + if (type == 'Issue' || type == 'PullRequest') { + String numberStr = url.split('/').lastWhere((_) => true); + number = int.parse(numberStr); + } else { + // print(input); + } + + title = input['subject']['title']; + updateAt = timeago.format(DateTime.parse(input['updated_at'])); + unread = input['unread']; + } +} + +class NotificationGroup { + String owner; + String name; + get repo => owner + '/' + name; + List items = []; + + // Add heading _ to fix number case + // - => __ + // . => ___ + String get key => + ('_' + owner + '_' + name).replaceAll('-', '__').replaceAll('.', '___'); + + NotificationGroup(this.owner, this.name); +} class NotificationModel with ChangeNotifier { int _count = 0; diff --git a/lib/screens/notifications.dart b/lib/screens/notifications.dart index fedc334..051186d 100644 --- a/lib/screens/notifications.dart +++ b/lib/screens/notifications.dart @@ -11,28 +11,6 @@ import '../widgets/link.dart'; import '../widgets/empty.dart'; import '../utils/utils.dart'; -String getRepoKey(NotificationGroup group) { - // Add heading _ to fix number case - // - => __ - // . => ___ - return ('_' + group.owner + '_' + group.name) - .replaceAll('-', '__') - .replaceAll('.', '___'); -} - -String getItemKey(NotificationPayload item) { - return '_' + item.number.toString(); -} - -class NotificationGroup { - String owner; - String name; - get repo => owner + '/' + name; - List items = []; - - NotificationGroup(this.owner, this.name); -} - class NotificationScreen extends StatefulWidget { @override NotificationScreenState createState() => NotificationScreenState(); @@ -74,8 +52,6 @@ class NotificationScreenState extends State { // query state of issues and pull requests var schema = '{'; _groupMap.forEach((repo, group) { - var repoKey = getRepoKey(group); - // Check if issue and pull request exist if (group.items.where((item) { return item.type == 'Issue' || item.type == 'PullRequest'; @@ -84,10 +60,10 @@ class NotificationScreenState extends State { } schema += - '$repoKey: repository(owner: "${group.owner}", name: "${group.name}") {'; + '${group.key}: repository(owner: "${group.owner}", name: "${group.name}") {'; group.items.forEach((item) { - var key = getItemKey(item); + var key = item.key; switch (item.type) { case 'Issue': @@ -115,10 +91,10 @@ $key: pullRequest(number: ${item.number}) { var data = await SettingsProvider.of(context).query(schema); _groupMap.forEach((repo, group) { group.items.forEach((item) { - var groupData = data[getRepoKey(group)]; + var groupData = data[group.key]; if (groupData == null) return; - var itemData = data[getRepoKey(group)][getItemKey(item)]; + var itemData = data[group.key][item.key]; if (itemData != null) { item.state = itemData['state']; } diff --git a/lib/widgets/notification_item.dart b/lib/widgets/notification_item.dart index 526f560..a3cb844 100644 --- a/lib/widgets/notification_item.dart +++ b/lib/widgets/notification_item.dart @@ -1,6 +1,6 @@ import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; -import 'package:timeago/timeago.dart' as timeago; +import 'package:git_touch/models/notification.dart'; import 'package:primer/primer.dart'; import 'package:url_launcher/url_launcher.dart'; import '../utils/utils.dart'; @@ -9,39 +9,6 @@ import '../screens/issue.dart'; import '../providers/settings.dart'; import 'link.dart'; -class NotificationPayload { - String id; - String type; - String owner; - String name; - int number; - String title; - String updateAt; - bool unread; - - String state; - - NotificationPayload.fromJson(input) { - id = input['id']; - type = input['subject']['type']; - name = input['repository']['name']; - owner = input['repository']['owner']['login']; - - String url = input['subject']['url']; - - if (type == 'Issue' || type == 'PullRequest') { - String numberStr = url.split('/').lastWhere((_) => true); - number = int.parse(numberStr); - } else { - // print(input); - } - - title = input['subject']['title']; - updateAt = timeago.format(DateTime.parse(input['updated_at'])); - unread = input['unread']; - } -} - class NotificationItem extends StatefulWidget { final NotificationPayload payload; final Function markAsRead;