Skip to content

Commit

Permalink
pebble-dev#151 - Refactor existing SharedPreferences setup, too
Browse files Browse the repository at this point in the history
  • Loading branch information
DreadBoy committed May 10, 2021
1 parent 5434cad commit e3ed2a8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
13 changes: 13 additions & 0 deletions lib/infrastructure/datasources/preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ class Preferences {
await _sharedPrefs.setBool("firstRun", true);
_preferencesUpdateStream.add(this);
}

bool wasSetupSuccessful() {
return _sharedPrefs.getBool("bootSetup") ?? false;
}

Future<void> setWasSetupSuccessful(bool value) async {
await _sharedPrefs.setBool("bootSetup", value);
_preferencesUpdateStream.add(this);
}
}

final preferencesProvider = FutureProvider<Preferences>((ref) async {
Expand Down Expand Up @@ -145,6 +154,10 @@ final hasBeenConnectedProvider = _createPreferenceProvider(
(preferences) => preferences.hasBeenConnected(),
);

final wasSetupSuccessfulProvider = _createPreferenceProvider(
(preferences) => preferences.wasSetupSuccessful(),
);

StreamProvider<T> _createPreferenceProvider<T>(
T Function(Preferences preferences) mapper,
) {
Expand Down
16 changes: 8 additions & 8 deletions lib/ui/setup/boot/rebble_setup_fail.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import 'package:cobble/infrastructure/datasources/preferences.dart';
import 'package:cobble/ui/home/home_page.dart';
import 'package:cobble/ui/router/cobble_navigator.dart';
import 'package:cobble/ui/router/cobble_scaffold.dart';
import 'package:cobble/ui/router/cobble_screen.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

class RebbleSetupFail extends StatelessWidget implements CobbleScreen {
class RebbleSetupFail extends HookWidget implements CobbleScreen {
@override
Widget build(BuildContext context) {
final preferences = useProvider(preferencesProvider);
return CobbleScaffold.page(
title: "Activate Rebble services",
child: Column(
Expand All @@ -21,12 +24,9 @@ class RebbleSetupFail extends StatelessWidget implements CobbleScreen {
],
),
floatingActionButton: FloatingActionButton.extended(
onPressed: () {
SharedPreferences.getInstance().then((prefs) {
prefs.setBool("bootSetup", false);
}).then((_) {
context.pushAndRemoveAllBelow(HomePage());
});
onPressed: () async {
await preferences.data?.value.setWasSetupSuccessful(false);
context.pushAndRemoveAllBelow(HomePage());
},
label: Text("OKAY")),
);
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/setup/boot/rebble_setup_success.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class RebbleSetupSuccess extends HookWidget implements CobbleScreen {
onPressed: () {
SharedPreferences.getInstance().then((prefs) async {
await preferences.data?.value.setHasBeenConnected();
prefs.setBool("bootSetup", true);
await preferences.data?.value.setWasSetupSuccessful(true);
}).then((_) {
context.pushAndRemoveAllBelow(HomePage());
});
Expand Down

0 comments on commit e3ed2a8

Please sign in to comment.