tsacdop-podcast-app-android/lib/class/settingstate.dart

159 lines
4.3 KiB
Dart
Raw Normal View History

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:tsacdop/class/podcastlocal.dart';
import 'package:workmanager/workmanager.dart';
import 'package:tsacdop/local_storage/sqflite_localpodcast.dart';
import 'package:tsacdop/local_storage/key_value_storage.dart';
void callbackDispatcher() {
Workmanager.executeTask((task, inputData) async {
var dbHelper = DBHelper();
List<PodcastLocal> podcastList = await dbHelper.getPodcastLocalAll();
await Future.forEach(podcastList, (podcastLocal) async {
2020-03-31 18:36:20 +02:00
await dbHelper.updatePodcastRss(podcastLocal);
print('Refresh ' + podcastLocal.title);
});
KeyValueStorage refreshstorage = KeyValueStorage('refreshdate');
await refreshstorage.saveInt(DateTime.now().millisecondsSinceEpoch);
return Future.value(true);
});
}
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-03-01 13:17:06 +01:00
Future initData() async {
await _getTheme();
await _getAccentSetColor();
}
ThemeMode _theme;
ThemeMode get theme => _theme;
2020-03-01 13:17:06 +01:00
set setTheme(ThemeMode mode) {
_theme = mode;
_saveTheme();
notifyListeners();
}
void setWorkManager(int hour) {
_updateInterval = hour;
notifyListeners();
_saveUpdateInterval();
Workmanager.initialize(
callbackDispatcher,
2020-03-31 18:36:20 +02:00
isInDebugMode: false,
);
Workmanager.registerPeriodicTask("1", "update_podcasts",
frequency: Duration(hours: hour),
initialDelay: Duration(seconds: 10),
constraints: Constraints(
networkType: NetworkType.connected,
));
print('work manager init done + ');
}
2020-03-31 18:36:20 +02:00
Future cancelWork() async{
await Workmanager.cancelAll();
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();
notifyListeners();
}
int _updateInterval;
int get updateInterval => _updateInterval;
int _initUpdateTag;
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();
}
@override
void addListener(VoidCallback listener) {
super.addListener(listener);
_getTheme();
2020-03-01 13:17:06 +01:00
_getAccentSetColor();
_getAutoUpdate();
2020-03-31 18:36:20 +02:00
_getDownloadUsingData();
_getUpdateInterval().then((value) {
if (_initUpdateTag == 0) setWorkManager(24);
});
}
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];
}
Future _saveTheme() async {
2020-03-31 18:36:20 +02:00
await themeStorage.saveInt(_theme.index);
2020-03-01 13:17:06 +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 {
_accentSetColor = Colors.blue[400];
}
}
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));
}
Future _getAutoUpdate() async {
2020-03-31 18:36:20 +02:00
int i = await autoupdateStorage.getInt();
_autoUpdate = i == 0 ? true : false;
}
Future _saveAutoUpdate() async {
2020-03-31 18:36:20 +02:00
await autoupdateStorage.saveInt(_autoUpdate ? 0 : 1);
}
Future _getUpdateInterval() async {
2020-03-31 18:36:20 +02:00
_initUpdateTag = await intervalStorage.getInt();
_updateInterval = _initUpdateTag;
}
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);
}
}