Files
2026-02-13 15:53:22 +01:00

99 lines
3.1 KiB
Dart

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<void> setBool(String key, bool value) async {
var prefs = await SharedPreferences.getInstance();
prefs.setBool(key, value);
}
static Future<bool> getBool(String key, bool defaultValue) async {
var prefs = await SharedPreferences.getInstance();
return prefs.getBool(key) ?? defaultValue;
}
/*static Future<bool> getRemoteBool(String key) async {
var prefs = await SharedPreferences.getInstance();
return prefs.getBool(key) ?? await RemoteConfigConnector.getBool(key);
}*/
static Future<void> setInt(String key, int value) async {
var prefs = await SharedPreferences.getInstance();
prefs.setInt(key, value);
}
static Future<int> getInt(String key, int defaultValue) async {
var prefs = await SharedPreferences.getInstance();
return prefs.getInt(key) ?? defaultValue;
}
/*static Future<int> getRemoteInt(String key) async {
var prefs = await SharedPreferences.getInstance();
return prefs.getInt(key) ?? await RemoteConfigConnector.getInt(key);
}*/
}
class AssetConnector {
static Future<String> getAssetString(String path) => rootBundle.loadString(path);
static getJson<JsonEncodable>(asset) async {
final jsonString = await AssetConnector.getAssetString(asset);
return jsonDecode(jsonString);
}
}
class DataConnector {
static Future<Directory> directory = getApplicationDocumentsDirectory();
static Future<void> saveJsonEncodable(key, List<JsonEncodable> 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<JsonEncodable> getJsonEncodable<JsonEncodable>(key, JsonEncodable defaultValue, JsonEncodable Function(Map<String, dynamic>) fromJson) async {
Map<String, dynamic>? jsonObject = await getJson(key);
if (jsonObject != null) {
return fromJson(jsonObject);
} else {
return defaultValue;
}
}
static Future<List<JsonEncodable>> getJsonEncodableList<JsonEncodable>(key, List<JsonEncodable> defaultValue, JsonEncodable Function(Map<String, dynamic>) fromJson) async {
List<dynamic>? jsonObject = await getJson(key);
if (jsonObject != null) {
return jsonObject.map((e) => fromJson(e)).toList().cast<JsonEncodable>();
} else {
return defaultValue;
}
}
static Future<File?> 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<String, dynamic> toJson();
}