~cytrogen/fluent-reader-mobile

ref: 38a0d41c4c0741c309db56b7edf88c955bfe8c57 fluent-reader-mobile/lib/models/feeds_model.dart -rw-r--r-- 2.6 KiB
38a0d41c — Dedsd Added ptBR translation (#47) 4 years 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
import 'package:fluent_reader_lite/models/feed.dart';
import 'package:fluent_reader_lite/utils/global.dart';
import 'package:fluent_reader_lite/utils/store.dart';
import 'package:fluent_reader_lite/utils/utils.dart';
import 'package:flutter/cupertino.dart';

import 'item.dart';

enum ItemSwipeOption {
  ToggleRead, ToggleStar, Share, OpenMenu, OpenExternal,
}

class FeedsModel with ChangeNotifier {
  RSSFeed all = RSSFeed();
  RSSFeed source;

  bool _showThumb = Store.sp.getBool(StoreKeys.SHOW_THUMB) ?? true;
  bool get showThumb => _showThumb;
  set showThumb(bool value) {
    _showThumb = value;
    Store.sp.setBool(StoreKeys.SHOW_THUMB, value);
    notifyListeners();
  }

  bool _showSnippet = Store.sp.getBool(StoreKeys.SHOW_SNIPPET) ?? true;
  bool get showSnippet => _showSnippet;
  set showSnippet(bool value) {
    _showSnippet = value;
    Store.sp.setBool(StoreKeys.SHOW_SNIPPET, value);
    notifyListeners();
  }

  bool _dimRead = Store.sp.getBool(StoreKeys.DIM_READ) ?? false;
  bool get dimRead => _dimRead;
  set dimRead(bool value) {
    _dimRead = value;
    Store.sp.setBool(StoreKeys.DIM_READ, value);
    notifyListeners();
  }

  ItemSwipeOption _swipeR = ItemSwipeOption.values[Store.sp.getInt(StoreKeys.FEED_SWIPE_R) ?? 0];
  ItemSwipeOption get swipeR => _swipeR;
  set swipeR(ItemSwipeOption value) {
    _swipeR = value;
    Store.sp.setInt(StoreKeys.FEED_SWIPE_R, value.index);
    notifyListeners();
  }

  ItemSwipeOption _swipeL = ItemSwipeOption.values[Store.sp.getInt(StoreKeys.FEED_SWIPE_L) ?? 1];
  ItemSwipeOption get swipeL => _swipeL;
  set swipeL(ItemSwipeOption value) {
    _swipeL = value;
    Store.sp.setInt(StoreKeys.FEED_SWIPE_L, value.index);
    notifyListeners();
  }

  void broadcast() { notifyListeners(); }

  Future<void> initSourcesFeed(Iterable<String> sids) async {
    Set<String> sidSet = Set.from(sids);
    source = RSSFeed(sids: sidSet);
    await source.init();
  }

  void addFetchedItems(Iterable<RSSItem> items) {
    for (var feed in [all, source]) {
      if (feed == null) continue;
      var lastDate = feed.iids.length > 0
        ? Global.itemsModel.getItem(feed.iids.last).date
        : null;
      for (var item in items) {
        if (!feed.testItem(item)) continue;
        if (lastDate != null && item.date.isBefore(lastDate)) continue;
        var idx = Utils.binarySearch(feed.iids, item.id, (a, b) {
          return Global.itemsModel.getItem(b).date.compareTo(Global.itemsModel.getItem(a).date);
        });
        feed.iids.insert(idx, item.id);
      }
    }
    notifyListeners();
  }

  void initAll() {
    for (var feed in [all, source]) {
      if (feed == null) continue;
      feed.init();
    }
  }
}