Skip to content

Commit

Permalink
fix(github): repo and user mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
pd4d10 committed Jan 31, 2021
1 parent 31c43ee commit 10477a7
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 100 deletions.
18 changes: 12 additions & 6 deletions lib/scaffolds/refresh_stateful.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import 'package:git_touch/scaffolds/utils.dart';

class RefreshStatefulScaffold<T> extends StatefulWidget {
final Widget title;
final Widget Function(T data, void Function(VoidCallback fn) setState)
bodyBuilder;
final Widget Function(T data, void Function(T newData) setData) bodyBuilder;
final Future<T> Function() fetch;
final Widget Function(T data, void Function(VoidCallback fn) setState)
actionBuilder;
final Widget Function(T data, void Function(T newData) setData) actionBuilder;
final Widget action;
final canRefresh;

Expand Down Expand Up @@ -62,13 +60,21 @@ class _RefreshStatefulScaffoldState<T>
Widget get _action {
if (widget.action != null) return widget.action;
if (widget.actionBuilder == null || _data == null) return null;
return widget.actionBuilder(_data, setState);
return widget.actionBuilder(_data, (v) {
setState(() {
_data = v;
});
});
}

@override
Widget build(BuildContext context) {
Widget child = ErrorLoadingWrapper(
bodyBuilder: () => widget.bodyBuilder(_data, setState),
bodyBuilder: () => widget.bodyBuilder(_data, (v) {
setState(() {
_data = v;
});
}),
error: _error,
loading: _data == null,
reload: _refresh,
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/bb_repo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class BbRepoScreen extends StatelessWidget {
});
return Tuple3(repo, readme, branches);
},
bodyBuilder: (t, setState) {
bodyBuilder: (t, _) {
final theme = Provider.of<ThemeModel>(context);
final p = t.item1;
final branches = t.item3;
Expand Down
13 changes: 6 additions & 7 deletions lib/screens/ge_repo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class GeRepoScreen extends StatelessWidget {
StatusPayload statusPayload = StatusPayload(isWatching, isStarred);
return Tuple4(repo, readmeData, branches, statusPayload);
},
bodyBuilder: (t, setState) {
bodyBuilder: (t, setData) {
final p = t.item1;
final branches = t.item3;
final theme = context.read<ThemeModel>();
Expand All @@ -92,9 +92,9 @@ class GeRepoScreen extends StatelessWidget {
await context.read<AuthModel>().fetchGitee(
'/user/subscriptions/$owner/$name?watch_type=$watchType',
requestType: t.item4.isWatching ? 'DELETE' : 'PUT');
setState(() {
t.item4.isWatching = !t.item4.isWatching;
});

t.item4.isWatching = !t.item4.isWatching;
setData(t);
},
),
SizedBox(width: 8),
Expand All @@ -106,9 +106,8 @@ class GeRepoScreen extends StatelessWidget {
'/user/starred/$owner/$name',
requestType: t.item4.isStarred ? 'DELETE' : 'PUT');

setState(() {
t.item4.isStarred = !t.item4.isStarred;
});
t.item4.isStarred = !t.item4.isStarred;
setData(t);
},
)
])
Expand Down
4 changes: 2 additions & 2 deletions lib/screens/ge_user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ class GeUserScreen extends StatelessWidget {
: null,
actionBuilder: isViewer
? null
: (p, setState) {
: (p, _) {
return ActionButton(
title: 'User Actions',
items: [...ActionItem.getUrlActions(p.item1.htmlUrl)],
);
},
bodyBuilder: (p, setState) {
bodyBuilder: (p, _) {
final user = p.item1;
final repos = p.item2;

Expand Down
81 changes: 30 additions & 51 deletions lib/screens/gh_repo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class GhRepoScreen extends StatelessWidget {

return Tuple3(repo, countFuture, readmeData);
},
actionBuilder: (data, setState) {
actionBuilder: (data, _) {
final repo = data.item1;
return ActionButton(
title: S.of(context).repositoryActions,
Expand All @@ -102,7 +102,7 @@ class GhRepoScreen extends StatelessWidget {
],
);
},
bodyBuilder: (data, setState) {
bodyBuilder: (data, setData) {
final repo = data.item1;
final contributionFuture = data.item2;
final readmeData = data.item3;
Expand Down Expand Up @@ -133,49 +133,34 @@ class GhRepoScreen extends StatelessWidget {
ActionItem(
text: _buildWatchState(v),
onTap: (_) async {
final activityApi =
context.read<AuthModel>().ghClient.activity;
switch (v) {
case GSubscriptionState.SUBSCRIBED:
case GSubscriptionState.IGNORED:
await context
.read<AuthModel>()
.ghClient
.activity
.setRepositorySubscription(
RepositorySlug(
repo.owner.login, repo.name),
subscribed: v ==
GSubscriptionState.SUBSCRIBED,
ignored:
v == GSubscriptionState.IGNORED,
);
setState(() {
// if (res.subscribed) {
// repo.viewerSubscription =
// GSubscriptionState.SUBSCRIBED;
// } else if (res.ignored) {
// repo.viewerSubscription =
// GSubscriptionState.IGNORED;
// }
});
await activityApi.setRepositorySubscription(
RepositorySlug(
repo.owner.login, repo.name),
subscribed:
v == GSubscriptionState.SUBSCRIBED,
ignored: v == GSubscriptionState.IGNORED,
);
break;
case GSubscriptionState.UNSUBSCRIBED:
await context
.read<AuthModel>()
.ghClient
.activity
await activityApi
.deleteRepositorySubscription(
RepositorySlug(
repo.owner.login,
repo.name,
),
);
setState(() {
// repo.viewerSubscription =
// GSubscriptionState.UNSUBSCRIBED;
});
RepositorySlug(
repo.owner.login,
repo.name,
),
);
break;
default:
}

setData(data.withItem1(repo.rebuild((b) {
b.viewerSubscription = v;
})));
},
)
]);
Expand All @@ -186,24 +171,18 @@ class GhRepoScreen extends StatelessWidget {
active: repo.viewerHasStarred,
text: repo.viewerHasStarred ? 'Unstar' : 'Star',
onTap: () async {
final activityApi =
context.read<AuthModel>().ghClient.activity;
if (repo.viewerHasStarred) {
await context
.read<AuthModel>()
.ghClient
.activity
.unstar(
RepositorySlug(repo.owner.login, repo.name));
await activityApi.unstar(
RepositorySlug(repo.owner.login, repo.name));
} else {
await context
.read<AuthModel>()
.ghClient
.activity
.star(
RepositorySlug(repo.owner.login, repo.name));
await activityApi.star(
RepositorySlug(repo.owner.login, repo.name));
}
setState(() {
// repo.viewerHasStarred = !repo.viewerHasStarred;
});
setData(data.withItem1(repo.rebuild((b) {
b.viewerHasStarred = !repo.viewerHasStarred;
})));
},
),
],
Expand Down
64 changes: 36 additions & 28 deletions lib/screens/gh_user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ class _Repos extends StatelessWidget {
class _User extends StatelessWidget {
final GUserParts p;
final bool isViewer;
const _User(this.p, {this.isViewer = false});
final List<Widget> rightWidgets;
const _User(this.p, {this.isViewer = false, this.rightWidgets = const []});

@override
Widget build(BuildContext context) {
final theme = Provider.of<ThemeModel>(context);
final auth = Provider.of<AuthModel>(context);
final login = p.login;

return Column(
Expand All @@ -77,25 +77,7 @@ class _User extends StatelessWidget {
createdAt: p.createdAt,
bio: p.bio,
isViewer: isViewer,
rightWidgets: [
if (p.viewerCanFollow)
MutationButton(
active: p.viewerIsFollowing,
text: p.viewerIsFollowing
? S.of(context).unfollow
: S.of(context).follow,
onTap: () async {
if (p.viewerIsFollowing) {
await auth.ghClient.users.unfollowUser(p.login);
} else {
await auth.ghClient.users.followUser(p.login);
}
// setState(() {
// // p.viewerIsFollowing = !p.viewerIsFollowing;
// });
},
)
],
rightWidgets: rightWidgets,
),
CommonStyle.border,
Row(children: [
Expand Down Expand Up @@ -297,7 +279,7 @@ class GhViewer extends StatelessWidget {
iconData: Icons.settings,
url: '/settings',
),
bodyBuilder: (p, setState) {
bodyBuilder: (p, _) {
return _User(p, isViewer: true);
},
);
Expand All @@ -318,18 +300,44 @@ class GhUser extends StatelessWidget {
return res.data;
},
title: AppBarTitle(login),
actionBuilder: (payload, setState) {
actionBuilder: (payload, _) {
return ActionButton(
title: 'User Actions',
items: ActionItem.getUrlActions(payload.repositoryOwner.url),
);
},
bodyBuilder: (p, setState) {
if (p.repositoryOwner.G__typename == 'User') {
return _User(p.repositoryOwner as GUserData_repositoryOwner__asUser);
bodyBuilder: (data, setData) {
if (data.repositoryOwner.G__typename == 'User') {
final p = data.repositoryOwner as GUserData_repositoryOwner__asUser;
return _User(
p,
rightWidgets: [
if (p.viewerCanFollow)
MutationButton(
active: p.viewerIsFollowing,
text: p.viewerIsFollowing
? S.of(context).unfollow
: S.of(context).follow,
onTap: () async {
if (p.viewerIsFollowing) {
await auth.ghClient.users.unfollowUser(p.login);
} else {
await auth.ghClient.users.followUser(p.login);
}
setData(data.rebuild((b) {
final u = b.repositoryOwner
as GUserData_repositoryOwner__asUser;
b.repositoryOwner = u.rebuild((b1) {
b1.viewerIsFollowing = !b1.viewerIsFollowing;
});
}));
},
)
],
);
} else {
return _Org(
p.repositoryOwner as GUserData_repositoryOwner__asOrganization);
return _Org(data.repositoryOwner
as GUserData_repositoryOwner__asOrganization);
}
},
);
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/gl_project.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class GlProjectScreen extends StatelessWidget {

return Tuple5(p, langFuture, memberCountFuture, readmeData, branches);
},
actionBuilder: (t, setState) {
actionBuilder: (t, _) {
return ActionButton(
title: S.of(context).projectActions,
items: [
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/go_repo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class GoRepoScreen extends StatelessWidget {

return Tuple3(repo, readmeData, branches);
},
bodyBuilder: (t, setState) {
bodyBuilder: (t, _) {
final p = t.item1;
final branches = t.item3;
final theme = context.read<ThemeModel>();
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/gt_repo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class GtRepoScreen extends StatelessWidget {

return Tuple2(repo, readmeData);
},
bodyBuilder: (t, setState) {
bodyBuilder: (t, _) {
final p = t.item1;
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
Expand Down
5 changes: 3 additions & 2 deletions lib/widgets/user_header.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ class UserHeader extends StatelessWidget {
@required this.bio,
bool isViewer = false,
List<Widget> rightWidgets,
}) : this.rightWidgets = [
...(rightWidgets ?? []),
}) : rightWidgets = [
if (isViewer)
MutationButton(
active: false,
text: 'Switch accounts',
url: '/login',
)
else
...(rightWidgets ?? []),
];

@override
Expand Down

0 comments on commit 10477a7

Please sign in to comment.