import 'dart:convert'; import 'dart:io'; import 'package:path_provider/path_provider.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:flutter/services.dart'; class SettingsConnector { static Future setBool(String key, bool value) async { var prefs = await SharedPreferences.getInstance(); prefs.setBool(key, value); } static Future getBool(String key, bool defaultValue) async { var prefs = await SharedPreferences.getInstance(); return prefs.getBool(key) ?? defaultValue; } /*static Future getRemoteBool(String key) async { var prefs = await SharedPreferences.getInstance(); return prefs.getBool(key) ?? await RemoteConfigConnector.getBool(key); }*/ static Future setInt(String key, int value) async { var prefs = await SharedPreferences.getInstance(); prefs.setInt(key, value); } static Future getInt(String key, int defaultValue) async { var prefs = await SharedPreferences.getInstance(); return prefs.getInt(key) ?? defaultValue; } /*static Future getRemoteInt(String key) async { var prefs = await SharedPreferences.getInstance(); return prefs.getInt(key) ?? await RemoteConfigConnector.getInt(key); }*/ } class AssetConnector { static Future getAssetString(String path) => rootBundle.loadString(path); static getJson(asset) async { final jsonString = await AssetConnector.getAssetString(asset); return jsonDecode(jsonString); } } class DataConnector { static Future directory = getApplicationDocumentsDirectory(); static Future saveJsonEncodable(key, List value) async { var jsonString = json.encode(value); var file = await getFile('$key.json', create: true); await file!.writeAsString(jsonString); } static getJson(key) async { var file = await getFile('$key.json'); if (file != null && await file.exists()) { var jsonString = await file.readAsString(); return json.decode(jsonString); } return null; } static Future getJsonEncodable(key, JsonEncodable defaultValue, JsonEncodable Function(Map) fromJson) async { Map? jsonObject = await getJson(key); if (jsonObject != null) { return fromJson(jsonObject); } else { return defaultValue; } } static Future> getJsonEncodableList(key, List defaultValue, JsonEncodable Function(Map) fromJson) async { List? jsonObject = await getJson(key); if (jsonObject != null) { return jsonObject.map((e) => fromJson(e)).toList().cast(); } else { return defaultValue; } } static Future getFile(String fileName, {bool create = false}) async{ String path = (await getApplicationDocumentsDirectory()).path; var file = new File('$path/$fileName'); if (!await file.exists()){ if (!create) return null; await file.create(); } return file; } } mixin JsonEncodable { Map toJson(); }