From f47758b10407548adf72d0e757dab6104d9fff5c Mon Sep 17 00:00:00 2001 From: stonegate Date: Wed, 15 Jul 2020 02:34:28 +0800 Subject: [PATCH] Support settings backuo. --- lib/home/addpodcast.dart | 4 +- lib/home/home.dart | 2 +- .../{importompl.dart => import_ompl.dart} | 0 lib/home/popupmenu.dart | 3 +- lib/local_storage/key_value_storage.dart | 1 - lib/state/setting_state.dart | 108 +++++++++++++----- lib/type/settings_backup.dart | 95 +++++++++++++++ lib/type/sub_history.dart | 10 ++ 8 files changed, 191 insertions(+), 32 deletions(-) rename lib/home/{importompl.dart => import_ompl.dart} (100%) create mode 100644 lib/type/settings_backup.dart diff --git a/lib/home/addpodcast.dart b/lib/home/addpodcast.dart index e5e2fe8..d606e1f 100644 --- a/lib/home/addpodcast.dart +++ b/lib/home/addpodcast.dart @@ -350,8 +350,8 @@ class _SearchResultState extends State var subscribeWorker = Provider.of(context, listen: false); final s = context.s; savePodcast(OnlinePodcast podcast) { - SubscribeItem item = - SubscribeItem(podcast.rss, podcast.title, imgUrl: podcast.image); + SubscribeItem item = SubscribeItem(podcast.rss, podcast.title, + imgUrl: podcast.image, group: 'Home'); subscribeWorker.setSubscribeItem(item); } diff --git a/lib/home/home.dart b/lib/home/home.dart index d0efbb7..70229c0 100644 --- a/lib/home/home.dart +++ b/lib/home/home.dart @@ -21,7 +21,7 @@ import '../util/custompaint.dart'; import '../state/download_state.dart'; import '../state/podcast_group.dart'; import 'playlist.dart'; -import 'importompl.dart'; +import 'import_ompl.dart'; import 'audioplayer.dart'; import 'addpodcast.dart'; import 'popupmenu.dart'; diff --git a/lib/home/importompl.dart b/lib/home/import_ompl.dart similarity index 100% rename from lib/home/importompl.dart rename to lib/home/import_ompl.dart diff --git a/lib/home/popupmenu.dart b/lib/home/popupmenu.dart index 3aeb22b..30f5242 100644 --- a/lib/home/popupmenu.dart +++ b/lib/home/popupmenu.dart @@ -7,7 +7,6 @@ import 'package:provider/provider.dart'; import 'package:tsacdop/local_storage/key_value_storage.dart'; import 'package:tsacdop/service/ompl_build.dart'; import 'package:tsacdop/state/podcast_group.dart'; -import 'package:xml/xml.dart' as xml; import 'package:file_picker/file_picker.dart'; import 'package:flutter/services.dart'; import 'package:fluttertoast/fluttertoast.dart'; @@ -67,7 +66,7 @@ class _PopupMenuState extends State { SubscribeItem item = SubscribeItem(rss.xmlUrl, rss.text, group: title); await subscribeWorker.setSubscribeItem(item); - await Future.delayed(Duration(seconds: 1)); + await Future.delayed(Duration(milliseconds: 500)); print(rss.text); } } diff --git a/lib/local_storage/key_value_storage.dart b/lib/local_storage/key_value_storage.dart index 2a8dab3..8de8f32 100644 --- a/lib/local_storage/key_value_storage.dart +++ b/lib/local_storage/key_value_storage.dart @@ -6,7 +6,6 @@ import 'package:shared_preferences/shared_preferences.dart'; import '../state/podcast_group.dart'; const String autoPlayKey = 'autoPlay'; -//const String autoAddKey = 'autoAdd'; const String audioPositionKey = 'audioposition'; const String lastWorkKey = 'lastWork'; const String refreshdateKey = 'refreshdate'; diff --git a/lib/state/setting_state.dart b/lib/state/setting_state.dart index ba41dc5..fe31a40 100644 --- a/lib/state/setting_state.dart +++ b/lib/state/setting_state.dart @@ -10,6 +10,7 @@ import '../local_storage/sqflite_localpodcast.dart'; import '../local_storage/key_value_storage.dart'; import '../type/podcastlocal.dart'; import '../type/episodebrief.dart'; +import '../type/settings_backup.dart'; import 'download_state.dart'; void callbackDispatcher() { @@ -25,7 +26,6 @@ void callbackDispatcher() { await dbHelper.updatePodcastRss(podcastLocal, removeMark: lastWork); print('Refresh ' + podcastLocal.title); } - ; await FlutterDownloader.initialize(); AutoDownloader downloader = AutoDownloader(); @@ -97,6 +97,13 @@ class SettingState extends ChangeNotifier { KeyValueStorage(autoSleepTimerEndKey); KeyValueStorage tapToOpenPopupMenuStorage = KeyValueStorage(tapToOpenPopupMenuKey); + KeyValueStorage cacheStorage = KeyValueStorage(cacheMaxKey); + KeyValueStorage podcastLayoutStorage = KeyValueStorage(podcastLayoutKey); + KeyValueStorage favLayoutStorage = KeyValueStorage(favLayoutKey); + KeyValueStorage downloadLayoutStorage = KeyValueStorage(downloadLayoutKey); + KeyValueStorage recentLayoutStorage = KeyValueStorage(recentLayoutKey); + KeyValueStorage autoDeleteStorage = KeyValueStorage(autoDeleteKey); + KeyValueStorage autoDownloadStorage = KeyValueStorage(autoDownloadNetworkKey); Future initData() async { await _getTheme(); @@ -249,10 +256,6 @@ class SettingState extends ChangeNotifier { _theme = ThemeMode.values[mode]; } - Future _saveTheme() async { - await themeStorage.saveInt(_theme.index); - } - Future _getAccentSetColor() async { String colorString = await accentStorage.getString(); if (colorString.isNotEmpty) { @@ -263,29 +266,16 @@ class SettingState extends ChangeNotifier { } } - Future _saveAccentSetColor() async { - await accentStorage - .saveString(_accentSetColor.toString().substring(10, 16)); - } - Future _getAutoUpdate() async { int i = await autoupdateStorage.getInt(); _autoUpdate = i == 0 ? true : false; } - Future _saveAutoUpdate() async { - await autoupdateStorage.saveInt(_autoUpdate ? 0 : 1); - } - Future _getUpdateInterval() async { _initUpdateTag = await intervalStorage.getInt(); _updateInterval = _initUpdateTag; } - Future _saveUpdateInterval() async { - await intervalStorage.saveInt(_updateInterval); - } - Future _getDownloadUsingData() async { int i = await downloadUsingDataStorage.getInt(); _downloadUsingData = i == 0 ? true : false; @@ -300,19 +290,11 @@ class SettingState extends ChangeNotifier { _showIntro = _initialShowIntor == 0 ? true : false; } - Future saveShowIntro(int i) async { - await introStorage.saveInt(i); - } - Future _getRealDark() async { int i = await realDarkStorage.getInt(); _realDark = i == 0 ? false : true; } - Future _setRealDark() async { - await realDarkStorage.saveInt(_realDark ? 1 : 0); - } - Future _getSleepTimerData() async { _defaultSleepTimer = await defaultSleepTimerStorage.getInt(defaultValue: 30); @@ -327,6 +309,31 @@ class SettingState extends ChangeNotifier { _autoSleepTimerMode = await autoSleepTimerModeStorage.getInt(); } + Future _saveAccentSetColor() async { + await accentStorage + .saveString(_accentSetColor.toString().substring(10, 16)); + } + + Future _setRealDark() async { + await realDarkStorage.saveInt(_realDark ? 1 : 0); + } + + Future saveShowIntro(int i) async { + await introStorage.saveInt(i); + } + + Future _saveUpdateInterval() async { + await intervalStorage.saveInt(_updateInterval); + } + + Future _saveTheme() async { + await themeStorage.saveInt(_theme.index); + } + + Future _saveAutoUpdate() async { + await autoupdateStorage.saveInt(_autoUpdate ? 0 : 1); + } + Future _saveAutoPlay() async { await autoPlayStorage.saveInt(_autoPlay ? 0 : 1); } @@ -350,4 +357,53 @@ class SettingState extends ChangeNotifier { Future _saveAutoSleepTimerEnd() async { await autoSleepTimerEndStorage.saveInt(_autoSleepTimerEnd); } + + Future 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(); + int updateInterval = await intervalStorage.getInt(); + int downloadUsingData = await downloadUsingDataStorage.getInt(); + int cacheMax = await cacheStorage.getInt(); + 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(); + List episodePopupMenu = + await KeyValueStorage(episodePopupMenuKey).getStringList(); + int autoDelete = await autoDeleteStorage.getInt(); + int autoSleepTimer = await autoSleepTimerStorage.getInt(); + 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); + + return SettingsBackup( + theme: theme, + accentColor: accentColor, + realDark: realDark, + autoPlay: autoPlay, + autoUpdate: autoUpdate, + updateInterval: updateInterval, + downloadUsingData: downloadUsingData, + cacheMax: cacheMax, + podcastLayout: podcastLayout, + recentLayout: recentLayout, + favLayout: favLayout, + downloadLayout: downloadLayout, + autoDownloadNetwork: autoDownloadNetwork, + episodePopupMenu: episodePopupMenu, + autoDelete: autoDelete, + autoSleepTimer: autoSleepTimer, + autoSleepTimerStart: autoSleepTimerStart, + autoSleepTimerEnd: autoSleepTimerEnd, + autoSleepTimerMode: autoSleepTimerMode, + defaultSleepTime: defaultSleepTime, + tapToOpenPopupMenu: tapToOpenPopupMenu); + } } diff --git a/lib/type/settings_backup.dart b/lib/type/settings_backup.dart new file mode 100644 index 0000000..360449e --- /dev/null +++ b/lib/type/settings_backup.dart @@ -0,0 +1,95 @@ +class SettingsBackup { + final int theme; + final String accentColor; + final int realDark; + final int autoPlay; + final int autoUpdate; + final int updateInterval; + final int downloadUsingData; + final int cacheMax; + final int podcastLayout; + final int recentLayout; + final int favLayout; + final int downloadLayout; + final int autoDownloadNetwork; + final List episodePopupMenu; + final int autoDelete; + final int autoSleepTimer; + final int autoSleepTimerStart; + final int autoSleepTimerEnd; + final int defaultSleepTime; + final int autoSleepTimerMode; + final int tapToOpenPopupMenu; + SettingsBackup( + {this.theme, + this.accentColor, + this.realDark, + this.autoPlay, + this.autoUpdate, + this.updateInterval, + this.downloadUsingData, + this.cacheMax, + this.podcastLayout, + this.recentLayout, + this.favLayout, + this.downloadLayout, + this.autoDownloadNetwork, + this.episodePopupMenu, + this.autoDelete, + this.autoSleepTimer, + this.autoSleepTimerStart, + this.autoSleepTimerEnd, + this.defaultSleepTime, + this.autoSleepTimerMode, + this.tapToOpenPopupMenu}); + + Map toJson() { + return { + 'theme': theme, + 'accentColor': accentColor, + 'realDark': realDark, + 'autoPlay': autoPlay, + 'autoUpdate': autoUpdate, + 'updateInterval': updateInterval, + 'downloadUsingData': downloadUsingData, + 'cacheMax': cacheMax, + 'podcastLayout': podcastLayout, + 'recentLayout': recentLayout, + 'favLayout': favLayout, + 'downloadLayout': downloadLayout, + 'autoDownloadNetwork': autoDownloadNetwork, + 'episodePopupMenu': episodePopupMenu, + 'autoDelete': autoDelete, + 'autoSleepTimer': autoSleepTimer, + 'autoSleepTimerStart': autoSleepTimerStart, + 'autoSleepTimerEnd': autoSleepTimerEnd, + 'autoSleepTimerMode': autoSleepTimerMode, + 'tapToOpenPopupMenu': tapToOpenPopupMenu + }; + } + + static SettingsBackup fromJson(Map json) { + List list = List.from(json['episodePopupMenu']); + 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, + updateInterval: json['updateInterval'] as int, + downloadUsingData: json['downloadUsingData'] as int, + 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, + episodePopupMenu: list, + autoDelete: json['autoDelete'] as int, + autoSleepTimer: json['autoSleepTimer'] as int, + autoSleepTimerStart: json['autoSleepeTimerStart'] as int, + autoSleepTimerEnd: json['autoSleepTimerEnd'] as int, + autoSleepTimerMode: json['autoSleepTimerMode'] as int, + tapToOpenPopupMenu: json['tapToOpenPopupMenu'] as int); + } +} diff --git a/lib/type/sub_history.dart b/lib/type/sub_history.dart index 2a0291a..fe69590 100644 --- a/lib/type/sub_history.dart +++ b/lib/type/sub_history.dart @@ -1,8 +1,18 @@ class SubHistory { + /// POdcast subscribe date. DateTime subDate; + + /// Podcast remove date. DateTime delDate; + + /// If podcast still on user device. bool status; + + /// POdcast title. String title; + + /// POdcast rss link. String rssUrl; + SubHistory(this.status, this.delDate, this.subDate, this.rssUrl, this.title); }