~cytrogen/fluent-reader-mobile

fluent-reader-mobile/lib/utils/store.dart -rw-r--r-- 5.4 KiB
28b99e81 — Cytrogen feat: add subscription search/sort, rewrite README, upgrade infrastructure 9 hours 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import 'dart:convert';

import 'package:fluent_reader_lite/models/global_model.dart';
import 'package:fluent_reader_lite/models/source.dart';
import 'package:flutter/cupertino.dart';
import 'package:shared_preferences/shared_preferences.dart';

abstract class StoreKeys {
  static const GROUPS = "groups";
  static const ERROR_LOG = "errorLog";
  static const UNCATEGORIZED = "uncategorized";
  static const UNREAD_SUBS_ONLY = "unreadSubsOnly";
  static const SUBSCRIPTION_SORT = "subSort";

  // General
  static const THEME = "theme";
  static const LOCALE = "locale";
  static const KEEP_ITEMS_DAYS = "keepItemsD";
  static const SYNC_ON_START = "syncOnStart";
  static const IN_APP_BROWSER = "inAppBrowser";
  static const TEXT_SCALE = "textScale";

  // Feed preferences
  static const FEED_FILTER_ALL = "feedFilterA";
  static const FEED_FILTER_SOURCE = "feedFilterS";
  static const SHOW_THUMB = "showThumb";
  static const SHOW_SNIPPET = "showSnippet";
  static const DIM_READ = "dimRead";
  static const FEED_SWIPE_R = "feedSwipeR";
  static const FEED_SWIPE_L = "feedSwipeL";
  static const UNREAD_SOURCE_TIP = "unreadSourceTip";

  // Reading preferences
  static const ARTICLE_FONT_SIZE = "articleFontSize";
  static const FONT_FAMILY = "fontFamily";
  static const CUSTOM_FONT_PATH = "customFontPath";

  // Syncing
  static const SYNC_SERVICE = "syncService";
  static const LAST_SYNCED = "lastSynced";
  static const LAST_SYNC_SUCCESS = "lastSyncSuccess";
  static const LAST_ID = "lastId";
  static const ENDPOINT = "endpoint";
  static const USERNAME = "username";
  static const PASSWORD = "password";
  static const API_ID = "apiId";
  static const API_KEY = "apiKey";
  static const FETCH_LIMIT = "fetchLimit";
  static const FEVER_INT_32 = "feverInt32";
  static const LAST_FETCHED = "lastFetched";
  static const AUTH = "auth";
  static const USE_INT_64 = "useInt64";
  static const INOREADER_REMOVE_AD = "inoRemoveAd";

  // Default open target
  static const GLOBAL_OPEN_TARGET = "globalOpenTarget";
  static const GROUP_OPEN_TARGETS = "groupOpenTargets";
}

class Store {
  // Initialized in main.dart
  static late SharedPreferences sp;

  static Locale? getLocale() {
    if (!sp.containsKey(StoreKeys.LOCALE)) return null;
    var localeString = sp.getString(StoreKeys.LOCALE);
    if (localeString == null) return null;
    var splitted = localeString.split('_');
    if (splitted.length > 1) {
      return Locale(splitted[0], splitted[1]);
    } else {
      return Locale(localeString);
    }
  }

  static void setLocale(Locale? locale) {
    if (locale == null) sp.remove(StoreKeys.LOCALE);
    else sp.setString(StoreKeys.LOCALE, locale.toString());
  }

  static ThemeSetting getTheme() {
    return sp.containsKey(StoreKeys.THEME)
    ? ThemeSetting.values[sp.getInt(StoreKeys.THEME)!]
    : ThemeSetting.Default;
  }

  static void setTheme(ThemeSetting theme) {
    sp.setInt(StoreKeys.THEME, theme.index);
  }

  static Map<String, List<String>> getGroups() {
    var groups = sp.getString(StoreKeys.GROUPS);
    if (groups == null) return Map();
    Map<String, List<String>> result = Map();
    var parsed = jsonDecode(groups);
    for (var key in parsed.keys) {
      result[key] = List.castFrom(parsed[key]);
    }
    return result;
  }

  static void setGroups(Map<String, List<String>> groups) {
    sp.setString(StoreKeys.GROUPS, jsonEncode(groups));
  }

  static List<String>? getUncategorized() {
    final stored = sp.getString(StoreKeys.UNCATEGORIZED);
    if (stored == null) return null;
    final parsed = jsonDecode(stored);
    return List.castFrom(parsed);
  }

  static void setUncategorized(List<String>? value) {
    if (value == null) {
      sp.remove(StoreKeys.UNCATEGORIZED);
    } else {
      sp.setString(StoreKeys.UNCATEGORIZED, jsonEncode(value));
    }
  }

  static int getArticleFontSize() {
    return sp.getInt(StoreKeys.ARTICLE_FONT_SIZE) ?? 16;
  }

  static void setArticleFontSize(int value) {
    sp.setInt(StoreKeys.ARTICLE_FONT_SIZE, value);
  }

  static String getErrorLog() {
    return sp.getString(StoreKeys.ERROR_LOG) ?? "";
  }

  static void setErrorLog(String value) {
    sp.setString(StoreKeys.ERROR_LOG, value);
  }

  static String getFontFamily() {
    return sp.getString(StoreKeys.FONT_FAMILY) ?? "System";
  }

  static void setFontFamily(String value) {
    sp.setString(StoreKeys.FONT_FAMILY, value);
  }

  static String? getCustomFontPath() {
    return sp.getString(StoreKeys.CUSTOM_FONT_PATH);
  }

  static void setCustomFontPath(String? value) {
    if (value == null) {
      sp.remove(StoreKeys.CUSTOM_FONT_PATH);
    } else {
      sp.setString(StoreKeys.CUSTOM_FONT_PATH, value);
    }
  }

  static SourceOpenTarget getGlobalOpenTarget() {
    var idx = sp.getInt(StoreKeys.GLOBAL_OPEN_TARGET);
    if (idx == null || idx >= SourceOpenTarget.values.length) {
      return SourceOpenTarget.Local;
    }
    return SourceOpenTarget.values[idx];
  }

  static void setGlobalOpenTarget(SourceOpenTarget target) {
    sp.setInt(StoreKeys.GLOBAL_OPEN_TARGET, target.index);
  }

  static Map<String, int> getGroupOpenTargets() {
    var stored = sp.getString(StoreKeys.GROUP_OPEN_TARGETS);
    if (stored == null) return {};
    Map<String, int> result = {};
    var parsed = jsonDecode(stored);
    for (var key in parsed.keys) {
      result[key] = parsed[key] as int;
    }
    return result;
  }

  static void setGroupOpenTargets(Map<String, int> targets) {
    sp.setString(StoreKeys.GROUP_OPEN_TARGETS, jsonEncode(targets));
  }
}