didvan-app/lib/views/profile/general_settings/settings_state.dart

149 lines
3.7 KiB
Dart

import 'package:didvan/models/enums.dart';
import 'package:didvan/models/home_page_content/home_page_content.dart';
import 'package:didvan/providers/core.dart';
import 'package:didvan/services/network/request.dart';
import 'package:didvan/services/network/request_helper.dart';
import 'package:didvan/services/storage/storage.dart';
class GeneralSettingsState extends CoreProvier {
GeneralSettingsState() {
getSettingsFromStorage();
}
List<int> _notificationTimeRange = [0, 24];
String _fontFamily = 'IranYekan';
double _fontSizeScale = 1;
String _brightness = 'light';
String time = "";
bool showSettings = false;
bool showThemes = false;
bool showContactUs = false;
int unread = 0;
late MainPageContent content;
void markChangeHandler(String type, int id, bool value) {
content.lists
.firstWhere((element) => element.type == type)
.contents
.firstWhere((element) => element.id == id)
.marked = value;
notifyListeners();
}
Future<void> _getMainPageContent() async {
final service = RequestService(RequestHelper.mainPageContent);
await service.httpGet();
if (service.isSuccess) {
content = MainPageContent.fromJson(service.result);
unread = service.result['unread'];
appState = AppState.idle;
return;
}
appState = AppState.failed;
}
void init() {
Future.delayed(Duration.zero, () {
_getMainPageContent();
});
}
Future<void> getTime() async {
appState = AppState.busy;
final service = RequestService(
RequestHelper.notificationTime(),
);
await service.httpGet();
if (service.isSuccess) {
time = service.data('time');
appState = AppState.idle;
} else {
appState = AppState.failed;
}
update();
}
set notificationTimeRange(List<int> value) {
_notificationTimeRange = value;
StorageService.setValue(
key: 'notificationTimeRangeStart',
value: value[0],
);
StorageService.setValue(
key: 'notificationTimeRangeEnd',
value: value[1],
);
notifyListeners();
_setSilenceInterval();
}
List<int> get notificationTimeRange => _notificationTimeRange;
set fontFamily(String value) {
_fontFamily = value;
StorageService.setValue(
key: 'fontFamily',
value: value,
);
notifyListeners();
}
String get fontFamily => _fontFamily;
set fontSizeScale(double value) {
_fontSizeScale = value;
StorageService.setValue(
key: 'fontSizeScale',
value: value,
);
notifyListeners();
}
double get fontSizeScale => _fontSizeScale;
set brightness(String value) {
_brightness = value;
StorageService.setValue(
key: 'brightness',
value: value,
);
notifyListeners();
}
String get brightness => _brightness;
Future<void> _setSilenceInterval() async {
final service = RequestService(RequestHelper.silenceInterval, body: {
'start': notificationTimeRange[0],
'end': notificationTimeRange[1]
});
await service.put();
}
Future<void> getSettingsFromStorage() async {
appState = AppState.busy;
try {
_notificationTimeRange[0] = int.parse(
await StorageService.getValue(key: 'notificationTimeRangeStart'),
);
_notificationTimeRange[1] = int.parse(
await StorageService.getValue(key: 'notificationTimeRangeEnd'),
);
} catch (e) {
notificationTimeRange = [0, 0];
}
_fontFamily = await StorageService.getValue(key: 'fontFamily');
_brightness = await StorageService.getValue(key: 'brightness');
final scale = await StorageService.getValue(key: 'fontSizeScale');
_fontSizeScale = double.parse(scale);
appState = AppState.idle;
}
}