~garritfra/FlutterFlux

ref: c11044279fdb22d6f51be3b3bda56722a12d2966 FlutterFlux/lib/views/unread_view.dart -rw-r--r-- 2.7 KiB
c1104427Garrit Franke feat: display placeholder if no unread posts left 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:localstorage/localstorage.dart';
import 'package:url_launcher/url_launcher.dart';

import '../config.dart';
import '../miniflux.dart';

class UnreadView extends StatefulWidget {
  UnreadView({Key key}) : super(key: key);

  @override
  _UnreadViewState createState() => _UnreadViewState();
}

class _UnreadViewState extends State<UnreadView> {
  final LocalStorage storage = new LocalStorage(Config.IDENTIFIER_LOCALSTORAGE);
  List<FeedEntry> unreadPosts = [];

  /// Check if the user token is set.
  /// If that's not the case, redirect to the config page
  Future<bool> checkConfig() async {
    await storage.ready;
    dynamic token = await storage.getItem(Config.IDENTIFIER_API_KEY);

    if (token == null || token.isEmpty) {
      Navigator.pushNamed(context, "/config");
      return false;
    }
    return true;
  }

  void _initUnreadPosts() async {
    var posts = await MinifluxApi.instance.getUnreadPosts();
    setState(() {
      unreadPosts = posts;
    });
  }

  void _readArticle(FeedEntry entry) async {
    if (await canLaunch(entry.url)) {
      MinifluxApi.instance.markAsRead(entry.id);
      launch(entry.url);
      setState(() {
        unreadPosts.remove(entry);
      });
    } else {
      throw 'Could not launch ${entry.url}';
    }
  }

  @override
  void initState() {
    checkConfig().then((success) {
      if (success) {
        _initUnreadPosts();
      }
    });

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    if (unreadPosts.isEmpty) {
      return Container(
        child: Center(
          child: Text("All done!"),
        ),
      );
    }
    return Container(
        key: UniqueKey(),
        child: Center(
          child: ListView.builder(
            itemCount: unreadPosts.length,
            key: UniqueKey(),
            itemBuilder: (BuildContext context, int index) {
              FeedEntry entry = unreadPosts[index];
              return Dismissible(
                key: UniqueKey(),
                direction: DismissDirection.horizontal,
                onDismissed: (direction) {
                  setState(() {
                    unreadPosts.removeAt(index);
                  });
                  MinifluxApi.instance.markAsRead(entry.id);
                },
                child: Card(
                  child: ListTile(
                    key: UniqueKey(),
                    title: Text(entry.title),
                    subtitle: Text(entry.feed.title),
                    onTap: () => _readArticle(entry),
                  ),
                ),
              );
            },
          ),
        ));
  }
}