Skip to content

Commit

Permalink
fix sample
Browse files Browse the repository at this point in the history
  • Loading branch information
hoc081098 committed Aug 13, 2024
1 parent 2f484b7 commit ac46214
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 16 deletions.
3 changes: 2 additions & 1 deletion example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hoc.example">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:label="example"
android:icon="@mipmap/ic_launcher">
<activity
android:exported="true"
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
Expand Down
4 changes: 2 additions & 2 deletions example/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
buildscript {
ext.kotlin_version = '1.7.0'
ext.kotlin_version = '2.0.0'
repositories {
google()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:7.0.2'
classpath 'com.android.tools.build:gradle:8.5.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Expand Down
2 changes: 1 addition & 1 deletion example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
4 changes: 2 additions & 2 deletions example/lib/dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ extension DialogExtensions on BuildContext {
}

try {
await rxPrefs.updateStringList(key, (currentList) {
await rxPrefs.updateStringList(listKey, (currentList) {
final list = currentList ?? const <String>[];
if (list.contains(string)) {
throw StateError('Duplicated $string!');
Expand Down Expand Up @@ -78,7 +78,7 @@ extension DialogExtensions on BuildContext {

try {
await rxPrefs.updateStringList(
key,
listKey,
(currentList) => [
for (final s in (currentList ?? const <String>[]))
if (s != needRemove) s
Expand Down
7 changes: 4 additions & 3 deletions example/lib/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -23,7 +23,7 @@ class _MyHomePageState extends State<MyHomePage> {
late final StateStream<ViewState> 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: '<<STATE>>', log: debugPrint)
Expand Down Expand Up @@ -146,6 +146,7 @@ extension BuildContextX on BuildContext {
}
}

@immutable
class ViewState {
final List<String> items;
final bool isLoading;
Expand All @@ -155,7 +156,7 @@ class ViewState {

const ViewState._(this.items, this.isLoading, this.error);

ViewState.success(List<String> items) : this._(items, false, null);
const ViewState.success(List<String> items) : this._(items, false, null);

ViewState.failure(Object e, StackTrace s)
: this._([], false, AsyncError(e, s));
Expand Down
4 changes: 2 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);

Expand Down
2 changes: 1 addition & 1 deletion example/lib/snippet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions lib/src/interface/rx_shared_preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ abstract class RxSharedPreferences extends RxStorage<String, void>
/// 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(
Expand All @@ -39,7 +40,8 @@ abstract class RxSharedPreferences extends RxStorage<String, void>

/// 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<SharedPreferences> prefsOrFuture, [
Expand Down

0 comments on commit ac46214

Please sign in to comment.