mirror of
https://github.com/git-touch/git-touch
synced 2025-01-31 16:14:49 +01:00
refactor(gh): query with get json
This commit is contained in:
parent
2e2aa7f235
commit
16ac73bc88
@ -394,17 +394,6 @@ class AuthModel with ChangeNotifier {
|
||||
return data['data'];
|
||||
}
|
||||
|
||||
Future<dynamic> getWithCredentials(String url) async {
|
||||
final res = await http
|
||||
.get(_apiPrefix + url, headers: _headers)
|
||||
.timeout(_timeoutDuration);
|
||||
final data = json.decode(res.body);
|
||||
if (res.statusCode >= 400) {
|
||||
throw data['message'];
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
String _oauthState;
|
||||
void redirectToGithubOauth() {
|
||||
_oauthState = nanoid();
|
||||
|
@ -131,7 +131,6 @@ class GithubEventRelease {
|
||||
@JsonSerializable(fieldRename: FieldRename.snake)
|
||||
class GithubNotificationItem {
|
||||
String id;
|
||||
String htmlUrl;
|
||||
GithubNotificationItemSubject subject;
|
||||
DateTime updatedAt;
|
||||
GithubNotificationItemRepo repository;
|
||||
|
@ -168,7 +168,6 @@ GithubNotificationItem _$GithubNotificationItemFromJson(
|
||||
Map<String, dynamic> json) {
|
||||
return GithubNotificationItem()
|
||||
..id = json['id'] as String
|
||||
..htmlUrl = json['html_url'] as String
|
||||
..subject = json['subject'] == null
|
||||
? null
|
||||
: GithubNotificationItemSubject.fromJson(
|
||||
@ -187,7 +186,6 @@ Map<String, dynamic> _$GithubNotificationItemToJson(
|
||||
GithubNotificationItem instance) =>
|
||||
<String, dynamic>{
|
||||
'id': instance.id,
|
||||
'html_url': instance.htmlUrl,
|
||||
'subject': instance.subject,
|
||||
'updated_at': instance.updatedAt?.toIso8601String(),
|
||||
'repository': instance.repository,
|
||||
|
@ -22,7 +22,8 @@ class GhNewsScreenState extends State<GhNewsScreen> {
|
||||
// Check if there are unread notification items.
|
||||
// 1 item is enough since count is not displayed for now.
|
||||
var items = await Provider.of<AuthModel>(context)
|
||||
.getWithCredentials('/notifications?per_page=1');
|
||||
.ghClient
|
||||
.getJSON('/notifications?per_page=1');
|
||||
|
||||
if (items is List && items.isNotEmpty) {
|
||||
Provider.of<NotificationModel>(context).setCount(1);
|
||||
@ -33,15 +34,14 @@ class GhNewsScreenState extends State<GhNewsScreen> {
|
||||
Future<ListPayload<GithubEvent, int>> fetchEvents([int page = 1]) async {
|
||||
final auth = Provider.of<AuthModel>(context);
|
||||
final login = auth.activeAccount.login;
|
||||
List data = await auth.getWithCredentials(
|
||||
'/users/$login/received_events?page=$page&per_page=$pageSize');
|
||||
// Fimber.d(data.length);
|
||||
var hasMore = data.length == pageSize;
|
||||
var events = data.map((item) => GithubEvent.fromJson(item)).toList();
|
||||
|
||||
final events = await auth.ghClient.getJSON(
|
||||
'/users/$login/received_events?page=$page&per_page=$pageSize',
|
||||
convert: (vs) => [for (var v in vs) GithubEvent.fromJson(v)],
|
||||
);
|
||||
return ListPayload(
|
||||
cursor: page + 1,
|
||||
hasMore: hasMore,
|
||||
hasMore: events.length == pageSize,
|
||||
items: events,
|
||||
);
|
||||
}
|
||||
|
@ -21,11 +21,11 @@ class GhNotificationScreen extends StatefulWidget {
|
||||
|
||||
class GhNotificationScreenState extends State<GhNotificationScreen> {
|
||||
Future<Map<String, NotificationGroup>> fetchNotifications(int index) async {
|
||||
List items = await Provider.of<AuthModel>(context).getWithCredentials(
|
||||
'/notifications?all=${index == 2}&participating=${index == 1}');
|
||||
final ns =
|
||||
items.map((item) => GithubNotificationItem.fromJson(item)).toList();
|
||||
|
||||
final ns = await Provider.of<AuthModel>(context).ghClient.getJSON(
|
||||
'/notifications?all=${index == 2}&participating=${index == 1}',
|
||||
convert: (vs) =>
|
||||
[for (var v in vs) GithubNotificationItem.fromJson(v)],
|
||||
);
|
||||
if (index == 0) {
|
||||
Provider.of<NotificationModel>(context).setCount(ns.length);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user