From 3cd08da3b6472de0940350df7f5d0704c44e5159 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 23 Nov 2024 19:54:38 +0800 Subject: [PATCH] :recycle: Replace storage token engine to prevent some platform specific issue --- lib/providers/sn_network.dart | 41 +++++++++------ lib/providers/userinfo.dart | 13 +++-- lib/screens/auth/login.dart | 2 +- lib/screens/post/post_search.dart | 17 ++++++- linux/flutter/generated_plugin_registrant.cc | 4 -- linux/flutter/generated_plugins.cmake | 1 - macos/Flutter/GeneratedPluginRegistrant.swift | 2 - pubspec.lock | 50 +------------------ pubspec.yaml | 2 +- .../flutter/generated_plugin_registrant.cc | 3 -- windows/flutter/generated_plugins.cmake | 1 - 11 files changed, 53 insertions(+), 83 deletions(-) diff --git a/lib/providers/sn_network.dart b/lib/providers/sn_network.dart index bcdea9e..9c3236c 100644 --- a/lib/providers/sn_network.dart +++ b/lib/providers/sn_network.dart @@ -1,11 +1,12 @@ +import 'dart:async'; import 'dart:convert'; import 'dart:developer'; import 'package:dio/dio.dart'; import 'package:dio_smart_retry/dio_smart_retry.dart'; -import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:surface/providers/adapters/sn_network_universal.dart'; +import 'package:synchronized/synchronized.dart'; const kAtkStoreKey = 'nex_user_atk'; const kRtkStoreKey = 'nex_user_rtk'; @@ -23,7 +24,6 @@ class SnNetworkProvider { late Dio client; late final SharedPreferences _prefs; - late final FlutterSecureStorage _storage = FlutterSecureStorage(); SnNetworkProvider() { client = Dio(); @@ -62,9 +62,19 @@ class SnNetworkProvider { }); } + final tkLock = Lock(); + + Completer? _refreshCompleter; + Future getFreshAtk() async { + if (_refreshCompleter != null) { + return await _refreshCompleter!.future; + } else { + _refreshCompleter = Completer(); + } + try { - var atk = await _storage.read(key: kAtkStoreKey); + var atk = _prefs.getString(kAtkStoreKey); if (atk != null) { final atkParts = atk.split('.'); if (atkParts.length != 3) { @@ -94,13 +104,18 @@ class SnNetworkProvider { } if (atk != null) { + _refreshCompleter!.complete(atk); return atk; } else { log('Access token refresh failed...'); + _refreshCompleter!.complete(null); } } } catch (err) { log('Failed to authenticate user: $err'); + _refreshCompleter!.completeError(err); + } finally { + _refreshCompleter = null; } return null; @@ -111,22 +126,18 @@ class SnNetworkProvider { return '${client.options.baseUrl}/cgi/uc/attachments/$ky'; } - Future setTokenPair(String atk, String rtk) async { - await Future.wait([ - _storage.write(key: kAtkStoreKey, value: atk), - _storage.write(key: kRtkStoreKey, value: rtk), - ]); + void setTokenPair(String atk, String rtk) { + _prefs.setString(kAtkStoreKey, atk); + _prefs.setString(kRtkStoreKey, rtk); } - Future clearTokenPair() async { - await Future.wait([ - _storage.delete(key: kAtkStoreKey), - _storage.delete(key: kRtkStoreKey), - ]); + void clearTokenPair() { + _prefs.remove(kAtkStoreKey); + _prefs.remove(kRtkStoreKey); } Future refreshToken() async { - final rtk = await _storage.read(key: kRtkStoreKey); + final rtk = _prefs.getString(kRtkStoreKey); if (rtk == null) return null; final dio = Dio(); @@ -139,7 +150,7 @@ class SnNetworkProvider { final atk = resp.data['access_token']; final nRtk = resp.data['refresh_token']; - await setTokenPair(atk, nRtk); + setTokenPair(atk, nRtk); return atk; } diff --git a/lib/providers/userinfo.dart b/lib/providers/userinfo.dart index c0b1597..26c4ba4 100644 --- a/lib/providers/userinfo.dart +++ b/lib/providers/userinfo.dart @@ -1,8 +1,8 @@ import 'dart:developer'; import 'package:flutter/material.dart'; -import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:provider/provider.dart'; +import 'package:shared_preferences/shared_preferences.dart'; import 'package:surface/providers/sn_network.dart'; import 'package:surface/types/account.dart'; @@ -11,14 +11,17 @@ class UserProvider extends ChangeNotifier { SnAccount? user; late final SnNetworkProvider _sn; - late final FlutterSecureStorage _storage = FlutterSecureStorage(); - Future get atk => _storage.read(key: kAtkStoreKey); + Future get atk async { + final prefs = await SharedPreferences.getInstance(); + return prefs.getString(kAtkStoreKey); + } UserProvider(BuildContext context) { _sn = context.read(); - _storage.read(key: kAtkStoreKey).then((value) { + SharedPreferences.getInstance().then((prefs) { + final value = prefs.getString(kAtkStoreKey); isAuthorized = value != null; notifyListeners(); refreshUser().then((value) { @@ -41,7 +44,7 @@ class UserProvider extends ChangeNotifier { } void logoutUser() async { - await _sn.clearTokenPair(); + _sn.clearTokenPair(); isAuthorized = false; user = null; notifyListeners(); diff --git a/lib/screens/auth/login.dart b/lib/screens/auth/login.dart index d4d3082..e7cff66 100644 --- a/lib/screens/auth/login.dart +++ b/lib/screens/auth/login.dart @@ -151,7 +151,7 @@ class _LoginCheckScreenState extends State<_LoginCheckScreen> { }); final atk = tokenResp.data['access_token']; final rtk = tokenResp.data['refresh_token']; - await sn.setTokenPair(atk, rtk); + sn.setTokenPair(atk, rtk); if (!mounted) return; final user = context.read(); final userinfo = await user.refreshUser(); diff --git a/lib/screens/post/post_search.dart b/lib/screens/post/post_search.dart index e773da9..a9b1c2a 100644 --- a/lib/screens/post/post_search.dart +++ b/lib/screens/post/post_search.dart @@ -74,6 +74,15 @@ class _PostSearchScreenState extends State { if (mounted) setState(() => _isBusy = false); } + void _showAdvancedSearchTune() { + showModalBottomSheet( + context: context, + builder: (context) => Column( + children: [], + ), + ); + } + @override Widget build(BuildContext context) { const labelShadows = [ @@ -87,11 +96,17 @@ class _PostSearchScreenState extends State { return Scaffold( appBar: AppBar( title: Text('screenPostSearch').tr(), + actions: [ + IconButton( + icon: const Icon(Symbols.tune), + onPressed: _showAdvancedSearchTune, + ), + ], ), body: Stack( children: [ InfiniteList( - padding: const EdgeInsets.only(top: 96), + padding: const EdgeInsets.only(top: 100), itemCount: _posts.length, isLoading: _isBusy, hasReachedMax: _postCount != null && _posts.length >= _postCount!, diff --git a/linux/flutter/generated_plugin_registrant.cc b/linux/flutter/generated_plugin_registrant.cc index a6f6671..66ab3fc 100644 --- a/linux/flutter/generated_plugin_registrant.cc +++ b/linux/flutter/generated_plugin_registrant.cc @@ -7,7 +7,6 @@ #include "generated_plugin_registrant.h" #include -#include #include #include #include @@ -20,9 +19,6 @@ void fl_register_plugins(FlPluginRegistry* registry) { g_autoptr(FlPluginRegistrar) file_selector_linux_registrar = fl_plugin_registry_get_registrar_for_plugin(registry, "FileSelectorPlugin"); file_selector_plugin_register_with_registrar(file_selector_linux_registrar); - g_autoptr(FlPluginRegistrar) flutter_secure_storage_linux_registrar = - fl_plugin_registry_get_registrar_for_plugin(registry, "FlutterSecureStorageLinuxPlugin"); - flutter_secure_storage_linux_plugin_register_with_registrar(flutter_secure_storage_linux_registrar); g_autoptr(FlPluginRegistrar) flutter_udid_registrar = fl_plugin_registry_get_registrar_for_plugin(registry, "FlutterUdidPlugin"); flutter_udid_plugin_register_with_registrar(flutter_udid_registrar); diff --git a/linux/flutter/generated_plugins.cmake b/linux/flutter/generated_plugins.cmake index 5a108eb..51673e7 100644 --- a/linux/flutter/generated_plugins.cmake +++ b/linux/flutter/generated_plugins.cmake @@ -4,7 +4,6 @@ list(APPEND FLUTTER_PLUGIN_LIST file_selector_linux - flutter_secure_storage_linux flutter_udid isar_flutter_libs media_kit_libs_linux diff --git a/macos/Flutter/GeneratedPluginRegistrant.swift b/macos/Flutter/GeneratedPluginRegistrant.swift index e85a9e7..2d9b468 100644 --- a/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/macos/Flutter/GeneratedPluginRegistrant.swift @@ -10,7 +10,6 @@ import file_selector_macos import firebase_analytics import firebase_core import firebase_messaging -import flutter_secure_storage_macos import flutter_udid import isar_flutter_libs import media_kit_libs_macos_video @@ -31,7 +30,6 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { FLTFirebaseAnalyticsPlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseAnalyticsPlugin")) FLTFirebaseCorePlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseCorePlugin")) FLTFirebaseMessagingPlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseMessagingPlugin")) - FlutterSecureStoragePlugin.register(with: registry.registrar(forPlugin: "FlutterSecureStoragePlugin")) FlutterUdidPlugin.register(with: registry.registrar(forPlugin: "FlutterUdidPlugin")) IsarFlutterLibsPlugin.register(with: registry.registrar(forPlugin: "IsarFlutterLibsPlugin")) MediaKitLibsMacosVideoPlugin.register(with: registry.registrar(forPlugin: "MediaKitLibsMacosVideoPlugin")) diff --git a/pubspec.lock b/pubspec.lock index e1d2e08..7180182 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -608,54 +608,6 @@ packages: url: "https://pub.dev" source: hosted version: "2.0.23" - flutter_secure_storage: - dependency: "direct main" - description: - name: flutter_secure_storage - sha256: "165164745e6afb5c0e3e3fcc72a012fb9e58496fb26ffb92cf22e16a821e85d0" - url: "https://pub.dev" - source: hosted - version: "9.2.2" - flutter_secure_storage_linux: - dependency: transitive - description: - name: flutter_secure_storage_linux - sha256: "4d91bfc23047422cbcd73ac684bc169859ee766482517c22172c86596bf1464b" - url: "https://pub.dev" - source: hosted - version: "1.2.1" - flutter_secure_storage_macos: - dependency: transitive - description: - name: flutter_secure_storage_macos - sha256: "1693ab11121a5f925bbea0be725abfcfbbcf36c1e29e571f84a0c0f436147a81" - url: "https://pub.dev" - source: hosted - version: "3.1.2" - flutter_secure_storage_platform_interface: - dependency: transitive - description: - name: flutter_secure_storage_platform_interface - sha256: cf91ad32ce5adef6fba4d736a542baca9daf3beac4db2d04be350b87f69ac4a8 - url: "https://pub.dev" - source: hosted - version: "1.1.2" - flutter_secure_storage_web: - dependency: transitive - description: - name: flutter_secure_storage_web - sha256: f4ebff989b4f07b2656fb16b47852c0aab9fed9b4ec1c70103368337bc1886a9 - url: "https://pub.dev" - source: hosted - version: "1.2.1" - flutter_secure_storage_windows: - dependency: transitive - description: - name: flutter_secure_storage_windows - sha256: b20b07cb5ed4ed74fc567b78a72936203f587eba460af1df11281c9326cd3709 - url: "https://pub.dev" - source: hosted - version: "3.1.2" flutter_shaders: dependency: transitive description: @@ -1608,7 +1560,7 @@ packages: source: hosted version: "1.0.6" synchronized: - dependency: transitive + dependency: "direct main" description: name: synchronized sha256: "69fe30f3a8b04a0be0c15ae6490fc859a78ef4c43ae2dd5e8a623d45bfcf9225" diff --git a/pubspec.yaml b/pubspec.yaml index 38db34e..9724db8 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -58,7 +58,6 @@ dependencies: google_fonts: ^6.2.1 path: ^1.9.0 relative_time: ^5.0.0 - flutter_secure_storage: ^9.2.2 image_picker: ^1.1.2 cross_file: ^0.3.4+2 file_picker: ^8.1.3 @@ -88,6 +87,7 @@ dependencies: media_kit_libs_video: ^1.0.5 pasteboard: ^0.3.0 sentry_flutter: ^8.10.1 + synchronized: ^3.3.0+3 dev_dependencies: flutter_test: diff --git a/windows/flutter/generated_plugin_registrant.cc b/windows/flutter/generated_plugin_registrant.cc index 3d874c9..b0b33d1 100644 --- a/windows/flutter/generated_plugin_registrant.cc +++ b/windows/flutter/generated_plugin_registrant.cc @@ -9,7 +9,6 @@ #include #include #include -#include #include #include #include @@ -26,8 +25,6 @@ void RegisterPlugins(flutter::PluginRegistry* registry) { registry->GetRegistrarForPlugin("FileSelectorWindows")); FirebaseCorePluginCApiRegisterWithRegistrar( registry->GetRegistrarForPlugin("FirebaseCorePluginCApi")); - FlutterSecureStorageWindowsPluginRegisterWithRegistrar( - registry->GetRegistrarForPlugin("FlutterSecureStorageWindowsPlugin")); FlutterUdidPluginCApiRegisterWithRegistrar( registry->GetRegistrarForPlugin("FlutterUdidPluginCApi")); IsarFlutterLibsPluginRegisterWithRegistrar( diff --git a/windows/flutter/generated_plugins.cmake b/windows/flutter/generated_plugins.cmake index b714b75..75f22ee 100644 --- a/windows/flutter/generated_plugins.cmake +++ b/windows/flutter/generated_plugins.cmake @@ -6,7 +6,6 @@ list(APPEND FLUTTER_PLUGIN_LIST connectivity_plus file_selector_windows firebase_core - flutter_secure_storage_windows flutter_udid isar_flutter_libs media_kit_libs_windows_video