From 65a8f1e6c3c870c7d5dbb04382e35af0bc65f710 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 21 Dec 2024 01:58:49 +0800 Subject: [PATCH] :bug: Bug fixes on config refactor --- lib/main.dart | 15 +++++------ lib/providers/config.dart | 25 +++++++++++++++++-- lib/providers/sn_network.dart | 19 ++++++-------- lib/providers/userinfo.dart | 7 ++++-- lib/screens/settings.dart | 4 ++- lib/screens/sharing.dart | 2 -- .../navigation/app_drawer_navigation.dart | 1 - lib/widgets/universal_image.dart | 2 -- 8 files changed, 47 insertions(+), 28 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index b287cd4..ca618ee 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -86,15 +86,16 @@ class SolianApp extends StatelessWidget { assetLoader: JsonAssetLoader(), child: MultiProvider( providers: [ - Provider(create: (ctx) => ConfigProvider()), + // System extensions layer + Provider(create: (ctx) => HomeWidgetProvider(ctx)), + + // Preferences layer + Provider(create: (ctx) => ConfigProvider(ctx)), // Display layer ChangeNotifierProvider(create: (_) => ThemeProvider()), ChangeNotifierProvider(create: (ctx) => NavigationProvider()), - // System extensions layer - Provider(create: (ctx) => HomeWidgetProvider(ctx)), - // Data layer Provider(create: (ctx) => SnNetworkProvider(ctx)), Provider(create: (ctx) => UserDirectoryProvider(ctx)), @@ -167,12 +168,12 @@ class _AppSplashScreenState extends State<_AppSplashScreen> { Future _initialize() async { try { - final config = context.read(); - await config.initialize(); - if (!mounted) return; final home = context.read(); await home.initialize(); 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(); await sn.initializeUserAgent(); if (!mounted) return; diff --git a/lib/providers/config.dart b/lib/providers/config.dart index 14b8f62..551e703 100644 --- a/lib/providers/config.dart +++ b/lib/providers/config.dart @@ -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: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 kImageQualityLevel = { 'settingsImageQualityLowest': FilterQuality.none, @@ -12,6 +19,12 @@ const Map kImageQualityLevel = { class ConfigProvider { late final SharedPreferences prefs; + late final HomeWidgetProvider _home; + + ConfigProvider(BuildContext context) { + _home = context.read(); + } + Future initialize() async { prefs = await SharedPreferences.getInstance(); } @@ -19,4 +32,12 @@ class ConfigProvider { FilterQuality get imageQuality { 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); + } } diff --git a/lib/providers/sn_network.dart b/lib/providers/sn_network.dart index 7436a48..d8edde7 100644 --- a/lib/providers/sn_network.dart +++ b/lib/providers/sn_network.dart @@ -11,15 +11,10 @@ import 'package:package_info_plus/package_info_plus.dart'; import 'package:device_info_plus/device_info_plus.dart'; import 'package:provider/provider.dart'; import 'package:shared_preferences/shared_preferences.dart'; +import 'package:surface/providers/config.dart'; import 'package:surface/providers/widget.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 = [ ('Solar Network', 'https://api.sn.solsynth.dev'), ('Local', 'http://localhost:8001'), @@ -29,6 +24,7 @@ class SnNetworkProvider { late final Dio client; late final SharedPreferences _prefs; + late final ConfigProvider _config; late final HomeWidgetProvider _home; String? _userAgent; @@ -66,11 +62,12 @@ class SnNetworkProvider { ), ); - SharedPreferences.getInstance().then((prefs) { - _prefs = prefs; - client.options.baseUrl = _prefs.getString(kNetworkServerStoreKey) ?? kNetworkServerDefault; + _config = context.read(); + _config.initialize().then((_) { + _prefs = _config.prefs; + client.options.baseUrl = _config.serverUrl; 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) { + _config.serverUrl = url; client.options.baseUrl = url; - _home.saveWidgetData("server_url", client.options.baseUrl); } } diff --git a/lib/providers/userinfo.dart b/lib/providers/userinfo.dart index 1039425..03a4345 100644 --- a/lib/providers/userinfo.dart +++ b/lib/providers/userinfo.dart @@ -1,8 +1,10 @@ import 'dart:developer'; import 'package:flutter/material.dart'; +import 'package:path/path.dart'; import 'package:provider/provider.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/widget.dart'; import 'package:surface/types/account.dart'; @@ -13,10 +15,12 @@ class UserProvider extends ChangeNotifier { late final SnNetworkProvider _sn; late final HomeWidgetProvider _home; + late final ConfigProvider _config; UserProvider(BuildContext context) { _sn = context.read(); _home = context.read(); + _config = context.read(); } Future get atk async { @@ -25,8 +29,7 @@ class UserProvider extends ChangeNotifier { } Future initialize() async { - final prefs = await SharedPreferences.getInstance(); - final value = prefs.getString(kAtkStoreKey); + final value = _config.prefs.getString(kAtkStoreKey); isAuthorized = value != null; notifyListeners(); refreshUser().then((value) { diff --git a/lib/screens/settings.dart b/lib/screens/settings.dart index b7920f1..fffe812 100644 --- a/lib/screens/settings.dart +++ b/lib/screens/settings.dart @@ -40,7 +40,9 @@ class _SettingsScreenState extends State { setState(() {}); } }); - _prefs = context.read().prefs; + final config = context.read(); + _prefs = config.prefs; + _serverUrlController.text = config.serverUrl; } @override diff --git a/lib/screens/sharing.dart b/lib/screens/sharing.dart index 2f8da8f..7c9c217 100644 --- a/lib/screens/sharing.dart +++ b/lib/screens/sharing.dart @@ -6,13 +6,11 @@ import 'package:cross_file/cross_file.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; -import 'package:flutter/scheduler.dart'; import 'package:gap/gap.dart'; import 'package:go_router/go_router.dart'; import 'package:receive_sharing_intent/receive_sharing_intent.dart'; import 'package:surface/controllers/post_write_controller.dart'; import 'package:surface/screens/post/post_editor.dart'; -import 'package:surface/widgets/dialog.dart'; class AppSharingListener extends StatefulWidget { final Widget child; diff --git a/lib/widgets/navigation/app_drawer_navigation.dart b/lib/widgets/navigation/app_drawer_navigation.dart index 8faa623..3585e1e 100644 --- a/lib/widgets/navigation/app_drawer_navigation.dart +++ b/lib/widgets/navigation/app_drawer_navigation.dart @@ -1,7 +1,6 @@ import 'package:easy_localization/easy_localization.dart'; import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; -import 'package:package_info_plus/package_info_plus.dart'; import 'package:provider/provider.dart'; import 'package:responsive_framework/responsive_framework.dart'; import 'package:styled_widget/styled_widget.dart'; diff --git a/lib/widgets/universal_image.dart b/lib/widgets/universal_image.dart index 9bd8a56..4a4aa17 100644 --- a/lib/widgets/universal_image.dart +++ b/lib/widgets/universal_image.dart @@ -1,5 +1,3 @@ -import 'dart:io'; - import 'package:cached_network_image/cached_network_image.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart';