Skip to content

Commit

Permalink
Shave shared_preferences in favour of hive
Browse files Browse the repository at this point in the history
  • Loading branch information
MillerAdulu committed Jul 30, 2024
1 parent 8705639 commit b5524f2
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 189 deletions.
6 changes: 2 additions & 4 deletions lib/app.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:fluttercon/common/repository/hive_repository.dart';
import 'package:fluttercon/common/utils/router.dart';
import 'package:fluttercon/core/di/injectable.dart';
import 'package:fluttercon/core/local_storage.dart';
import 'package:fluttercon/core/theme/bloc/theme_bloc.dart';
import 'package:fluttercon/core/theme/theme_data.dart';
import 'package:fluttercon/l10n/l10n.dart';
Expand All @@ -18,16 +18,14 @@ class MyApp extends StatefulWidget {
class MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
final localStorage = getIt<LocalStorage>();

return BlocProvider(
create: (context) => ThemeBloc(),
child: BlocBuilder<ThemeBloc, ThemeMode>(
builder: (context, themeMode) {
return Sizer(
builder: (context, orientation, deviceType) {
return MaterialApp.router(
themeMode: localStorage.getThemeMode(),
themeMode: getIt<HiveRepository>().retrieveThemeMode(),
theme: AppTheme.lightTheme(),
darkTheme: AppTheme.darkTheme(),
debugShowCheckedModeBanner: false,
Expand Down
21 changes: 21 additions & 0 deletions lib/common/repository/hive_repository.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter/material.dart';
import 'package:fluttercon/common/data/models/adapters.dart';
import 'package:fluttercon/common/data/models/auth.dart';
import 'package:fluttercon/common/utils/env/flavor_config.dart';
Expand Down Expand Up @@ -45,4 +46,24 @@ class HiveRepository {
return Hive.box<dynamic>(FlutterConConfig.instance!.values.hiveBox)
.get('profile') as FlutterConUser?;
}

void persistThemeMode(ThemeMode themeMode) {
Hive.box<dynamic>(FlutterConConfig.instance!.values.hiveBox)
.put('themeMode', themeMode.toString());
}

ThemeMode retrieveThemeMode() {
final themeMode =
Hive.box<dynamic>(FlutterConConfig.instance!.values.hiveBox)
.get('themeMode') as String?;

if (themeMode == null) {
return ThemeMode.system;
}

return ThemeMode.values.firstWhere(
(element) => element.toString() == themeMode,
orElse: () => ThemeMode.system,
);
}
}
3 changes: 0 additions & 3 deletions lib/common/widgets/app_bar/app_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import 'package:flutter/material.dart';
import 'package:fluttercon/common/utils/constants/app_assets.dart';
import 'package:fluttercon/common/widgets/app_bar/feedback_button.dart';
import 'package:fluttercon/common/widgets/app_bar/user_profile_icon.dart';
import 'package:fluttercon/core/di/injectable.dart';
import 'package:fluttercon/core/local_storage.dart';

class CustomAppBar extends StatefulWidget {
const CustomAppBar({required this.selectedIndex, super.key});
Expand All @@ -15,7 +13,6 @@ class CustomAppBar extends StatefulWidget {

class _CustomBottomNavigationBarState extends State<CustomAppBar> {
/// This is used for the swipe drag gesture on the bottom nav bar
LocalStorage localStorage = getIt<LocalStorage>();
@override
Widget build(BuildContext context) {
Expand Down
4 changes: 1 addition & 3 deletions lib/common/widgets/bottom_nav/bottom_nav_bar.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import 'package:flutter/material.dart';
import 'package:fluttercon/common/widgets/bottom_nav/app_nav_icon.dart';
import 'package:fluttercon/common/widgets/page_item.dart';
import 'package:fluttercon/core/di/injectable.dart';
import 'package:fluttercon/core/local_storage.dart';
import 'package:fluttercon/core/theme/theme_colors.dart';

/// Custom Bottom Navigation Bar that will handles the page to be displayed on
Expand All @@ -26,7 +24,7 @@ class CustomBottomNavigationBar extends StatefulWidget {

class _CustomBottomNavigationBarState extends State<CustomBottomNavigationBar> {
/// This is used for the swipe drag gesture on the bottom nav bar
LocalStorage localStorage = getIt<LocalStorage>();
bool bottomNavBarSwipeGestures = false;
bool bottomNavBarDoubleTapGestures = false;

Expand Down
13 changes: 2 additions & 11 deletions lib/core/di/injectable.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:fluttercon/core/di/injectable.config.dart';
import 'package:get_it/get_it.dart';
import 'package:injectable/injectable.dart';
import 'package:shared_preferences/shared_preferences.dart';

final getIt = GetIt.instance;

Expand All @@ -14,19 +12,12 @@ final getIt = GetIt.instance;
generateForDir: ['lib'],
)
Future<void> configureDependencies() async {
await getIt.initGetIt();
getIt.initGetIt();
await getIt.allReady();
}

@module
abstract class RegisterModule {
@singleton
@preResolve
Future<SharedPreferences> prefs() => SharedPreferences.getInstance();

@singleton
Dio dio() => Dio();
}
abstract class RegisterModule {}

dynamic _parseAndDecode(String response) => jsonDecode(response);

Expand Down
111 changes: 0 additions & 111 deletions lib/core/local_storage.dart

This file was deleted.

56 changes: 0 additions & 56 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -965,62 +965,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.28.0"
shared_preferences:
dependency: "direct main"
description:
name: shared_preferences
sha256: d3bbe5553a986e83980916ded2f0b435ef2e1893dfaa29d5a7a790d0eca12180
url: "https://pub.dev"
source: hosted
version: "2.2.3"
shared_preferences_android:
dependency: transitive
description:
name: shared_preferences_android
sha256: "1ee8bf911094a1b592de7ab29add6f826a7331fb854273d55918693d5364a1f2"
url: "https://pub.dev"
source: hosted
version: "2.2.2"
shared_preferences_foundation:
dependency: transitive
description:
name: shared_preferences_foundation
sha256: "0a8a893bf4fd1152f93fec03a415d11c27c74454d96e2318a7ac38dd18683ab7"
url: "https://pub.dev"
source: hosted
version: "2.4.0"
shared_preferences_linux:
dependency: transitive
description:
name: shared_preferences_linux
sha256: "9f2cbcf46d4270ea8be39fa156d86379077c8a5228d9dfdb1164ae0bb93f1faa"
url: "https://pub.dev"
source: hosted
version: "2.3.2"
shared_preferences_platform_interface:
dependency: transitive
description:
name: shared_preferences_platform_interface
sha256: "22e2ecac9419b4246d7c22bfbbda589e3acf5c0351137d87dd2939d984d37c3b"
url: "https://pub.dev"
source: hosted
version: "2.3.2"
shared_preferences_web:
dependency: transitive
description:
name: shared_preferences_web
sha256: "9aee1089b36bd2aafe06582b7d7817fd317ef05fc30e6ba14bff247d0933042a"
url: "https://pub.dev"
source: hosted
version: "2.3.0"
shared_preferences_windows:
dependency: transitive
description:
name: shared_preferences_windows
sha256: "841ad54f3c8381c480d0c9b508b89a34036f512482c407e6df7a9c4aa2ef8f59"
url: "https://pub.dev"
source: hosted
version: "2.3.2"
shelf:
dependency: transitive
description:
Expand Down
1 change: 0 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ dependencies:
logger: ^2.4.0
pretty_dio_logger: ^1.4.0
rxdart: ^0.28.0
shared_preferences: ^2.2.3
sizer: ^2.0.15
stream_transform: ^2.1.0
styled_widget: ^0.4.1
Expand Down

0 comments on commit b5524f2

Please sign in to comment.