Skip to content

Commit

Permalink
🚸 Store settings preferences on system shared_preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
Cavitedev committed Feb 2, 2021
1 parent f9e48e4 commit 563eba8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
45 changes: 36 additions & 9 deletions lib/application/core/app_manager_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:injectable/injectable.dart';
import 'package:shared_preferences/shared_preferences.dart';

part 'app_manager_cubit.freezed.dart';
part 'app_manager_state.dart';
Expand All @@ -12,24 +13,50 @@ class AppManagerCubit extends Cubit<AppManagerState> {
AppManagerCubit() : super(AppManagerState.initial());


void changeThemeData(ThemeMode themeMode){
emit(state.copyWith(themeMode: themeMode));
Future<void> changeThemeData(ThemeMode themeMode) async {

await _getPrefs().then((prefs) => prefs.setInt("theme", themeMode.index));
_setThemeData(themeMode);
}

void _setThemeData(ThemeMode themeMode) {
emit(state.copyWith(themeMode: themeMode));
}

Future<void> getSystemRegion() async {
const channel = MethodChannel("com.cavitedev.scorecontacts/region");
final String region = await channel.invokeMethod("getSystemRegion");
emit(state.copyWith(region:region));
Future<void> changeLanguage(String languageCode) async {
await _getPrefs().then((prefs) => prefs.setString("language", languageCode));
_setLanguageCode(languageCode);
}

Future<void> changeLanguage(BuildContext context, String languageCode) async {
if(languageCode == "system"){
void _setLanguageCode(String languageCode) {
if (languageCode == "system") {
emit(state.copyWith(languageCode: null));
}else{
} else {
emit(state.copyWith(languageCode: languageCode));
}
}

Future<void> init() async {
_getSystemRegion();
_readPreferences();
}

Future<void> _getSystemRegion() async {
const channel = MethodChannel("com.cavitedev.scorecontacts/region");
final String region = await channel.invokeMethod("getSystemRegion");
emit(state.copyWith(region: region));
}

Future<void> _readPreferences() async {
final prefs = await _getPrefs();
final int themeIndex = prefs.getInt('theme') ?? ThemeMode.system.index;
_setThemeData(ThemeMode.values[themeIndex]);
final String language = prefs.getString('language') ?? "system";
_setLanguageCode(language);
}

SharedPreferences _prefs;

Future<SharedPreferences> _getPrefs() async =>
_prefs == null ? SharedPreferences.getInstance() : Future.value(_prefs);
}
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class MyApp extends StatelessWidget {
getIt<AuthBloc>()..add(const AuthEvent.getUser()),
),
BlocProvider(
create: (context) => getIt<AppManagerCubit>()..getSystemRegion())
create: (context) => getIt<AppManagerCubit>()..init())
],
child: BlocBuilder<AppManagerCubit, AppManagerState>(
builder: (context, state) => MaterialApp(
Expand Down
2 changes: 1 addition & 1 deletion lib/presentation/settings/change_language_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class RadioLanguageColumn extends StatelessWidget {
title: Text(msg),
onChanged: (_) {
Navigator.pop(context);
context.read<AppManagerCubit>().changeLanguage(context, value);
context.read<AppManagerCubit>().changeLanguage(value);
},
);
}
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies:
cloud_firestore: ^0.16.0
google_sign_in: ^4.5.9
flutter_bloc: ^6.1.1
shared_preferences: ^0.5.12+4
dartz: ^0.9.2
injectable: ^1.0.7
get_it: ^5.0.6
Expand Down

0 comments on commit 563eba8

Please sign in to comment.