-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add: theme data persist using shared_preferences
- Loading branch information
1 parent
15e9b51
commit c2f7afb
Showing
12 changed files
with
210 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
abstract class ThemeRepository { | ||
Future<void> saveTheme(bool isDarkTheme); | ||
Future<bool> loadTheme(); | ||
} |
18 changes: 18 additions & 0 deletions
18
lib/features/infrastructure/datasources/theme_local_datasource.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import 'package:injectable/injectable.dart'; | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
|
||
@injectable | ||
class ThemeLocalDataSource { | ||
final SharedPreferences sharedPreferences; | ||
static const String _themeKey = 'THEME_MODE'; | ||
|
||
ThemeLocalDataSource(this.sharedPreferences); | ||
|
||
Future<void> cacheTheme(bool isDarkTheme) async { | ||
await sharedPreferences.setBool(_themeKey, isDarkTheme); | ||
} | ||
|
||
Future<bool> getTheme() async { | ||
return sharedPreferences.getBool(_themeKey) ?? false; // false for light theme | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
lib/features/infrastructure/repositories/theme_repository_impl.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import 'package:injectable/injectable.dart'; | ||
|
||
import '../../domain/repositories/theme_repository.dart'; | ||
import '../datasources/theme_local_datasource.dart'; | ||
|
||
@LazySingleton(as: ThemeRepository) | ||
class ThemeRepositoryImpl implements ThemeRepository { | ||
final ThemeLocalDataSource localDataSource; | ||
|
||
ThemeRepositoryImpl(this.localDataSource); | ||
|
||
@override | ||
Future<void> saveTheme(bool isDarkTheme) async { | ||
await localDataSource.cacheTheme(isDarkTheme); | ||
} | ||
|
||
@override | ||
Future<bool> loadTheme() async { | ||
return await localDataSource.getTheme(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import 'package:injectable/injectable.dart'; | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
|
||
@module | ||
abstract class InjectableModules { | ||
@preResolve | ||
Future<SharedPreferences> get prefs => SharedPreferences.getInstance(); | ||
} |
Oops, something went wrong.