tsacdop-podcast-app-android/lib/state/setting_state.dart

353 lines
10 KiB
Dart
Raw Normal View History

2020-05-10 10:56:04 +02:00
import 'dart:io';
2020-06-12 19:56:13 +02:00
import 'dart:ui';
import 'package:connectivity/connectivity.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:workmanager/workmanager.dart';
2020-06-12 19:56:13 +02:00
import 'package:flutter_downloader/flutter_downloader.dart';
2020-05-06 18:50:32 +02:00
import '../local_storage/sqflite_localpodcast.dart';
import '../local_storage/key_value_storage.dart';
import '../type/podcastlocal.dart';
2020-06-07 20:42:27 +02:00
import '../type/episodebrief.dart';
import 'download_state.dart';
void callbackDispatcher() {
2020-06-07 20:42:27 +02:00
if (Platform.isAndroid)
Workmanager.executeTask((task, inputData) async {
var dbHelper = DBHelper();
List<PodcastLocal> podcastList = await dbHelper.getPodcastLocalAll();
//lastWork is a indicator for if the app was opened since last backgroundwork
//if the app wes opend,then the old marked new episode would be marked not new.
KeyValueStorage lastWorkStorage = KeyValueStorage(lastWorkKey);
int lastWork = await lastWorkStorage.getInt();
await Future.forEach<PodcastLocal>(podcastList, (podcastLocal) async {
2020-06-12 19:56:13 +02:00
await dbHelper.updatePodcastRss(podcastLocal, removeMark: lastWork);
2020-06-07 20:42:27 +02:00
print('Refresh ' + podcastLocal.title);
});
2020-06-12 19:56:13 +02:00
await FlutterDownloader.initialize();
AutoDownloader downloader = AutoDownloader();
2020-06-16 06:40:51 +02:00
2020-06-12 19:56:13 +02:00
KeyValueStorage autoDownloadStorage =
KeyValueStorage(autoDownloadNetworkKey);
int autoDownloadNetwork = await autoDownloadStorage.getInt();
var result = await Connectivity().checkConnectivity();
if (autoDownloadNetwork == 1) {
List<EpisodeBrief> episodes = await dbHelper.getNewEpisodes('all');
// For safety
2020-06-16 06:40:51 +02:00
if (episodes.length < 100 && episodes.length > 0) {
downloader.bindBackgroundIsolate();
2020-06-12 19:56:13 +02:00
await downloader.startTask(episodes);
2020-06-16 06:40:51 +02:00
}
2020-06-12 19:56:13 +02:00
} else if (result == ConnectivityResult.wifi) {
List<EpisodeBrief> episodes = await dbHelper.getNewEpisodes('all');
//For safety
2020-06-16 06:40:51 +02:00
if (episodes.length < 100 && episodes.length > 0) {
downloader.bindBackgroundIsolate();
2020-06-12 19:56:13 +02:00
await downloader.startTask(episodes);
2020-06-16 06:40:51 +02:00
}
2020-06-12 19:56:13 +02:00
}
2020-06-07 20:42:27 +02:00
await lastWorkStorage.saveInt(1);
KeyValueStorage refreshstorage = KeyValueStorage(refreshdateKey);
await refreshstorage.saveInt(DateTime.now().millisecondsSinceEpoch);
return Future.value(true);
});
}
2020-04-29 19:32:45 +02:00
ThemeData lightTheme = ThemeData(
accentColorBrightness: Brightness.dark,
primaryColor: Colors.grey[100],
// accentColor: _accentSetColor,
primaryColorLight: Colors.white,
primaryColorDark: Colors.grey[300],
dialogBackgroundColor: Colors.white,
backgroundColor: Colors.grey[100],
appBarTheme: AppBarTheme(
color: Colors.grey[100],
elevation: 0,
),
textTheme: TextTheme(
bodyText2: TextStyle(fontSize: 15.0, fontWeight: FontWeight.normal),
),
tabBarTheme: TabBarTheme(
labelColor: Colors.black,
unselectedLabelColor: Colors.grey[400],
),
);
class SettingState extends ChangeNotifier {
KeyValueStorage themeStorage = KeyValueStorage(themesKey);
KeyValueStorage accentStorage = KeyValueStorage(accentsKey);
2020-06-27 20:27:39 +02:00
KeyValueStorage autoupdateStorage = KeyValueStorage(autoUpdateKey);
KeyValueStorage intervalStorage = KeyValueStorage(updateIntervalKey);
2020-03-31 18:36:20 +02:00
KeyValueStorage downloadUsingDataStorage =
KeyValueStorage(downloadUsingDataKey);
KeyValueStorage introStorage = KeyValueStorage(introKey);
KeyValueStorage realDarkStorage = KeyValueStorage(realDarkKey);
2020-06-27 20:27:39 +02:00
KeyValueStorage autoPlayStorage = KeyValueStorage(autoPlayKey);
KeyValueStorage defaultSleepTimerStorage =
KeyValueStorage(defaultSleepTimerKey);
KeyValueStorage autoSleepTimerStorage = KeyValueStorage(autoSleepTimerKey);
KeyValueStorage autoSleepTimerModeStorage =
KeyValueStorage(autoSleepTimerModeKey);
KeyValueStorage autoSleepTimerStartStorage =
KeyValueStorage(autoSleepTimerStartKey);
KeyValueStorage autoSleepTimerEndStorage =
KeyValueStorage(autoSleepTimerEndKey);
KeyValueStorage tapToOpenPopupMenuStorage =
KeyValueStorage(tapToOpenPopupMenuKey);
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();
await _getRealDark();
2020-03-01 13:17:06 +01:00
}
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-04-06 14:18:08 +02:00
Future cancelWork() async {
2020-03-31 18:36:20 +02:00
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();
}
2020-06-16 06:40:51 +02:00
int _initialShowIntor;
2020-04-06 14:18:08 +02:00
bool _showIntro;
bool get showIntro => _showIntro;
bool _realDark;
bool get realDark => _realDark;
set setRealDark(bool boo) {
_realDark = boo;
_setRealDark();
notifyListeners();
}
2020-06-27 20:27:39 +02:00
int _defaultSleepTimer;
int get defaultSleepTimer => _defaultSleepTimer;
set setDefaultSleepTimer(int i) {
_defaultSleepTimer = i;
_setDefaultSleepTimer();
notifyListeners();
}
bool _autoPlay;
bool get autoPlay => _autoPlay;
set setAutoPlay(bool boo) {
_autoPlay = boo;
notifyListeners();
_saveAutoPlay();
}
bool _autoSleepTimer;
bool get autoSleepTimer => _autoSleepTimer;
set setAutoSleepTimer(bool boo) {
_autoSleepTimer = boo;
notifyListeners();
_saveAutoSleepTimer();
}
int _autoSleepTimerMode;
int get autoSleepTimerMode => _autoSleepTimerMode;
set setAutoSleepTimerMode(int mode) {
_autoSleepTimerMode = mode;
notifyListeners();
_saveAutoSleepTimerMode();
}
int _autoSleepTimerStart;
int get autoSleepTimerStart => _autoSleepTimerStart;
set setAutoSleepTimerStart(int start) {
_autoSleepTimerStart = start;
notifyListeners();
_saveAutoSleepTimerStart();
}
int _autoSleepTimerEnd;
int get autoSleepTimerEnd => _autoSleepTimerEnd;
set setAutoSleepTimerEnd(int end) {
_autoSleepTimerEnd = end;
notifyListeners();
_saveAutoSleepTimerEnd();
}
@override
void addListener(VoidCallback listener) {
super.addListener(listener);
_getAutoUpdate();
2020-03-31 18:36:20 +02:00
_getDownloadUsingData();
2020-06-27 20:27:39 +02:00
_getSleepTimerData();
2020-06-16 06:40:51 +02:00
_getUpdateInterval().then((value) async {
if (_initUpdateTag == 0)
setWorkManager(24);
//Restart worker if anythin changed in worker callback.
2020-06-27 20:27:39 +02:00
//varsion 2 add auto download new episodes
2020-06-16 06:40:51 +02:00
else if (_autoUpdate && _initialShowIntor == 1) {
await cancelWork();
setWorkManager(_initUpdateTag);
await saveShowIntro(2);
}
});
}
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 {
2020-04-11 19:23:12 +02:00
_accentSetColor = Colors.teal[500];
2020-03-01 13:17:06 +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));
}
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);
}
2020-04-06 14:18:08 +02:00
Future _getShowIntro() async {
2020-06-16 06:40:51 +02:00
_initialShowIntor = await introStorage.getInt();
_showIntro = _initialShowIntor == 0 ? true : false;
2020-04-06 14:18:08 +02:00
}
2020-06-16 06:40:51 +02:00
Future saveShowIntro(int i) async {
await introStorage.saveInt(i);
2020-04-06 14:18:08 +02:00
}
Future _getRealDark() async {
int i = await realDarkStorage.getInt();
_realDark = i == 0 ? false : true;
}
Future _setRealDark() async {
await realDarkStorage.saveInt(_realDark ? 1 : 0);
}
2020-06-27 20:27:39 +02:00
Future _getSleepTimerData() async {
_defaultSleepTimer =
await defaultSleepTimerStorage.getInt(defaultValue: 30);
int i = await autoSleepTimerStorage.getInt();
_autoSleepTimer = i == 1;
_autoSleepTimerStart =
await autoSleepTimerStartStorage.getInt(defaultValue: 1380);
_autoSleepTimerEnd =
await autoSleepTimerEndStorage.getInt(defaultValue: 360);
int a = await autoPlayStorage.getInt();
_autoPlay = a == 0;
_autoSleepTimerMode = await autoSleepTimerModeStorage.getInt();
}
Future _saveAutoPlay() async {
await autoPlayStorage.saveInt(_autoPlay ? 0 : 1);
}
Future _setDefaultSleepTimer() async {
await defaultSleepTimerStorage.saveInt(_defaultSleepTimer);
}
Future _saveAutoSleepTimer() async {
await autoSleepTimerStorage.saveInt(_autoSleepTimer ? 1 : 0);
}
Future _saveAutoSleepTimerMode() async {
await autoSleepTimerModeStorage.saveInt(_autoSleepTimerMode);
}
Future _saveAutoSleepTimerStart() async {
await autoSleepTimerStartStorage.saveInt(_autoSleepTimerStart);
}
Future _saveAutoSleepTimerEnd() async {
await autoSleepTimerEndStorage.saveInt(_autoSleepTimerEnd);
}
}