From f397b18f91947059e23837e2f86a5adc3be98d5a Mon Sep 17 00:00:00 2001 From: Garrit Franke Date: Fri, 12 Feb 2021 13:47:04 +0100 Subject: [PATCH] feat: mark entries as read --- lib/main.dart | 3 ++- lib/miniflux.dart | 42 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 37dddd0..c6ee226 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -65,7 +65,8 @@ class _MyHomePageState extends State { _readArticle(FeedEntry entry) async { if (await canLaunch(entry.url)) { - await launch(entry.url); + MinifluxApi.instance.markAsRead(entry.id); + launch(entry.url); } else { throw 'Could not launch ${entry.url}'; } diff --git a/lib/miniflux.dart b/lib/miniflux.dart index 1107269..cd248f7 100644 --- a/lib/miniflux.dart +++ b/lib/miniflux.dart @@ -22,6 +22,12 @@ class FeedEntry { } } +enum RequestMethod { + get, + post, + put, +} + class MinifluxApi { final storage = new LocalStorage(Config.IDENTIFIER_LOCALSTORAGE); String serverUrl; @@ -34,7 +40,9 @@ class MinifluxApi { static final MinifluxApi instance = MinifluxApi._(); - Future getRequest(String path) async { + // TODO: Make DRY + Future request( + {RequestMethod method, String path, body: String}) async { if (serverUrl == null || serverUrl.isEmpty || apiKey == null || @@ -43,14 +51,29 @@ class MinifluxApi { return null; } - return await http.get("$serverUrl/v1$path", - headers: {"X-Auth-Token": apiKey}); + switch (method) { + // GET is default + case RequestMethod.get: + break; + case RequestMethod.post: + return await http + .post("$serverUrl/v1$path", headers: {"X-Auth-Token": apiKey}); + break; + case RequestMethod.put: + return await http.put("$serverUrl/v1$path", + headers: {"X-Auth-Token": apiKey}, body: body); + break; + } + + return await http + .get("$serverUrl/v1$path", headers: {"X-Auth-Token": apiKey}); } Future> getUnreadPosts() async { await storage.ready; - Response response = await getRequest("/entries?status=unread"); + Response response = await request( + method: RequestMethod.get, path: "/entries?status=unread"); Map body = json.decode(response.body); List rawEntries = body["entries"]; @@ -59,4 +82,15 @@ class MinifluxApi { for (var entry in rawEntries) feedEntries.add(FeedEntry.fromJson(entry)); return feedEntries; } + + Future markAsRead(int id) async { + var body = { + "entry_ids": [id], + "status": "read" + }; + String encodedBody = json.encode(body); + Response res = await request( + body: encodedBody, method: RequestMethod.put, path: "/entries"); + return res; + } } -- 2.45.2