From ac46214add7fadeac6d569eb3863b1c8d4943020 Mon Sep 17 00:00:00 2001 From: "hoc081098@gmail.com" Date: Tue, 13 Aug 2024 23:42:27 +0700 Subject: [PATCH] fix sample --- example/android/app/build.gradle | 3 ++- example/android/app/src/main/AndroidManifest.xml | 4 ++-- example/android/build.gradle | 4 ++-- example/android/gradle/wrapper/gradle-wrapper.properties | 2 +- example/lib/dialog.dart | 4 ++-- example/lib/home.dart | 7 ++++--- example/lib/main.dart | 4 ++-- example/lib/snippet.dart | 2 +- lib/src/interface/rx_shared_preferences.dart | 6 ++++-- 9 files changed, 20 insertions(+), 16 deletions(-) diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index 7a6c5a1..91f5e93 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -27,6 +27,7 @@ apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { compileSdkVersion 34 + namespace 'com.hoc.example' sourceSets { main.java.srcDirs += 'src/main/kotlin' @@ -35,7 +36,7 @@ android { defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId "com.hoc.example" - minSdkVersion 16 + minSdkVersion 24 targetSdkVersion 34 versionCode flutterVersionCode.toInteger() versionName flutterVersionName diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml index fa9516b..11ae96c 100644 --- a/example/android/app/src/main/AndroidManifest.xml +++ b/example/android/app/src/main/AndroidManifest.xml @@ -1,9 +1,9 @@ - + []; if (list.contains(string)) { throw StateError('Duplicated $string!'); @@ -78,7 +78,7 @@ extension DialogExtensions on BuildContext { try { await rxPrefs.updateStringList( - key, + listKey, (currentList) => [ for (final s in (currentList ?? const [])) if (s != needRemove) s diff --git a/example/lib/home.dart b/example/lib/home.dart index d6d3992..096fb08 100644 --- a/example/lib/home.dart +++ b/example/lib/home.dart @@ -7,7 +7,7 @@ import 'package:flutter_provider/flutter_provider.dart'; import 'package:rx_shared_preferences/rx_shared_preferences.dart'; import 'package:rxdart_ext/rxdart_ext.dart'; -const key = 'com.hoc.list'; +const listKey = 'com.hoc.list'; class MyHomePage extends StatefulWidget { const MyHomePage({Key? key}) : super(key: key); @@ -23,7 +23,7 @@ class _MyHomePageState extends State { late final StateStream list$ = controller.stream .startWith(null) .switchMap((_) => context.rxPrefs - .getStringListStream(key) + .getStringListStream(listKey) .map((list) => ViewState.success(list ?? const [])) .onErrorReturnWith((e, s) => ViewState.failure(e, s))) .debug(identifier: '<>', log: debugPrint) @@ -146,6 +146,7 @@ extension BuildContextX on BuildContext { } } +@immutable class ViewState { final List items; final bool isLoading; @@ -155,7 +156,7 @@ class ViewState { const ViewState._(this.items, this.isLoading, this.error); - ViewState.success(List items) : this._(items, false, null); + const ViewState.success(List items) : this._(items, false, null); ViewState.failure(Object e, StackTrace s) : this._([], false, AsyncError(e, s)); diff --git a/example/lib/main.dart b/example/lib/main.dart index fb4c588..5fb7c2e 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -8,8 +8,8 @@ void main() { WidgetsFlutterBinding.ensureInitialized(); /// Singleton instance for app - final rxPrefs = RxSharedPreferences( - SharedPreferences.getInstance(), + final rxPrefs = RxSharedPreferences.async( + SharedPreferencesAsync(), kReleaseMode ? null : const RxSharedPreferencesDefaultLogger(), ); diff --git a/example/lib/snippet.dart b/example/lib/snippet.dart index cef26e2..7ee1763 100644 --- a/example/lib/snippet.dart +++ b/example/lib/snippet.dart @@ -25,7 +25,7 @@ const userKey = 'user'; void main() async { // Get RxSharedPreferences instance. - final rxPrefs = RxSharedPreferences.getInstance(); + final rxPrefs = RxSharedPreferences.getAsyncInstance(); // Select stream by key and observe rxPrefs diff --git a/lib/src/interface/rx_shared_preferences.dart b/lib/src/interface/rx_shared_preferences.dart index 5d11c1b..290a13c 100644 --- a/lib/src/interface/rx_shared_preferences.dart +++ b/lib/src/interface/rx_shared_preferences.dart @@ -28,7 +28,8 @@ abstract class RxSharedPreferences extends RxStorage /// Return default singleton instance. /// Custom logger via [RxSharedPreferencesConfigs.logger]. /// - /// This is a legacy API. For new code, consider [RxSharedPreferences.async] or [RxSharedPreferences.asyncWithCache]. + /// This is a legacy API. For new code, consider [RxSharedPreferences.getAsyncInstance] + /// or [RxSharedPreferences.getWithCacheInstance]. @legacyRxSharedPreferencesApi factory RxSharedPreferences.getInstance() => _defaultInstance ??= RxSharedPreferences( @@ -39,7 +40,8 @@ abstract class RxSharedPreferences extends RxStorage /// Construct a [RxSharedPreferences] with [SharedPreferences] and optional [Logger]. /// - /// This is a legacy API. For new code, consider [RxSharedPreferences.async] or [RxSharedPreferences.asyncWithCache]. + /// This is a legacy API. For new code, consider [RxSharedPreferences.async] + /// or [RxSharedPreferences.withCache]. @legacyRxSharedPreferencesApi factory RxSharedPreferences( FutureOr prefsOrFuture, [