Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support SharedPreferencesWithCache and SharedPreferencesAsync. #76

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
6 changes: 3 additions & 3 deletions .github/workflows/build-example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: '11'
java-version: '17'

- uses: subosito/[email protected]
with:
Expand Down Expand Up @@ -64,7 +64,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
version: [ '3.3.0', '3.7.0' ]
version: [ '3.22.0' ]
defaults:
run:
working-directory: ./example
Expand All @@ -74,7 +74,7 @@ jobs:
- uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: '11'
java-version: '17'

- uses: subosito/[email protected]
with:
Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ jobs:
- name: Run tests
run: flutter test --coverage --coverage-path=lcov.info

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

test-old-versions:
runs-on: ubuntu-latest
strategy:
matrix:
version: [ '3.3.0', '3.7.0' ]
version: [ '3.22.0' ]
steps:
- uses: actions/checkout@v4

Expand Down
40 changes: 25 additions & 15 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
plugins {
id "com.android.application"
id "kotlin-android"
id "dev.flutter.flutter-gradle-plugin"
}

kotlin {
jvmToolchain {
languageVersion = JavaLanguageVersion.of(17)
vendor = JvmVendorSpec.AZUL
}
}

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
Expand All @@ -6,11 +19,6 @@ if (localPropertiesFile.exists()) {
}
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
flutterVersionCode = '1'
Expand All @@ -21,12 +29,9 @@ if (flutterVersionName == null) {
flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 31
compileSdkVersion 34
namespace 'com.hoc.example'

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
Expand All @@ -35,8 +40,8 @@ android {
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.hoc.example"
minSdkVersion 16
targetSdkVersion 30
minSdkVersion 24
targetSdkVersion 34
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
Expand All @@ -48,12 +53,17 @@ android {
signingConfig signingConfigs.debug
}
}
kotlinOptions {
jvmTarget = '1.8'
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

flutter {
source '../..'
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
dependencies {}
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
15 changes: 1 addition & 14 deletions example/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
buildscript {
ext.kotlin_version = '1.6.10'
repositories {
google()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

allprojects {
repositories {
google()
Expand All @@ -26,6 +13,6 @@ subprojects {
project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
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-6.7-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
30 changes: 22 additions & 8 deletions example/android/settings.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
include ':app'
pluginManagement {
def flutterSdkPath = {
def properties = new Properties()
file("local.properties").withInputStream { properties.load(it) }
def flutterSdkPath = properties.getProperty("flutter.sdk")
assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
return flutterSdkPath
}()

def localPropertiesFile = new File(rootProject.projectDir, "local.properties")
def properties = new Properties()
includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")

assert localPropertiesFile.exists()
localPropertiesFile.withReader("UTF-8") { reader -> properties.load(reader) }
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
}

def flutterSdkPath = properties.getProperty("flutter.sdk")
assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
apply from: "$flutterSdkPath/packages/flutter_tools/gradle/app_plugin_loader.gradle"
plugins {
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
id "com.android.application" version "8.5.2" apply false
id "org.jetbrains.kotlin.android" version "2.0.0" apply false
}

include ":app"
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
11 changes: 6 additions & 5 deletions example/lib/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ 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);
const MyHomePage({super.key});

@override
_MyHomePageState createState() => _MyHomePageState();
State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
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
6 changes: 3 additions & 3 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 All @@ -22,7 +22,7 @@ void main() {
}

class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
const MyApp({super.key});

@override
Widget build(BuildContext context) {
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
Loading