Skip to content

Commit 2a95ce6

Browse files
authored
support SharedPreferencesWithCache and SharedPreferencesAsync. (#76)
* support SharedPreferencesWithCache and SharedPreferencesAsync. issue #75 * add test: RxSharedPreferences.async * add test: RxSharedPreferences.withCache * bump constraints * include tests * shared_preferences: ^2.3.0 in sample * fix samples * min flutter 3.22 * gradle wrapper up * sdk 34 * fix sample * fix android sample * CI * pub upgrade * fix lint * use allowList in SharedPreferencesAsyncAdapter
1 parent 897fce0 commit 2a95ce6

24 files changed

+1473
-142
lines changed

.github/workflows/build-example.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
- uses: actions/setup-java@v4
2424
with:
2525
distribution: 'zulu'
26-
java-version: '11'
26+
java-version: '17'
2727

2828
- uses: subosito/[email protected]
2929
with:
@@ -64,7 +64,7 @@ jobs:
6464
runs-on: ubuntu-latest
6565
strategy:
6666
matrix:
67-
version: [ '3.3.0', '3.7.0' ]
67+
version: [ '3.22.0' ]
6868
defaults:
6969
run:
7070
working-directory: ./example
@@ -74,7 +74,7 @@ jobs:
7474
- uses: actions/setup-java@v4
7575
with:
7676
distribution: 'zulu'
77-
java-version: '11'
77+
java-version: '17'
7878

7979
- uses: subosito/[email protected]
8080
with:

.github/workflows/tests.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,16 @@ jobs:
4141
- name: Run tests
4242
run: flutter test --coverage --coverage-path=lcov.info
4343

44-
- uses: codecov/[email protected]
44+
- uses: codecov/codecov-action@v4
45+
env:
46+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
4547
if: ${{ matrix.channel == 'stable' }}
4648

4749
test-old-versions:
4850
runs-on: ubuntu-latest
4951
strategy:
5052
matrix:
51-
version: [ '3.3.0', '3.7.0' ]
53+
version: [ '3.22.0' ]
5254
steps:
5355
- uses: actions/checkout@v4
5456

example/android/app/build.gradle

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
plugins {
2+
id "com.android.application"
3+
id "kotlin-android"
4+
id "dev.flutter.flutter-gradle-plugin"
5+
}
6+
7+
kotlin {
8+
jvmToolchain {
9+
languageVersion = JavaLanguageVersion.of(17)
10+
vendor = JvmVendorSpec.AZUL
11+
}
12+
}
13+
114
def localProperties = new Properties()
215
def localPropertiesFile = rootProject.file('local.properties')
316
if (localPropertiesFile.exists()) {
@@ -6,11 +19,6 @@ if (localPropertiesFile.exists()) {
619
}
720
}
821

9-
def flutterRoot = localProperties.getProperty('flutter.sdk')
10-
if (flutterRoot == null) {
11-
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
12-
}
13-
1422
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
1523
if (flutterVersionCode == null) {
1624
flutterVersionCode = '1'
@@ -21,12 +29,9 @@ if (flutterVersionName == null) {
2129
flutterVersionName = '1.0'
2230
}
2331

24-
apply plugin: 'com.android.application'
25-
apply plugin: 'kotlin-android'
26-
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
27-
2832
android {
29-
compileSdkVersion 31
33+
compileSdkVersion 34
34+
namespace 'com.hoc.example'
3035

3136
sourceSets {
3237
main.java.srcDirs += 'src/main/kotlin'
@@ -35,8 +40,8 @@ android {
3540
defaultConfig {
3641
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
3742
applicationId "com.hoc.example"
38-
minSdkVersion 16
39-
targetSdkVersion 30
43+
minSdkVersion 24
44+
targetSdkVersion 34
4045
versionCode flutterVersionCode.toInteger()
4146
versionName flutterVersionName
4247
}
@@ -48,12 +53,17 @@ android {
4853
signingConfig signingConfigs.debug
4954
}
5055
}
56+
kotlinOptions {
57+
jvmTarget = '1.8'
58+
}
59+
compileOptions {
60+
sourceCompatibility JavaVersion.VERSION_1_8
61+
targetCompatibility JavaVersion.VERSION_1_8
62+
}
5163
}
5264

5365
flutter {
5466
source '../..'
5567
}
5668

57-
dependencies {
58-
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
59-
}
69+
dependencies {}

example/android/app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
package="com.hoc.example">
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
32
<application
43
android:label="example"
54
android:icon="@mipmap/ic_launcher">
65
<activity
6+
android:exported="true"
77
android:name=".MainActivity"
88
android:launchMode="singleTop"
99
android:theme="@style/LaunchTheme"

example/android/build.gradle

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,3 @@
1-
buildscript {
2-
ext.kotlin_version = '1.6.10'
3-
repositories {
4-
google()
5-
jcenter()
6-
}
7-
8-
dependencies {
9-
classpath 'com.android.tools.build:gradle:4.1.0'
10-
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
11-
}
12-
}
13-
141
allprojects {
152
repositories {
163
google()
@@ -26,6 +13,6 @@ subprojects {
2613
project.evaluationDependsOn(':app')
2714
}
2815

29-
task clean(type: Delete) {
16+
tasks.register("clean", Delete) {
3017
delete rootProject.buildDir
3118
}

example/android/gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip

example/android/settings.gradle

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
1-
include ':app'
1+
pluginManagement {
2+
def flutterSdkPath = {
3+
def properties = new Properties()
4+
file("local.properties").withInputStream { properties.load(it) }
5+
def flutterSdkPath = properties.getProperty("flutter.sdk")
6+
assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
7+
return flutterSdkPath
8+
}()
29

3-
def localPropertiesFile = new File(rootProject.projectDir, "local.properties")
4-
def properties = new Properties()
10+
includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")
511

6-
assert localPropertiesFile.exists()
7-
localPropertiesFile.withReader("UTF-8") { reader -> properties.load(reader) }
12+
repositories {
13+
google()
14+
mavenCentral()
15+
gradlePluginPortal()
16+
}
17+
}
818

9-
def flutterSdkPath = properties.getProperty("flutter.sdk")
10-
assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
11-
apply from: "$flutterSdkPath/packages/flutter_tools/gradle/app_plugin_loader.gradle"
19+
plugins {
20+
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
21+
id "com.android.application" version "8.5.2" apply false
22+
id "org.jetbrains.kotlin.android" version "2.0.0" apply false
23+
}
24+
25+
include ":app"

example/lib/dialog.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ extension DialogExtensions on BuildContext {
3636
}
3737

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

7979
try {
8080
await rxPrefs.updateStringList(
81-
key,
81+
listKey,
8282
(currentList) => [
8383
for (final s in (currentList ?? const <String>[]))
8484
if (s != needRemove) s

example/lib/home.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import 'package:flutter_provider/flutter_provider.dart';
77
import 'package:rx_shared_preferences/rx_shared_preferences.dart';
88
import 'package:rxdart_ext/rxdart_ext.dart';
99

10-
const key = 'com.hoc.list';
10+
const listKey = 'com.hoc.list';
1111

1212
class MyHomePage extends StatefulWidget {
13-
const MyHomePage({Key? key}) : super(key: key);
13+
const MyHomePage({super.key});
1414

1515
@override
16-
_MyHomePageState createState() => _MyHomePageState();
16+
State<MyHomePage> createState() => _MyHomePageState();
1717
}
1818

1919
class _MyHomePageState extends State<MyHomePage> {
@@ -23,7 +23,7 @@ class _MyHomePageState extends State<MyHomePage> {
2323
late final StateStream<ViewState> list$ = controller.stream
2424
.startWith(null)
2525
.switchMap((_) => context.rxPrefs
26-
.getStringListStream(key)
26+
.getStringListStream(listKey)
2727
.map((list) => ViewState.success(list ?? const []))
2828
.onErrorReturnWith((e, s) => ViewState.failure(e, s)))
2929
.debug(identifier: '<<STATE>>', log: debugPrint)
@@ -146,6 +146,7 @@ extension BuildContextX on BuildContext {
146146
}
147147
}
148148

149+
@immutable
149150
class ViewState {
150151
final List<String> items;
151152
final bool isLoading;
@@ -155,7 +156,7 @@ class ViewState {
155156

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

158-
ViewState.success(List<String> items) : this._(items, false, null);
159+
const ViewState.success(List<String> items) : this._(items, false, null);
159160

160161
ViewState.failure(Object e, StackTrace s)
161162
: this._([], false, AsyncError(e, s));

example/lib/main.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ void main() {
88
WidgetsFlutterBinding.ensureInitialized();
99

1010
/// Singleton instance for app
11-
final rxPrefs = RxSharedPreferences(
12-
SharedPreferences.getInstance(),
11+
final rxPrefs = RxSharedPreferences.async(
12+
SharedPreferencesAsync(),
1313
kReleaseMode ? null : const RxSharedPreferencesDefaultLogger(),
1414
);
1515

@@ -22,7 +22,7 @@ void main() {
2222
}
2323

2424
class MyApp extends StatelessWidget {
25-
const MyApp({Key? key}) : super(key: key);
25+
const MyApp({super.key});
2626

2727
@override
2828
Widget build(BuildContext context) {

0 commit comments

Comments
 (0)