loris/lib/business_logic/settings.dart

75 lines
2.3 KiB
Dart
Raw Normal View History

2022-07-03 13:47:24 +00:00
import 'package:flutter/painting.dart';
2022-06-30 15:34:09 +00:00
import 'package:shared_preferences/shared_preferences.dart';
2022-07-04 20:39:25 +00:00
import '../business_logic/auth/oauth.dart' as oauth;
2022-06-30 15:34:09 +00:00
2022-08-12 17:00:09 +00:00
class Settings {
late String instanceUrl;
static const String instanceUrlKey = "instance-url";
late String authCode;
static const String authCodeKey = "authcode";
late Locale locale;
static const localeKey = "active-locale";
late String clientSecret;
static const clientSecretKey = "client-secret";
late String clientId;
static const clientIdKey = "client-id";
late String token;
static const tokenKey = "access-token";
late int batchSize;
static const batchSizeKey = "post-batch-size";
late SharedPreferences prefs;
2022-06-30 15:34:09 +00:00
2022-08-12 17:00:09 +00:00
Settings._create();
2022-06-30 15:34:09 +00:00
2022-08-12 17:00:09 +00:00
static Future<Settings> create() async {
Settings settings = Settings._create();
2022-07-01 14:14:13 +00:00
2022-08-12 17:00:09 +00:00
settings.prefs = await SharedPreferences.getInstance();
settings.instanceUrl =
settings.prefs.getString(instanceUrlKey) ?? "example.com";
settings.authCode = settings.prefs.getString(authCodeKey) ?? "";
settings.locale = Locale(settings.prefs.getString(localeKey) ?? "en");
settings.clientSecret = settings.prefs.getString(clientSecretKey) ?? "";
settings.clientId = settings.prefs.getString(clientIdKey) ?? "";
settings.token = settings.prefs.getString(tokenKey) ?? "";
settings.batchSize = settings.prefs.getInt(batchSizeKey) ?? 20;
if (settings.batchSize < 5) {
settings.batchSize = 5;
}
2022-07-01 14:14:13 +00:00
2022-08-12 17:00:09 +00:00
return settings;
2022-07-01 14:14:13 +00:00
}
2022-07-03 13:47:24 +00:00
2022-08-12 17:00:09 +00:00
Future<bool> saveInstanceUrl(String url) async {
instanceUrl = url;
return await prefs.setString(instanceUrlKey, url);
2022-07-03 13:47:24 +00:00
}
2022-07-04 20:39:25 +00:00
2022-08-12 17:00:09 +00:00
Future<bool> saveAuthCode(String code) async {
authCode = code;
return await prefs.setString(authCodeKey, code);
2022-07-04 20:39:25 +00:00
}
2022-08-12 17:00:09 +00:00
Future<bool> saveLocale(String locale) async {
this.locale = Locale(locale);
return await prefs.setString(localeKey, locale);
2022-07-04 20:39:25 +00:00
}
2022-08-12 17:00:09 +00:00
Future<void> saveApp(oauth.App app) async {
clientId = app.clientId;
clientSecret = clientSecret;
prefs.setString(clientSecretKey, app.clientSecret);
prefs.setString(clientIdKey, app.clientId);
2022-07-04 20:39:25 +00:00
}
2022-08-10 16:47:34 +00:00
2022-08-12 17:00:09 +00:00
Future<bool> saveToken(String token) async {
this.token = token;
return await prefs.setString(tokenKey, token);
}
2022-08-10 16:47:34 +00:00
2022-08-12 17:00:09 +00:00
Future<bool> saveBatchSize(int size) async {
batchSize = size;
return await prefs.setInt(batchSizeKey, size);
}
2022-08-10 16:47:34 +00:00
}