Skip to content

Commit

Permalink
[release] v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mym0404 committed Oct 14, 2023
1 parent 4db71b3 commit 541dc32
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.0.0

- Make `LocalFilePrefMixin` implement `ValueNotifier`
- Publish 1.0.0!

## 0.0.6

- Add example
Expand Down
4 changes: 4 additions & 0 deletions doc/docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
sidebar_position: 2
---

# Note

- If a class mixed in with `LocalFilePrefMixin`, then it can be used as `ValueNotifier`. `LocalFilePrevMixin` implements all `ValueNotifier` methods with internal `ValueNotifier` delegate.

# Usage

## 1. Register global `Storage`
Expand Down
20 changes: 12 additions & 8 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class LocalFileCounter with LocalFilePrefMixin<int> {
Map<String, dynamic> toJson() {
return {'value': value};
}

@override
Duration get throttleDuration => const Duration(milliseconds: 1);
}

class MyApp extends StatelessWidget {
Expand Down Expand Up @@ -53,10 +56,7 @@ class _MyHomePageState extends State<MyHomePage> {
final _counter = LocalFileCounter();

void _incrementCounter() {
setState(() {
_counter.value += 1;
});
_counter.scheduleSave();
_counter.value += 1;
}

@override
Expand All @@ -73,10 +73,14 @@ class _MyHomePageState extends State<MyHomePage> {
const Text(
'You have pushed the button this many times:',
),
Text(
'${_counter.value}',
style: Theme.of(context).textTheme.headlineMedium,
),
ValueListenableBuilder(
valueListenable: _counter,
builder: (context, value, _) {
return Text(
'$value',
style: Theme.of(context).textTheme.headlineMedium,
);
}),
],
),
),
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.0.6"
version: "1.0.0"
matcher:
dependency: transitive
description:
Expand Down
40 changes: 33 additions & 7 deletions lib/src/local_file_pref_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@ import 'storage/storage.dart';
import 'throttler.dart';
import 'util/register.dart';

mixin LocalFilePrefMixin<T> {
mixin LocalFilePrefMixin<T> implements ValueNotifier<T> {
late final JsonFile _file = JsonFile(fileName, storage);
final Throttler _throttle = Throttler(const Duration(seconds: 2));
late final Throttler _throttle = Throttler(throttleDuration);
late final ValueNotifier<T> _notifier = ValueNotifier<T>(load());

late final ValueNotifier<T> data = ValueNotifier<T>(load())
..addListener(scheduleSave);
T get value => data.value;
set value(T value) => data.value = value;
@override
T get value => _notifier.value;
@override
set value(T value) {
if (_notifier.value == value) {
return;
}
_notifier.value = value;
notifyListeners();
scheduleSave();
}

T load() {
try {
Expand All @@ -38,10 +46,28 @@ mixin LocalFilePrefMixin<T> {

void scheduleSave() => _throttle.call(save);

/// Serialization
Storage get storage => globalStorage!;
String get fileName;
T get fallback;
Map<String, dynamic> toJson();
T fromJson(Map<String, dynamic> json);
Duration get throttleDuration => const Duration(seconds: 2);

@override
void dispose() {
_notifier.dispose();
}

@override
void addListener(VoidCallback listener) => _notifier.addListener(listener);

@override
bool get hasListeners => _notifier.hasListeners;

@override
void notifyListeners() => _notifier.notifyListeners();

@override
void removeListener(VoidCallback listener) =>
_notifier.removeListener(listener);
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: local_file_preferences
description: This package provides local json file saving and Loading utility.
version: 0.0.6
version: 1.0.0
homepage: https://mj-studio-library.github.io/flutter-local-file-preferences/
repository: https://github.com/mj-studio-library/flutter-local-file-preferences

Expand Down
4 changes: 3 additions & 1 deletion tool/publish/publish.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ git tag $tag
git push origin $tag
git add .
git commit -m "[release] $tag" --allow-empty
git push
git push

cd doc && yarn deploy

0 comments on commit 541dc32

Please sign in to comment.