fix(gh): subscribe permissions

related: #47
This commit is contained in:
Rongjian Zhang 2020-04-06 11:18:35 +08:00
parent a1984309a9
commit 80764817ca
1 changed files with 68 additions and 52 deletions

View File

@ -1,3 +1,5 @@
import 'dart:convert';
import 'package:filesize/filesize.dart'; import 'package:filesize/filesize.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
@ -49,19 +51,6 @@ class GhRepoScreen extends StatelessWidget {
} }
} }
String _buildWatchSelectState(GhWatchSubscriptionState state) {
switch (state) {
case GhWatchSubscriptionState.IGNORED:
return 'Ignoring';
case GhWatchSubscriptionState.SUBSCRIBED:
return 'Watching';
case GhWatchSubscriptionState.UNSUBSCRIBED:
return 'Not watching';
default:
return '';
}
}
Future<String> _fetchReadme(BuildContext context) async { Future<String> _fetchReadme(BuildContext context) async {
try { try {
final auth = Provider.of<AuthModel>(context); final auth = Provider.of<AuthModel>(context);
@ -128,40 +117,70 @@ class GhRepoScreen extends StatelessWidget {
GhRepoSubscriptionState.SUBSCRIBED, GhRepoSubscriptionState.SUBSCRIBED,
text: _buildWatchState(repo.viewerSubscription), text: _buildWatchState(repo.viewerSubscription),
onPressed: () async { onPressed: () async {
final vs = GhWatchSubscriptionState.values.where((v) => final vs = GhRepoSubscriptionState.values.where((v) =>
v != GhWatchSubscriptionState.ARTEMIS_UNKNOWN); v != GhRepoSubscriptionState.ARTEMIS_UNKNOWN);
theme.showActions(context, [ theme.showActions(context, [
for (var v in vs) for (var v in vs)
ActionItem( ActionItem(
text: _buildWatchSelectState(v), text: _buildWatchState(v),
onTap: (_) async { onTap: (_) async {
final res = await auth.gqlClient.execute( switch (v) {
GhWatchQuery( case GhRepoSubscriptionState.SUBSCRIBED:
variables: GhWatchArguments( case GhRepoSubscriptionState.IGNORED:
id: repo.id, // TODO: https://github.com/SpinlockLabs/github.dart/pull/215
state: v, // final res = await auth.ghClient.activity
), // .setRepositorySubscription(
), // RepositorySlug(
); // repo.owner.login,
setState(() { // repo.name,
final r = res.data.updateSubscription // ),
.subscribable as GhWatchRepository; // subscribed: v ==
switch (r.viewerSubscription) { // GhRepoSubscriptionState.SUBSCRIBED,
case GhWatchSubscriptionState.IGNORED: // ignored:
repo.viewerSubscription = // v == GhRepoSubscriptionState.IGNORED,
GhRepoSubscriptionState.IGNORED; // );
break; final slug = RepositorySlug(
case GhWatchSubscriptionState.SUBSCRIBED: repo.owner.login, repo.name);
repo.viewerSubscription = final response =
GhRepoSubscriptionState.SUBSCRIBED; await auth.ghClient.request(
break; 'PUT',
case GhWatchSubscriptionState.UNSUBSCRIBED: '/repos/${slug.fullName}/subscription',
statusCode: StatusCodes.OK,
body: json.encode({
'subscribed': v ==
GhRepoSubscriptionState.SUBSCRIBED,
'ignored':
v == GhRepoSubscriptionState.IGNORED
}),
);
final res = RepositorySubscription.fromJson(
jsonDecode(response.body)
as Map<String, dynamic>);
setState(() {
if (res.subscribed) {
repo.viewerSubscription =
GhRepoSubscriptionState.SUBSCRIBED;
} else if (res.ignored) {
repo.viewerSubscription =
GhRepoSubscriptionState.IGNORED;
}
});
break;
case GhRepoSubscriptionState.UNSUBSCRIBED:
await auth.ghClient.activity
.deleteRepositorySubscription(
RepositorySlug(
repo.owner.login,
repo.name,
),
);
setState(() {
repo.viewerSubscription = repo.viewerSubscription =
GhRepoSubscriptionState.UNSUBSCRIBED; GhRepoSubscriptionState.UNSUBSCRIBED;
break; });
default: break;
} default:
}); }
}, },
) )
]); ]);
@ -172,18 +191,15 @@ class GhRepoScreen extends StatelessWidget {
active: repo.viewerHasStarred, active: repo.viewerHasStarred,
text: repo.viewerHasStarred ? 'Unstar' : 'Star', text: repo.viewerHasStarred ? 'Unstar' : 'Star',
onPressed: () async { onPressed: () async {
final res = await auth.gqlClient.execute( if (repo.viewerHasStarred) {
GhStarQuery( await auth.ghClient.activity.unstar(
variables: GhStarArguments( RepositorySlug(repo.owner.login, repo.name));
id: repo.id, } else {
flag: !repo.viewerHasStarred, await auth.ghClient.activity.star(
), RepositorySlug(repo.owner.login, repo.name));
), }
);
setState(() { setState(() {
repo.viewerHasStarred = res.data.removeStar?.starrable repo.viewerHasStarred = !repo.viewerHasStarred;
?.viewerHasStarred ??
res.data.addStar.starrable.viewerHasStarred;
}); });
}, },
), ),