Skip to content

Commit

Permalink
Add local storage: sharedpreferences or hive
Browse files Browse the repository at this point in the history
  • Loading branch information
Truong Nhat Duy committed Oct 17, 2022
1 parent b3636a4 commit 2986d8a
Show file tree
Hide file tree
Showing 15 changed files with 333 additions and 19 deletions.
6 changes: 6 additions & 0 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ PODS:
- FMDB (2.7.5):
- FMDB/standard (= 2.7.5)
- FMDB/standard (2.7.5)
- shared_preferences_ios (0.0.1):
- Flutter
- sqflite (0.0.2):
- Flutter
- FMDB (>= 2.7.5)

DEPENDENCIES:
- Flutter (from `Flutter`)
- shared_preferences_ios (from `.symlinks/plugins/shared_preferences_ios/ios`)
- sqflite (from `.symlinks/plugins/sqflite/ios`)

SPEC REPOS:
Expand All @@ -18,12 +21,15 @@ SPEC REPOS:
EXTERNAL SOURCES:
Flutter:
:path: Flutter
shared_preferences_ios:
:path: ".symlinks/plugins/shared_preferences_ios/ios"
sqflite:
:path: ".symlinks/plugins/sqflite/ios"

SPEC CHECKSUMS:
Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854
FMDB: 2ce00b547f966261cd18927a3ddb07cb6f3db82a
shared_preferences_ios: 548a61f8053b9b8a49ac19c1ffbc8b92c50d68ad
sqflite: 6d358c025f5b867b29ed92fc697fd34924e11904

PODFILE CHECKSUM: ef19549a9bc3046e7bb7d2fab4d021637c0c58a3
Expand Down
9 changes: 4 additions & 5 deletions ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@
<key>UIApplicationSupportsIndirectInputEvents</key>
<true/>
<key>CFBundleLocalizations</key>
<array>
<string>ar</string>
<string>en</string>
<string>es</string>
</array>
<array>
<string>vi</string>
<string>en</string>
</array>
</dict>
</plist>
2 changes: 2 additions & 0 deletions lib/configs/app_config.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class AppConfig {
static String baseUrl = '';

static const String defaultLocale = 'en';

static void configDev() {
baseUrl = 'https://dog.ceo/api';
}
Expand Down
26 changes: 23 additions & 3 deletions lib/features/application/bloc/application_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:boilerplate/generated/l10n.dart';
import 'package:boilerplate/services/local_storage_service/local_storage_service.dart';
import 'package:flutter/material.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:meta/meta.dart';
Expand All @@ -11,12 +12,17 @@ part 'application_state.dart';
part 'application_bloc.freezed.dart';

class ApplicationBloc extends Bloc<ApplicationEvent, ApplicationState> {
ApplicationBloc() : super(const ApplicationState()) {
ApplicationBloc({
required LocalStorageService localStorageService,
}) : super(const ApplicationState()) {
_localStorageService = localStorageService;
on<ApplicationLoaded>(_onLoaded);
on<ApplicationLocaleChanged>(_onLocaleChanged);
on<ApplicationDarkModeChanged>(_onDarkModeChanged);
}

late LocalStorageService _localStorageService;

FutureOr<void> _onLoaded(
ApplicationLoaded event,
Emitter<ApplicationState> emit,
Expand All @@ -25,9 +31,13 @@ class ApplicationBloc extends Bloc<ApplicationEvent, ApplicationState> {
status: UIStatus.loading,
));

final String locale = _localStorageService.locale;
final bool isDarkMode = _localStorageService.isDarkMode;

emit(state.copyWith(
status: UIStatus.loadSuccess,
locale: 'en',
locale: locale,
isDarkMode: isDarkMode,
));
}

Expand All @@ -40,6 +50,9 @@ class ApplicationBloc extends Bloc<ApplicationEvent, ApplicationState> {
status: UIStatus.loading,
));
await S.load(Locale(event.locale));

_localStorageService.setLocale(event.locale);

emit(state.copyWith(
status: UIStatus.loadSuccess,
locale: event.locale,
Expand All @@ -50,9 +63,16 @@ class ApplicationBloc extends Bloc<ApplicationEvent, ApplicationState> {
FutureOr<void> _onDarkModeChanged(
ApplicationDarkModeChanged event,
Emitter<ApplicationState> emit,
) {
) async {
if (state.isDarkMode != event.enable) {
emit(state.copyWith(
status: UIStatus.loading,
));

_localStorageService.setIsDarkMode(event.enable);

emit(state.copyWith(
status: UIStatus.loadSuccess,
isDarkMode: event.enable,
));
}
Expand Down
2 changes: 1 addition & 1 deletion lib/features/application/config_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class _ConfigPageState extends State<ConfigPage> {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(S.current.config),
title: Text(S.of(context).config),
),
body: Padding(
padding: const EdgeInsets.all(AppDimens.basePadding),
Expand Down
11 changes: 5 additions & 6 deletions lib/features/application/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class HomePage extends StatefulWidget {
}

class _HomePageState extends State<HomePage> {

@override
void didChangeDependencies() {
super.didChangeDependencies();
Expand All @@ -22,34 +21,34 @@ class _HomePageState extends State<HomePage> {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(S.current.go_route),
title: Text(S.of(context).go_route),
),
body: Center(
child: Column(
children: [
ElevatedButton(
child: Text(S.current.dog_image_random),
child: Text(S.of(context).dog_image_random),
onPressed: () {
context.push(AppRouter.dogImageRandomPath);
},
),
AppSpacing.verticalSpacing32,
ElevatedButton(
child: Text(S.current.config),
child: Text(S.of(context).config),
onPressed: () {
context.push(AppRouter.config);
},
),
AppSpacing.verticalSpacing32,
ElevatedButton(
child: Text(S.current.assets),
child: Text(S.of(context).assets),
onPressed: () {
context.push(AppRouter.assets);
},
),
AppSpacing.verticalSpacing32,
ElevatedButton(
child: Text(S.current.image_from_db),
child: Text(S.of(context).image_from_db),
onPressed: () {
context.push(AppRouter.imagesFromDb);
},
Expand Down
3 changes: 2 additions & 1 deletion lib/injector/bloc_module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class BlocModule {
static void init() {
final injector = Injector.instance;

injector.registerFactory<ApplicationBloc>(() => ApplicationBloc());
injector.registerSingleton<ApplicationBloc>(
ApplicationBloc(localStorageService: injector()));

injector.registerFactory<DogImageRandomBloc>(
() => DogImageRandomBloc(
Expand Down
7 changes: 7 additions & 0 deletions lib/injector/service_module.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:boilerplate/services/crashlytics_service/crashlytics_service.dart';
import 'package:boilerplate/services/crashlytics_service/firebase_crashlytics_service.dart';
import 'package:boilerplate/services/local_storage_service/local_storage_service.dart';
import 'package:boilerplate/services/local_storage_service/shared_preferences_service.dart';
import 'package:boilerplate/services/log_service/debug_log_service.dart';
import 'package:boilerplate/services/log_service/log_service.dart';

Expand All @@ -16,5 +18,10 @@ class ServiceModule {
});

injector.registerFactory<LogService>(() => DebugLogService());

injector.registerSingleton<LocalStorageService>(
SharedPreferencesService(),
signalsReady: true,
);
}
}
4 changes: 2 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import 'package:logger/logger.dart';

import 'my_app.dart';

void main() {
runZonedGuarded(() async {
Future<void> main() async {
await runZonedGuarded(() async {
WidgetsFlutterBinding.ensureInitialized();

/// Firebase initialize ...
Expand Down
3 changes: 2 additions & 1 deletion lib/my_app.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:boilerplate/common/app_themes.dart';
import 'package:boilerplate/features/application/bloc/application_bloc.dart';
import 'package:boilerplate/generated/l10n.dart';
import 'package:boilerplate/injector/injector.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
Expand All @@ -23,7 +24,7 @@ class _MyAppState extends State<MyApp> {
@override
void initState() {
_locale = 'en';
_bloc = ApplicationBloc();
_bloc = Injector.instance<ApplicationBloc>();;
_bloc.add(const ApplicationLoaded());
_isDarkMode = false;
super.initState();
Expand Down
75 changes: 75 additions & 0 deletions lib/services/local_storage_service/hive_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import 'dart:async';

import 'package:boilerplate/services/local_storage_service/shared_preferences_service.dart';

/// TODO: modify this if you use Hive packages
class HiveService implements SharedPreferencesService {
@override
String isDarkModeKey = 'hiveIsDarkModeKey';

@override
String isFirstUseKey = 'hiveIsFirstUseKey';

@override
String localeKey = 'hiveLocaleKey';

@override
String tokenKey = 'hiveTokenKey';

@override
getValue({required String key}) {
// TODO: implement getValue
throw UnimplementedError();
}

@override
FutureOr<void> init() {
// TODO: implement init
throw UnimplementedError();
}

@override
// TODO: implement isDarkMode
bool get isDarkMode => throw UnimplementedError();

@override
// TODO: implement isFirstUse
bool get isFirstUse => throw UnimplementedError();

@override
// TODO: implement locale
String get locale => throw UnimplementedError();

@override
Future<bool> setIsDarkMode(bool isDarkMode) {
// TODO: implement setIsDarkMode
throw UnimplementedError();
}

@override
Future<bool> setIsFirstUse(bool isFirstUse) {
// TODO: implement setIsFirstUse
throw UnimplementedError();
}

@override
Future<bool> setLocale(String locale) {
// TODO: implement setLocale
throw UnimplementedError();
}

@override
Future<bool> setToken(String token) {
// TODO: implement setToken
throw UnimplementedError();
}

@override
void setValue({required String key, required value}) {
// TODO: implement setValue
}

@override
// TODO: implement token
String? get token => throw UnimplementedError();
}
23 changes: 23 additions & 0 deletions lib/services/local_storage_service/local_storage_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'dart:async';

abstract class LocalStorageService {
String tokenKey = '';
String localeKey = '';
String isDarkModeKey = '';
String isFirstUseKey = '';

FutureOr<void> init();
String? get token;
String get locale;
bool get isDarkMode;

bool get isFirstUse;

Future<bool> setToken(String token);
Future<bool> setLocale(String locale);
Future<bool> setIsDarkMode(bool isDarkMode);
Future<bool> setIsFirstUse(bool isFirstUse);

void setValue({required String key, required dynamic value});
dynamic getValue({required String key});
}
Loading

0 comments on commit 2986d8a

Please sign in to comment.