Skip to content

Commit

Permalink
✨ subscribe comics (button)
Browse files Browse the repository at this point in the history
  • Loading branch information
niuhuan committed Aug 30, 2024
1 parent a535b57 commit 2882cdb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/basic/Method.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1061,8 +1061,8 @@ class Method {
return ComicSubscribe.fromJson(jsonDecode(data));
}

Future addSubscribed(ComicInfo comicInfo) async {
return _flatInvoke("addSubscribed", comicInfo);
Future addSubscribed(String comicId) async {
return _flatInvoke("addSubscribed", comicId);
}

Future removeAllSubscribed() async {
Expand Down
48 changes: 48 additions & 0 deletions lib/screens/ComicInfoScreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class _ComicInfoScreenState extends State<ComicInfoScreen> with RouteAware {
late Future<ComicInfo> _comicFuture = _loadComic();
late Key _comicFutureKey = UniqueKey();
late Future<ViewLog?> _viewFuture = _loadViewLog();
late Future<ComicSubscribe?> _subscribedFuture = _loadSubscribed();
late Future<List<Ep>> _epListFuture = _loadEps();
StreamSubscription<String?>? _linkSubscription;

Expand All @@ -63,6 +64,10 @@ class _ComicInfoScreenState extends State<ComicInfoScreen> with RouteAware {
return method.loadView(widget.comicId);
}

Future<ComicSubscribe?> _loadSubscribed() {
return method.loadSubscribed(widget.comicId);
}

@override
void didChangeDependencies() {
super.didChangeDependencies();
Expand Down Expand Up @@ -144,6 +149,7 @@ class _ComicInfoScreenState extends State<ComicInfoScreen> with RouteAware {
appBar: AppBar(
title: Text(_comicInfo.title),
actions: [
_buildSubscribeAction(_subscribedFuture, _comicInfo),
_buildDownloadAction(_epListFuture, _comicInfo),
],
),
Expand Down Expand Up @@ -262,6 +268,48 @@ class _ComicInfoScreenState extends State<ComicInfoScreen> with RouteAware {
);
}

Widget _buildSubscribeAction(
Future<ComicSubscribe?> _subscribedFuture,
ComicInfo _comicInfo,
) {
return FutureBuilder(
future: _subscribedFuture,
builder: (BuildContext context, AsyncSnapshot<ComicSubscribe?> snapshot) {
if (snapshot.hasError) {
return IconButton(
onPressed: () {
setState(() {
this._subscribedFuture = _loadSubscribed();
});
},
icon: const Icon(Icons.sync_problem),
);
}
if (snapshot.connectionState != ConnectionState.done) {
return IconButton(onPressed: () {}, icon: const Icon(Icons.sync));
}
var _subscribed = snapshot.data;
return IconButton(
onPressed: () async {
if (_subscribed == null) {
await method.addSubscribed(_comicInfo.id);
} else {
await method.removeSubscribed(_comicInfo.id);
}
setState(() {
this._subscribedFuture = _loadSubscribed();
});
},
icon: Icon(
_subscribed == null
? Icons.notifications_none
: Icons.notifications,
),
);
},
);
}

Widget _buildDownloadAction(
Future<List<Ep>> _epListFuture,
ComicInfo _comicInfo,
Expand Down

0 comments on commit 2882cdb

Please sign in to comment.