import 'package:flutter/painting.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../business_logic/auth/oauth.dart' as oauth; 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; Settings._create(); static Future create() async { Settings settings = Settings._create(); 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; } return settings; } Future saveInstanceUrl(String url) async { instanceUrl = url; return await prefs.setString(instanceUrlKey, url); } Future saveAuthCode(String code) async { authCode = code; return await prefs.setString(authCodeKey, code); } Future saveLocale(String locale) async { this.locale = Locale(locale); return await prefs.setString(localeKey, locale); } Future saveApp(oauth.App app) async { clientId = app.clientId; clientSecret = clientSecret; prefs.setString(clientSecretKey, app.clientSecret); prefs.setString(clientIdKey, app.clientId); } Future saveToken(String token) async { this.token = token; return await prefs.setString(tokenKey, token); } Future saveBatchSize(int size) async { batchSize = size; return await prefs.setInt(batchSizeKey, size); } }