🐛 Bug fixes on config refactor

This commit is contained in:
LittleSheep 2024-12-21 01:58:49 +08:00
parent 2671ffad4b
commit 65a8f1e6c3
8 changed files with 47 additions and 28 deletions

View File

@ -86,15 +86,16 @@ class SolianApp extends StatelessWidget {
assetLoader: JsonAssetLoader(), assetLoader: JsonAssetLoader(),
child: MultiProvider( child: MultiProvider(
providers: [ providers: [
Provider(create: (ctx) => ConfigProvider()), // System extensions layer
Provider(create: (ctx) => HomeWidgetProvider(ctx)),
// Preferences layer
Provider(create: (ctx) => ConfigProvider(ctx)),
// Display layer // Display layer
ChangeNotifierProvider(create: (_) => ThemeProvider()), ChangeNotifierProvider(create: (_) => ThemeProvider()),
ChangeNotifierProvider(create: (ctx) => NavigationProvider()), ChangeNotifierProvider(create: (ctx) => NavigationProvider()),
// System extensions layer
Provider(create: (ctx) => HomeWidgetProvider(ctx)),
// Data layer // Data layer
Provider(create: (ctx) => SnNetworkProvider(ctx)), Provider(create: (ctx) => SnNetworkProvider(ctx)),
Provider(create: (ctx) => UserDirectoryProvider(ctx)), Provider(create: (ctx) => UserDirectoryProvider(ctx)),
@ -167,12 +168,12 @@ class _AppSplashScreenState extends State<_AppSplashScreen> {
Future<void> _initialize() async { Future<void> _initialize() async {
try { try {
final config = context.read<ConfigProvider>();
await config.initialize();
if (!mounted) return;
final home = context.read<HomeWidgetProvider>(); final home = context.read<HomeWidgetProvider>();
await home.initialize(); await home.initialize();
if (!mounted) return; if (!mounted) return;
// The Network initialization must be done after the HomeWidget initialization
// The Network initialization will save the server url to the HomeWidget
// The Network initialization will also save initialize the Config, so it not need to be initialized again
final sn = context.read<SnNetworkProvider>(); final sn = context.read<SnNetworkProvider>();
await sn.initializeUserAgent(); await sn.initializeUserAgent();
if (!mounted) return; if (!mounted) return;

View File

@ -1,6 +1,13 @@
import 'dart:ui'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:surface/providers/widget.dart';
const kAtkStoreKey = 'nex_user_atk';
const kRtkStoreKey = 'nex_user_rtk';
const kNetworkServerDefault = 'https://api.sn.solsynth.dev';
const kNetworkServerStoreKey = 'app_server_url';
const Map<String, FilterQuality> kImageQualityLevel = { const Map<String, FilterQuality> kImageQualityLevel = {
'settingsImageQualityLowest': FilterQuality.none, 'settingsImageQualityLowest': FilterQuality.none,
@ -12,6 +19,12 @@ const Map<String, FilterQuality> kImageQualityLevel = {
class ConfigProvider { class ConfigProvider {
late final SharedPreferences prefs; late final SharedPreferences prefs;
late final HomeWidgetProvider _home;
ConfigProvider(BuildContext context) {
_home = context.read<HomeWidgetProvider>();
}
Future<void> initialize() async { Future<void> initialize() async {
prefs = await SharedPreferences.getInstance(); prefs = await SharedPreferences.getInstance();
} }
@ -19,4 +32,12 @@ class ConfigProvider {
FilterQuality get imageQuality { FilterQuality get imageQuality {
return kImageQualityLevel.values.elementAtOrNull(prefs.getInt('app_image_quality') ?? 3) ?? FilterQuality.high; return kImageQualityLevel.values.elementAtOrNull(prefs.getInt('app_image_quality') ?? 3) ?? FilterQuality.high;
} }
String get serverUrl {
return prefs.getString(kNetworkServerStoreKey) ?? kNetworkServerDefault;
}
set serverUrl(String url) {
prefs.setString(kNetworkServerStoreKey, url);
_home.saveWidgetData("nex_server_url", url);
}
} }

View File

@ -11,15 +11,10 @@ import 'package:package_info_plus/package_info_plus.dart';
import 'package:device_info_plus/device_info_plus.dart'; import 'package:device_info_plus/device_info_plus.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:surface/providers/config.dart';
import 'package:surface/providers/widget.dart'; import 'package:surface/providers/widget.dart';
import 'package:synchronized/synchronized.dart'; import 'package:synchronized/synchronized.dart';
const kAtkStoreKey = 'nex_user_atk';
const kRtkStoreKey = 'nex_user_rtk';
const kNetworkServerDefault = 'https://api.sn.solsynth.dev';
const kNetworkServerStoreKey = 'app_server_url';
const kNetworkServerDirectory = [ const kNetworkServerDirectory = [
('Solar Network', 'https://api.sn.solsynth.dev'), ('Solar Network', 'https://api.sn.solsynth.dev'),
('Local', 'http://localhost:8001'), ('Local', 'http://localhost:8001'),
@ -29,6 +24,7 @@ class SnNetworkProvider {
late final Dio client; late final Dio client;
late final SharedPreferences _prefs; late final SharedPreferences _prefs;
late final ConfigProvider _config;
late final HomeWidgetProvider _home; late final HomeWidgetProvider _home;
String? _userAgent; String? _userAgent;
@ -66,11 +62,12 @@ class SnNetworkProvider {
), ),
); );
SharedPreferences.getInstance().then((prefs) { _config = context.read<ConfigProvider>();
_prefs = prefs; _config.initialize().then((_) {
client.options.baseUrl = _prefs.getString(kNetworkServerStoreKey) ?? kNetworkServerDefault; _prefs = _config.prefs;
client.options.baseUrl = _config.serverUrl;
if (!context.mounted) return; if (!context.mounted) return;
_home.saveWidgetData("server_url", client.options.baseUrl); _home.saveWidgetData("nex_server_url", client.options.baseUrl);
}); });
} }
@ -197,7 +194,7 @@ class SnNetworkProvider {
} }
void setBaseUrl(String url) { void setBaseUrl(String url) {
_config.serverUrl = url;
client.options.baseUrl = url; client.options.baseUrl = url;
_home.saveWidgetData("server_url", client.options.baseUrl);
} }
} }

View File

@ -1,8 +1,10 @@
import 'dart:developer'; import 'dart:developer';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:path/path.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:surface/providers/config.dart';
import 'package:surface/providers/sn_network.dart'; import 'package:surface/providers/sn_network.dart';
import 'package:surface/providers/widget.dart'; import 'package:surface/providers/widget.dart';
import 'package:surface/types/account.dart'; import 'package:surface/types/account.dart';
@ -13,10 +15,12 @@ class UserProvider extends ChangeNotifier {
late final SnNetworkProvider _sn; late final SnNetworkProvider _sn;
late final HomeWidgetProvider _home; late final HomeWidgetProvider _home;
late final ConfigProvider _config;
UserProvider(BuildContext context) { UserProvider(BuildContext context) {
_sn = context.read<SnNetworkProvider>(); _sn = context.read<SnNetworkProvider>();
_home = context.read<HomeWidgetProvider>(); _home = context.read<HomeWidgetProvider>();
_config = context.read<ConfigProvider>();
} }
Future<String?> get atk async { Future<String?> get atk async {
@ -25,8 +29,7 @@ class UserProvider extends ChangeNotifier {
} }
Future<void> initialize() async { Future<void> initialize() async {
final prefs = await SharedPreferences.getInstance(); final value = _config.prefs.getString(kAtkStoreKey);
final value = prefs.getString(kAtkStoreKey);
isAuthorized = value != null; isAuthorized = value != null;
notifyListeners(); notifyListeners();
refreshUser().then((value) { refreshUser().then((value) {

View File

@ -40,7 +40,9 @@ class _SettingsScreenState extends State<SettingsScreen> {
setState(() {}); setState(() {});
} }
}); });
_prefs = context.read<ConfigProvider>().prefs; final config = context.read<ConfigProvider>();
_prefs = config.prefs;
_serverUrlController.text = config.serverUrl;
} }
@override @override

View File

@ -6,13 +6,11 @@ import 'package:cross_file/cross_file.dart';
import 'package:easy_localization/easy_localization.dart'; import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:gap/gap.dart'; import 'package:gap/gap.dart';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
import 'package:receive_sharing_intent/receive_sharing_intent.dart'; import 'package:receive_sharing_intent/receive_sharing_intent.dart';
import 'package:surface/controllers/post_write_controller.dart'; import 'package:surface/controllers/post_write_controller.dart';
import 'package:surface/screens/post/post_editor.dart'; import 'package:surface/screens/post/post_editor.dart';
import 'package:surface/widgets/dialog.dart';
class AppSharingListener extends StatefulWidget { class AppSharingListener extends StatefulWidget {
final Widget child; final Widget child;

View File

@ -1,7 +1,6 @@
import 'package:easy_localization/easy_localization.dart'; import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:responsive_framework/responsive_framework.dart'; import 'package:responsive_framework/responsive_framework.dart';
import 'package:styled_widget/styled_widget.dart'; import 'package:styled_widget/styled_widget.dart';

View File

@ -1,5 +1,3 @@
import 'dart:io';
import 'package:cached_network_image/cached_network_image.dart'; import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';