~garritfra/FlutterFlux

7e48e712741b61bfadf42a3249b140877b406410 — Garrit Franke 3 years ago e945346
chore: refactor minifluxapi
2 files changed, 26 insertions(+), 13 deletions(-)

M lib/main.dart
M lib/miniflux.dart
M lib/main.dart => lib/main.dart +6 -6
@@ 57,17 57,17 @@ class _MyHomePageState extends State<MyHomePage> {
  }

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

  _launchURL(String url) async {
    if (await canLaunch(url)) {
      await launch(url);
  _readArticle(FeedEntry entry) async {
    if (await canLaunch(entry.url)) {
      await launch(entry.url);
    } else {
      throw 'Could not launch $url';
      throw 'Could not launch ${entry.url}';
    }
  }



@@ 102,7 102,7 @@ class _MyHomePageState extends State<MyHomePage> {
                    title: Text(entry.title),
                  ),
                ),
                onTap: () => _launchURL(entry.url),
                onTap: () => _readArticle(entry),
              );
            },
          )),

M lib/miniflux.dart => lib/miniflux.dart +20 -7
@@ 23,21 23,34 @@ class FeedEntry {
}

class MinifluxApi {
  Future<List<FeedEntry>> getUnreadPosts() async {
    final storage = new LocalStorage(Config.IDENTIFIER_LOCALSTORAGE);
    await storage.ready;
  final storage = new LocalStorage(Config.IDENTIFIER_LOCALSTORAGE);
  String serverUrl;
  String apiKey;

    String serverUrl = storage.getItem(Config.IDENTIFIER_SERVER_URL);
    String apiKey = storage.getItem(Config.IDENTIFIER_API_KEY);
  MinifluxApi._() {
    serverUrl = storage.getItem(Config.IDENTIFIER_SERVER_URL);
    apiKey = storage.getItem(Config.IDENTIFIER_API_KEY);
  }

  static final MinifluxApi instance = MinifluxApi._();

  Future<Response> getRequest(String path) async {
    if (serverUrl == null ||
        serverUrl.isEmpty ||
        apiKey == null ||
        apiKey.isEmpty) {
      return [];
      // TODO: Proper handling
      return null;
    }
    Response response = await http.get("$serverUrl/v1/entries?status=unread",

    return await http.get("$serverUrl/v1$path",
        headers: {"X-Auth-Token": apiKey});
  }

  Future<List<FeedEntry>> getUnreadPosts() async {
    await storage.ready;

    Response response = await getRequest("/entries?status=unread");
    Map<String, dynamic> body = json.decode(response.body);
    List<dynamic> rawEntries = body["entries"];