Add getBool/saveBool methods in key_value_storage.

Change default layout to layout.two.
This commit is contained in:
stonegate 2020-07-16 15:18:45 +08:00
parent 8cca497d63
commit c5f2e4bdc1
7 changed files with 104 additions and 54 deletions

View File

@ -673,7 +673,7 @@ class _RecentUpdateState extends State<_RecentUpdate>
with AutomaticKeepAliveClientMixin, SingleTickerProviderStateMixin {
Future<List<EpisodeBrief>> _getRssItem(int top, List<String> group) async {
KeyValueStorage storage = KeyValueStorage(recentLayoutKey);
int index = await storage.getInt();
int index = await storage.getInt(defaultValue: 1);
if (_layout == null) _layout = Layout.values[index];
var dbHelper = DBHelper();
@ -695,6 +695,7 @@ class _RecentUpdateState extends State<_RecentUpdate>
return episodes.length;
}
/// Load more episodes.
_loadMoreEpisode() async {
if (mounted) setState(() => _loadMore = true);
await Future.delayed(Duration(seconds: 3));
@ -705,8 +706,13 @@ class _RecentUpdateState extends State<_RecentUpdate>
});
}
/// Episodes loaded first time.
int _top = 99;
/// Load more episodes when scroll to bottom.
bool _loadMore;
/// For group fliter.
String _groupName;
List<String> _group;
Layout _layout;
@ -1023,7 +1029,7 @@ class _MyFavoriteState extends State<_MyFavorite>
with AutomaticKeepAliveClientMixin {
Future<List<EpisodeBrief>> _getLikedRssItem(int top, int sortBy) async {
KeyValueStorage storage = KeyValueStorage(favLayoutKey);
int index = await storage.getInt();
int index = await storage.getInt(defaultValue: 1);
if (_layout == null) _layout = Layout.values[index];
var dbHelper = DBHelper();
List<EpisodeBrief> episodes = await dbHelper.getLikedRssItem(top, sortBy);
@ -1248,7 +1254,7 @@ class _MyDownloadState extends State<_MyDownload>
Layout _layout;
_getLayout() async {
KeyValueStorage keyValueStorage = KeyValueStorage(downloadLayoutKey);
int layout = await keyValueStorage.getInt();
int layout = await keyValueStorage.getInt(defaultValue: 1);
if (_layout == null)
setState(() {
_layout = Layout.values[layout];

View File

@ -1,6 +1,7 @@
import 'dart:async';
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../state/podcast_group.dart';
@ -111,4 +112,22 @@ class KeyValueStorage {
List<String> list = prefs.getStringList(key);
return list.map((e) => int.parse(e)).toList();
}
/// Rreverse is used for compatite bool value save before which set true = 0, false = 1
Future<bool> getBool(
{@required bool defaultValue, bool reverse = false}) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
if (prefs.getInt(key) == null)
reverse
? await prefs.setInt(key, defaultValue ? 0 : 1)
: await prefs.setInt(key, defaultValue ? 1 : 0);
int i = prefs.getInt(key);
return reverse ? i == 0 : i == 1;
}
/// Rreverse is used for compatite bool value save before which set true = 0, false = 1
saveBool(bool boo, {bool reverse = false}) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
reverse ? prefs.setInt(key, boo ? 0 : 1) : prefs.setInt(key, boo ? 1 : 0);
}
}

View File

@ -39,10 +39,19 @@ class PodcastDetail extends StatefulWidget {
class _PodcastDetailState extends State<PodcastDetail> {
final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
GlobalKey<RefreshIndicatorState>();
/// Fireside background if hosted on fireside.
String _backgroundImage;
/// Fireside hosts if hosted on fireside.
List<PodcastHost> _hosts;
/// Episodes total count.
int _episodeCount;
/// Default layout.
Layout _layout;
bool _scroll;
Future _updateRssItem(BuildContext context, PodcastLocal podcastLocal) async {
var dbHelper = DBHelper();
@ -95,7 +104,7 @@ class _PodcastDetailState extends State<PodcastDetail> {
var dbHelper = DBHelper();
_episodeCount = await dbHelper.getPodcastCounts(podcastLocal.id);
KeyValueStorage storage = KeyValueStorage(podcastLayoutKey);
int index = await storage.getInt();
int index = await storage.getInt(defaultValue: 1);
if (_layout == null) _layout = Layout.values[index];
List<EpisodeBrief> episodes =
await dbHelper.getRssItem(podcastLocal.id, i, reverse);
@ -252,8 +261,14 @@ class _PodcastDetailState extends State<PodcastDetail> {
double _topHeight = 0;
ScrollController _controller;
/// Episodes num load first time.
int _top;
/// Load more episodes when scroll to bottom.
bool _loadMore;
/// Change sort by.
bool _reverse;
@override
void initState() {

View File

@ -24,8 +24,8 @@ class _PopupMenuSettingState extends State<PopupMenuSetting> {
Future<bool> _getTapToOpenPopupMenu() async {
KeyValueStorage tapToOpenPopupMenuStorage =
KeyValueStorage(tapToOpenPopupMenuKey);
int boo = await tapToOpenPopupMenuStorage.getInt(defaultValue: 0);
return boo == 1;
bool boo = await tapToOpenPopupMenuStorage.getBool(defaultValue: false);
return boo;
}
_saveEpisodeMene(List<int> list) async {
@ -37,7 +37,7 @@ class _PopupMenuSettingState extends State<PopupMenuSetting> {
_saveTapToOpenPopupMenu(bool boo) async {
KeyValueStorage tapToOpenPopupMenuStorage =
KeyValueStorage(tapToOpenPopupMenuKey);
await tapToOpenPopupMenuStorage.saveInt(boo ? 1 : 0);
await tapToOpenPopupMenuStorage.saveBool(boo);
if (mounted) setState(() {});
}

View File

@ -36,8 +36,8 @@ class _StorageSettingState extends State<StorageSetting>
Future<bool> _getAutoDownloadNetwork() async {
KeyValueStorage storage = KeyValueStorage(autoDownloadNetworkKey);
int value = await storage.getInt();
return value != 0;
bool value = await storage.getBool(defaultValue: false);
return value;
}
Future<int> _getAutoDeleteDays() async {
@ -58,7 +58,7 @@ class _StorageSettingState extends State<StorageSetting>
_setAudtDownloadNetwork(bool boo) async {
KeyValueStorage storage = KeyValueStorage(autoDownloadNetworkKey);
await storage.saveInt(boo ? 1 : 0);
await storage.saveBool(boo);
}
double _value;

View File

@ -112,6 +112,7 @@ class SettingState extends ChangeNotifier {
await _getRealDark();
}
/// Spp thememode. default auto.
ThemeMode _theme;
ThemeMode get theme => _theme;
@ -156,6 +157,8 @@ class SettingState extends ChangeNotifier {
int get updateInterval => _updateInterval;
int _initUpdateTag;
/// Auto syncing podcasts in background, default true.
bool _autoUpdate;
bool get autoUpdate => _autoUpdate;
set autoUpdate(bool boo) {
@ -164,6 +167,7 @@ class SettingState extends ChangeNotifier {
notifyListeners();
}
/// Confirem before using data to download episode, default true(reverse).
bool _downloadUsingData;
bool get downloadUsingData => _downloadUsingData;
set downloadUsingData(bool boo) {
@ -176,6 +180,7 @@ class SettingState extends ChangeNotifier {
bool _showIntro;
bool get showIntro => _showIntro;
/// Real dark theme, default false.
bool _realDark;
bool get realDark => _realDark;
set setRealDark(bool boo) {
@ -200,6 +205,7 @@ class SettingState extends ChangeNotifier {
_saveAutoPlay();
}
/// Auto start sleep timer at night. Defualt false.
bool _autoSleepTimer;
bool get autoSleepTimer => _autoSleepTimer;
set setAutoSleepTimer(bool boo) {
@ -268,8 +274,8 @@ class SettingState extends ChangeNotifier {
}
Future _getAutoUpdate() async {
int i = await autoupdateStorage.getInt();
_autoUpdate = i == 0 ? true : false;
_autoUpdate =
await autoupdateStorage.getBool(defaultValue: true, reverse: true);
}
Future _getUpdateInterval() async {
@ -278,12 +284,12 @@ class SettingState extends ChangeNotifier {
}
Future _getDownloadUsingData() async {
int i = await downloadUsingDataStorage.getInt();
_downloadUsingData = i == 0 ? true : false;
_downloadUsingData = await downloadUsingDataStorage.getBool(
defaultValue: true, reverse: true);
}
Future _saveDownloadUsingData() async {
await downloadUsingDataStorage.saveInt(_downloadUsingData ? 0 : 1);
await downloadUsingDataStorage.saveBool(_downloadUsingData, reverse: true);
}
Future _getShowIntro() async {
@ -292,21 +298,19 @@ class SettingState extends ChangeNotifier {
}
Future _getRealDark() async {
int i = await realDarkStorage.getInt();
_realDark = i == 0 ? false : true;
_realDark = await realDarkStorage.getBool(defaultValue: false);
}
Future _getSleepTimerData() async {
_defaultSleepTimer =
await defaultSleepTimerStorage.getInt(defaultValue: 30);
int i = await autoSleepTimerStorage.getInt();
_autoSleepTimer = i == 1;
_autoSleepTimer = await autoSleepTimerStorage.getBool(defaultValue: false);
_autoSleepTimerStart =
await autoSleepTimerStartStorage.getInt(defaultValue: 1380);
_autoSleepTimerEnd =
await autoSleepTimerEndStorage.getInt(defaultValue: 360);
int a = await autoPlayStorage.getInt();
_autoPlay = a == 0;
_autoPlay =
await autoPlayStorage.getBool(defaultValue: true, reverse: true);
_autoSleepTimerMode = await autoSleepTimerModeStorage.getInt();
}
@ -316,7 +320,7 @@ class SettingState extends ChangeNotifier {
}
Future _setRealDark() async {
await realDarkStorage.saveInt(_realDark ? 1 : 0);
await realDarkStorage.saveBool(_realDark);
}
Future saveShowIntro(int i) async {
@ -332,11 +336,11 @@ class SettingState extends ChangeNotifier {
}
Future _saveAutoUpdate() async {
await autoupdateStorage.saveInt(_autoUpdate ? 0 : 1);
await autoupdateStorage.saveBool(_autoUpdate, reverse: true);
}
Future _saveAutoPlay() async {
await autoPlayStorage.saveInt(_autoPlay ? 0 : 1);
await autoPlayStorage.saveBool(_autoPlay, reverse: true);
}
Future _setDefaultSleepTimer() async {
@ -344,7 +348,7 @@ class SettingState extends ChangeNotifier {
}
Future _saveAutoSleepTimer() async {
await autoSleepTimerStorage.saveInt(_autoSleepTimer ? 1 : 0);
await autoSleepTimerStorage.saveBool(_autoSleepTimer);
}
Future _saveAutoSleepTimerMode() async {
@ -362,27 +366,32 @@ class SettingState extends ChangeNotifier {
Future<SettingsBackup> backup() async {
int theme = await themeStorage.getInt();
String accentColor = await accentStorage.getString();
int realDark = await realDarkStorage.getInt();
int autoPlay = await autoPlayStorage.getInt();
int autoUpdate = await autoupdateStorage.getInt();
bool realDark = await realDarkStorage.getBool(defaultValue: false);
bool autoPlay =
await autoPlayStorage.getBool(defaultValue: true, reverse: true);
bool autoUpdate =
await autoupdateStorage.getBool(defaultValue: true, reverse: true);
int updateInterval = await intervalStorage.getInt();
int downloadUsingData = await downloadUsingDataStorage.getInt();
bool downloadUsingData = await downloadUsingDataStorage.getBool(
defaultValue: true, reverse: true);
int cacheMax = await cacheStorage.getInt(defaultValue: 500 * 1024 * 1024);
int podcastLayout = await podcastLayoutStorage.getInt();
int recentLayout = await recentLayoutStorage.getInt();
int favLayout = await favLayoutStorage.getInt();
int downloadLayout = await downloadLayoutStorage.getInt();
int autoDownloadNetwork = await autoDownloadStorage.getInt();
bool autoDownloadNetwork =
await autoDownloadStorage.getBool(defaultValue: false);
List<String> episodePopupMenu =
await KeyValueStorage(episodePopupMenuKey).getStringList();
int autoDelete = await autoDeleteStorage.getInt();
int autoSleepTimer = await autoSleepTimerStorage.getInt();
bool autoSleepTimer =
await autoSleepTimerStorage.getBool(defaultValue: false);
int autoSleepTimerStart = await autoSleepTimerStartStorage.getInt();
int autoSleepTimerEnd = await autoSleepTimerEndStorage.getInt();
int autoSleepTimerMode = await autoSleepTimerModeStorage.getInt();
int defaultSleepTime = await defaultSleepTimerStorage.getInt();
int tapToOpenPopupMenu =
await KeyValueStorage(tapToOpenPopupMenuKey).getInt(defaultValue: 0);
bool tapToOpenPopupMenu = await KeyValueStorage(tapToOpenPopupMenuKey)
.getBool(defaultValue: false);
return SettingsBackup(
theme: theme,
@ -411,27 +420,28 @@ class SettingState extends ChangeNotifier {
Future<void> restore(SettingsBackup backup) async {
await themeStorage.saveInt(backup.theme);
await accentStorage.saveString(backup.accentColor);
await realDarkStorage.saveInt(backup.realDark);
await autoPlayStorage.saveInt(backup.autoPlay);
await autoupdateStorage.saveInt(backup.autoUpdate);
await realDarkStorage.saveBool(backup.realDark);
await autoPlayStorage.saveBool(backup.autoPlay, reverse: true);
await autoupdateStorage.saveBool(backup.autoUpdate, reverse: true);
await intervalStorage.saveInt(backup.updateInterval);
await downloadUsingDataStorage.saveInt(backup.downloadUsingData);
await downloadUsingDataStorage.saveBool(backup.downloadUsingData,
reverse: true);
await cacheStorage.saveInt(backup.cacheMax);
await podcastLayoutStorage.saveInt(backup.podcastLayout);
await recentLayoutStorage.saveInt(backup.recentLayout);
await favLayoutStorage.saveInt(backup.favLayout);
await downloadLayoutStorage.saveInt(backup.downloadLayout);
await autoDownloadStorage.saveInt(backup.autoDownloadNetwork);
await autoDownloadStorage.saveBool(backup.autoDownloadNetwork);
await KeyValueStorage(episodePopupMenuKey)
.saveStringList(backup.episodePopupMenu);
await autoDeleteStorage.saveInt(backup.autoDelete);
await autoSleepTimerStorage.saveInt(backup.autoSleepTimer);
await autoSleepTimerStorage.saveBool(backup.autoSleepTimer);
await autoSleepTimerStartStorage.saveInt(backup.autoSleepTimerStart);
await autoSleepTimerEndStorage.saveInt(backup.autoSleepTimerEnd);
await autoSleepTimerModeStorage.saveInt(backup.autoSleepTimerMode);
await defaultSleepTimerStorage.saveInt(backup.defaultSleepTime);
await KeyValueStorage(tapToOpenPopupMenuKey)
.saveInt(backup.tapToOpenPopupMenu);
.saveBool(backup.tapToOpenPopupMenu);
await initData();
await _getAutoUpdate();
await _getDownloadUsingData();

View File

@ -1,25 +1,25 @@
class SettingsBackup {
final int theme;
final String accentColor;
final int realDark;
final int autoPlay;
final int autoUpdate;
final bool realDark;
final bool autoPlay;
final bool autoUpdate;
final int updateInterval;
final int downloadUsingData;
final bool downloadUsingData;
final int cacheMax;
final int podcastLayout;
final int recentLayout;
final int favLayout;
final int downloadLayout;
final int autoDownloadNetwork;
final bool autoDownloadNetwork;
final List<String> episodePopupMenu;
final int autoDelete;
final int autoSleepTimer;
final bool autoSleepTimer;
final int autoSleepTimerStart;
final int autoSleepTimerEnd;
final int defaultSleepTime;
final int autoSleepTimerMode;
final int tapToOpenPopupMenu;
final bool tapToOpenPopupMenu;
SettingsBackup(
{this.theme,
this.accentColor,
@ -73,23 +73,23 @@ class SettingsBackup {
return SettingsBackup(
theme: json['theme'] as int,
accentColor: json['accentColor'] as String,
realDark: json['realDark'] as int,
autoPlay: json['autoPlay'] as int,
autoUpdate: json['autoUpdate'] as int,
realDark: json['realDark'] as bool,
autoPlay: json['autoPlay'] as bool,
autoUpdate: json['autoUpdate'] as bool,
updateInterval: json['updateInterval'] as int,
downloadUsingData: json['downloadUsingData'] as int,
downloadUsingData: json['downloadUsingData'] as bool,
cacheMax: json['cacheMax'] as int,
podcastLayout: json['podcastLayout'] as int,
recentLayout: json['recentLayout'] as int,
favLayout: json['favLayout'] as int,
downloadLayout: json['downloadLayout'] as int,
autoDownloadNetwork: json['autoDownloadNetwork'] as int,
autoDownloadNetwork: json['autoDownloadNetwork'] as bool,
episodePopupMenu: list,
autoDelete: json['autoDelete'] as int,
autoSleepTimer: json['autoSleepTimer'] as int,
autoSleepTimer: json['autoSleepTimer'] as bool,
autoSleepTimerStart: json['autoSleepeTimerStart'] as int,
autoSleepTimerEnd: json['autoSleepTimerEnd'] as int,
autoSleepTimerMode: json['autoSleepTimerMode'] as int,
tapToOpenPopupMenu: json['tapToOpenPopupMenu'] as int);
tapToOpenPopupMenu: json['tapToOpenPopupMenu'] as bool);
}
}