mark as read gesture

This commit is contained in:
Lea 2022-12-30 17:10:09 +01:00
parent 3dd315d37a
commit 0d3ca86df9
Signed by: Lea
GPG key ID: 1BAFFE8347019C42
2 changed files with 82 additions and 16 deletions

View file

@ -48,7 +48,8 @@ class FeverAPI {
/// Checks whether the given API URL is a valid Fever API endpoint and the given API key is authorized to access it
static Future<bool> isAuthenticated(String url, String key) async {
try {
final response = jsonDecode((await http.post(Uri.parse(url), body: { 'api_key': key })).body);
final response = jsonDecode(
(await http.post(Uri.parse(url), body: {'api_key': key})).body);
return response['api_version'] == 3 && response['auth'] == 1;
} catch (e) {
print(e);
@ -71,6 +72,8 @@ class APIRequestBuilder {
final http.Client _httpClient;
List<String> args = [];
Item? _markedItem;
ItemMarkType? _markedItemAs;
APIRequestBuilder(this.api, this._httpClient);
@ -100,11 +103,23 @@ class APIRequestBuilder {
return this;
}
APIRequestBuilder markItem(Item item, ItemMarkType markAs) {
_addArg('mark=item');
_addArg('as=${markAs.name}');
_addArg('id=${item.id}');
_markedItem = item;
_markedItemAs = markAs;
return this;
}
/// Executes the API request and returns the JSON data
Future<Map<String, dynamic>> fetch() async {
if (api.apiKey == null || api.apiUrl == null) throw Exception('API Key or API URL not set');
if (api.apiKey == null || api.apiUrl == null) {
throw Exception('API Key or API URL not set');
}
final response = await _httpClient.post(generateUrl(), body: { 'api_key': api.apiKey });
final response =
await _httpClient.post(generateUrl(), body: {'api_key': api.apiKey});
final data = jsonDecode((response).body);
if (data['auth'] == 0) throw UnauthenticatedException(data['auth']);
return data;
@ -152,5 +167,30 @@ class APIRequestBuilder {
api.cache.feedsGroups.clear();
api.cache.feedsGroups.addAll(feedsGroups);
}
if (_markedItem != null && _markedItemAs != null) {
switch (_markedItemAs!) {
case ItemMarkType.read:
_markedItem!.isRead = true;
break;
case ItemMarkType.unread:
_markedItem!.isRead = false;
break;
case ItemMarkType.saved:
_markedItem!.isSaved = true;
break;
case ItemMarkType.unsaved:
_markedItem!.isSaved = false;
break;
}
api.cache.items.set(_markedItem!.id, _markedItem!);
}
}
}
enum ItemMarkType {
read,
unread,
saved,
unsaved,
}

View file

@ -7,12 +7,18 @@ String formatDate(DateTime date) {
return '${date.day}.${date.month}.${date.year}, ${date.hour}:${date.minute < 10 ? '0${date.minute}' : date.minute}';
}
class HomepagePost extends StatelessWidget {
class HomepagePost extends StatefulWidget {
final Item post;
const HomepagePost({super.key, required this.post});
@override
State<StatefulWidget> createState() => _HomepagePostState();
}
class _HomepagePostState extends State<HomepagePost> {
@override
Widget build(BuildContext context) {
var post = widget.post;
return Container(
margin: const EdgeInsets.symmetric(vertical: 6.0, horizontal: 8.0),
decoration: BoxDecoration(
@ -27,9 +33,29 @@ class HomepagePost extends StatelessWidget {
child: InkWell(
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(builder: (context) => ArticlePage(article: post)),
MaterialPageRoute(
builder: (context) => ArticlePage(article: post)),
);
},
onLongPress: () async {
var theme = Theme.of(context);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
'Marking post as ${post.isRead ? 'Unread' : 'Read'}',
style: TextStyle(color: theme.colorScheme.onBackground)
),
duration: const Duration(seconds: 2),
backgroundColor: theme.colorScheme.secondaryContainer,
),
);
await post.api
.request()
.markItem(
post, post.isRead ? ItemMarkType.unread : ItemMarkType.read)
.execute();
setState(() {});
},
borderRadius: const BorderRadius.all(Radius.circular(15)),
child: Container(
margin: const EdgeInsets.all(8.0),