2020-02-16 09:25:53 +01:00
|
|
|
import 'package:flutter/foundation.dart';
|
2020-02-22 13:25:06 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2020-03-19 20:58:30 +01:00
|
|
|
import 'package:tsacdop/class/podcastlocal.dart';
|
|
|
|
import 'package:workmanager/workmanager.dart';
|
2020-02-16 09:25:53 +01:00
|
|
|
|
2020-03-19 20:58:30 +01:00
|
|
|
import 'package:tsacdop/local_storage/sqflite_localpodcast.dart';
|
2020-02-22 13:25:06 +01:00
|
|
|
import 'package:tsacdop/local_storage/key_value_storage.dart';
|
|
|
|
|
2020-03-19 20:58:30 +01:00
|
|
|
void callbackDispatcher() {
|
|
|
|
Workmanager.executeTask((task, inputData) async {
|
|
|
|
var dbHelper = DBHelper();
|
|
|
|
List<PodcastLocal> podcastList = await dbHelper.getPodcastLocalAll();
|
|
|
|
await Future.forEach(podcastList, (podcastLocal) async {
|
2020-04-06 14:18:08 +02:00
|
|
|
await dbHelper.updatePodcastRss(podcastLocal);
|
2020-03-19 20:58:30 +01:00
|
|
|
print('Refresh ' + podcastLocal.title);
|
|
|
|
});
|
|
|
|
KeyValueStorage refreshstorage = KeyValueStorage('refreshdate');
|
|
|
|
await refreshstorage.saveInt(DateTime.now().millisecondsSinceEpoch);
|
|
|
|
return Future.value(true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-22 13:25:06 +01:00
|
|
|
class SettingState extends ChangeNotifier {
|
2020-03-31 18:36:20 +02:00
|
|
|
KeyValueStorage themeStorage = KeyValueStorage('themes');
|
|
|
|
KeyValueStorage accentStorage = KeyValueStorage('accents');
|
|
|
|
KeyValueStorage autoupdateStorage = KeyValueStorage('autoupdate');
|
|
|
|
KeyValueStorage intervalStorage = KeyValueStorage('updateInterval');
|
|
|
|
KeyValueStorage downloadUsingDataStorage =
|
|
|
|
KeyValueStorage('downloadUsingData');
|
2020-04-06 14:18:08 +02:00
|
|
|
KeyValueStorage introStorage = KeyValueStorage('intro');
|
2020-02-22 13:25:06 +01:00
|
|
|
|
2020-03-01 13:17:06 +01:00
|
|
|
Future initData() async {
|
|
|
|
await _getTheme();
|
|
|
|
await _getAccentSetColor();
|
2020-04-06 14:18:08 +02:00
|
|
|
await _getShowIntro();
|
2020-03-01 13:17:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ThemeMode _theme;
|
|
|
|
ThemeMode get theme => _theme;
|
2020-03-14 04:14:24 +01:00
|
|
|
|
2020-03-01 13:17:06 +01:00
|
|
|
set setTheme(ThemeMode mode) {
|
|
|
|
_theme = mode;
|
|
|
|
_saveTheme();
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2020-03-19 20:58:30 +01:00
|
|
|
void setWorkManager(int hour) {
|
|
|
|
_updateInterval = hour;
|
|
|
|
notifyListeners();
|
|
|
|
_saveUpdateInterval();
|
|
|
|
Workmanager.initialize(
|
|
|
|
callbackDispatcher,
|
2020-03-31 18:36:20 +02:00
|
|
|
isInDebugMode: false,
|
2020-03-19 20:58:30 +01:00
|
|
|
);
|
|
|
|
Workmanager.registerPeriodicTask("1", "update_podcasts",
|
|
|
|
frequency: Duration(hours: hour),
|
|
|
|
initialDelay: Duration(seconds: 10),
|
|
|
|
constraints: Constraints(
|
|
|
|
networkType: NetworkType.connected,
|
|
|
|
));
|
|
|
|
print('work manager init done + ');
|
|
|
|
}
|
|
|
|
|
2020-04-06 14:18:08 +02:00
|
|
|
Future cancelWork() async {
|
2020-03-31 18:36:20 +02:00
|
|
|
await Workmanager.cancelAll();
|
2020-03-19 20:58:30 +01:00
|
|
|
print('work job cancelled');
|
|
|
|
}
|
|
|
|
|
2020-03-01 13:17:06 +01:00
|
|
|
Color _accentSetColor;
|
|
|
|
Color get accentSetColor => _accentSetColor;
|
|
|
|
|
|
|
|
set setAccentColor(Color color) {
|
|
|
|
_accentSetColor = color;
|
|
|
|
_saveAccentSetColor();
|
2020-02-16 09:25:53 +01:00
|
|
|
notifyListeners();
|
2020-02-22 13:25:06 +01:00
|
|
|
}
|
|
|
|
|
2020-03-19 20:58:30 +01:00
|
|
|
int _updateInterval;
|
|
|
|
int get updateInterval => _updateInterval;
|
|
|
|
|
|
|
|
int _initUpdateTag;
|
2020-03-14 04:14:24 +01:00
|
|
|
bool _autoUpdate;
|
|
|
|
bool get autoUpdate => _autoUpdate;
|
|
|
|
set autoUpdate(bool boo) {
|
|
|
|
_autoUpdate = boo;
|
|
|
|
_saveAutoUpdate();
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2020-03-31 18:36:20 +02:00
|
|
|
bool _downloadUsingData;
|
|
|
|
bool get downloadUsingData => _downloadUsingData;
|
|
|
|
set downloadUsingData(bool boo) {
|
|
|
|
_downloadUsingData = boo;
|
|
|
|
_saveDownloadUsingData();
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2020-04-06 14:18:08 +02:00
|
|
|
bool _showIntro;
|
|
|
|
bool get showIntro => _showIntro;
|
|
|
|
|
2020-02-22 13:25:06 +01:00
|
|
|
@override
|
|
|
|
void addListener(VoidCallback listener) {
|
|
|
|
super.addListener(listener);
|
|
|
|
_getTheme();
|
2020-03-01 13:17:06 +01:00
|
|
|
_getAccentSetColor();
|
2020-03-14 04:14:24 +01:00
|
|
|
_getAutoUpdate();
|
2020-03-31 18:36:20 +02:00
|
|
|
_getDownloadUsingData();
|
2020-03-19 20:58:30 +01:00
|
|
|
_getUpdateInterval().then((value) {
|
|
|
|
if (_initUpdateTag == 0) setWorkManager(24);
|
|
|
|
});
|
2020-02-16 09:25:53 +01:00
|
|
|
}
|
|
|
|
|
2020-03-19 20:58:30 +01:00
|
|
|
Future _getTheme() async {
|
2020-03-31 18:36:20 +02:00
|
|
|
int mode = await themeStorage.getInt();
|
2020-03-01 13:17:06 +01:00
|
|
|
_theme = ThemeMode.values[mode];
|
|
|
|
}
|
|
|
|
|
2020-03-19 20:58:30 +01:00
|
|
|
Future _saveTheme() async {
|
2020-03-31 18:36:20 +02:00
|
|
|
await themeStorage.saveInt(_theme.index);
|
2020-03-01 13:17:06 +01:00
|
|
|
}
|
|
|
|
|
2020-03-19 20:58:30 +01:00
|
|
|
Future _getAccentSetColor() async {
|
2020-03-31 18:36:20 +02:00
|
|
|
String colorString = await accentStorage.getString();
|
2020-03-01 13:17:06 +01:00
|
|
|
if (colorString.isNotEmpty) {
|
|
|
|
int color = int.parse('FF' + colorString.toUpperCase(), radix: 16);
|
|
|
|
_accentSetColor = Color(color).withOpacity(1.0);
|
|
|
|
} else {
|
2020-04-06 14:18:08 +02:00
|
|
|
_accentSetColor = Color.fromRGBO(35, 204, 198, 1);
|
2020-03-01 13:17:06 +01:00
|
|
|
}
|
2020-02-22 13:25:06 +01:00
|
|
|
}
|
|
|
|
|
2020-03-19 20:58:30 +01:00
|
|
|
Future _saveAccentSetColor() async {
|
2020-03-31 18:36:20 +02:00
|
|
|
await accentStorage
|
2020-03-01 13:17:06 +01:00
|
|
|
.saveString(_accentSetColor.toString().substring(10, 16));
|
2020-03-14 04:14:24 +01:00
|
|
|
}
|
|
|
|
|
2020-03-19 20:58:30 +01:00
|
|
|
Future _getAutoUpdate() async {
|
2020-03-31 18:36:20 +02:00
|
|
|
int i = await autoupdateStorage.getInt();
|
2020-03-19 20:58:30 +01:00
|
|
|
_autoUpdate = i == 0 ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future _saveAutoUpdate() async {
|
2020-03-31 18:36:20 +02:00
|
|
|
await autoupdateStorage.saveInt(_autoUpdate ? 0 : 1);
|
2020-03-19 20:58:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Future _getUpdateInterval() async {
|
2020-03-31 18:36:20 +02:00
|
|
|
_initUpdateTag = await intervalStorage.getInt();
|
|
|
|
_updateInterval = _initUpdateTag;
|
2020-03-14 04:14:24 +01:00
|
|
|
}
|
|
|
|
|
2020-03-19 20:58:30 +01:00
|
|
|
Future _saveUpdateInterval() async {
|
2020-03-31 18:36:20 +02:00
|
|
|
await intervalStorage.saveInt(_updateInterval);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future _getDownloadUsingData() async {
|
|
|
|
int i = await downloadUsingDataStorage.getInt();
|
|
|
|
_downloadUsingData = i == 0 ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future _saveDownloadUsingData() async {
|
|
|
|
await downloadUsingDataStorage.saveInt(_downloadUsingData ? 0 : 1);
|
2020-02-22 13:25:06 +01:00
|
|
|
}
|
2020-04-06 14:18:08 +02:00
|
|
|
|
|
|
|
Future _getShowIntro() async {
|
|
|
|
int i = await introStorage.getInt();
|
|
|
|
_showIntro = i == 0 ? true : false;
|
|
|
|
}
|
|
|
|
Future saveShowIntro() async{
|
|
|
|
await introStorage.saveInt(1);
|
|
|
|
}
|
2020-02-22 13:25:06 +01:00
|
|
|
}
|