~cytrogen/fluent-reader-mobile

ref: b39dfad9d08734897c1271b30d4ce8decea2bdd3 fluent-reader-mobile/lib/utils/store.dart -rw-r--r-- 3.7 KiB
b39dfad9 — Preckrasno Add Ukrainian (ua) translation (#53) 3 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
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
import 'dart:convert';

import 'package:fluent_reader_lite/models/global_model.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";
 
  // 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";

  // 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";
}

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

  static Locale getLocale() {
    if (!sp.containsKey(StoreKeys.LOCALE)) return null;
    var localeString = sp.getString(StoreKeys.LOCALE);
    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);
  }
}