1
0
mirror of https://github.com/git-touch/git-touch synced 2025-01-18 18:29:59 +01:00

refactor: notification model

This commit is contained in:
Rongjian Zhang 2019-09-03 11:56:25 +08:00
parent 9b1921adda
commit 85e02e84a6
3 changed files with 56 additions and 62 deletions

View File

@ -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<NotificationPayload> 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;

View File

@ -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<NotificationPayload> items = [];
NotificationGroup(this.owner, this.name);
}
class NotificationScreen extends StatefulWidget {
@override
NotificationScreenState createState() => NotificationScreenState();
@ -74,8 +52,6 @@ class NotificationScreenState extends State<NotificationScreen> {
// 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<NotificationScreen> {
}
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'];
}

View File

@ -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;