From 563eba8c5a680904c940bac1c89400d335a397f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20V=C3=ADllora=20Tercero?= Date: Tue, 2 Feb 2021 15:17:17 +0100 Subject: [PATCH] :children_crossing: Store settings preferences on system shared_preferences --- lib/application/core/app_manager_cubit.dart | 45 +++++++++++++++---- lib/main.dart | 2 +- .../settings/change_language_widget.dart | 2 +- pubspec.yaml | 1 + 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/lib/application/core/app_manager_cubit.dart b/lib/application/core/app_manager_cubit.dart index e3dd7a3..587bcd6 100644 --- a/lib/application/core/app_manager_cubit.dart +++ b/lib/application/core/app_manager_cubit.dart @@ -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'; @@ -12,24 +13,50 @@ class AppManagerCubit extends Cubit { AppManagerCubit() : super(AppManagerState.initial()); - void changeThemeData(ThemeMode themeMode){ - emit(state.copyWith(themeMode: themeMode)); + Future 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 getSystemRegion() async { - const channel = MethodChannel("com.cavitedev.scorecontacts/region"); - final String region = await channel.invokeMethod("getSystemRegion"); - emit(state.copyWith(region:region)); + Future changeLanguage(String languageCode) async { + await _getPrefs().then((prefs) => prefs.setString("language", languageCode)); + _setLanguageCode(languageCode); } - Future 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 init() async { + _getSystemRegion(); + _readPreferences(); } + Future _getSystemRegion() async { + const channel = MethodChannel("com.cavitedev.scorecontacts/region"); + final String region = await channel.invokeMethod("getSystemRegion"); + emit(state.copyWith(region: region)); + } + + Future _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 _getPrefs() async => + _prefs == null ? SharedPreferences.getInstance() : Future.value(_prefs); } diff --git a/lib/main.dart b/lib/main.dart index 9640f28..9ca50c2 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -29,7 +29,7 @@ class MyApp extends StatelessWidget { getIt()..add(const AuthEvent.getUser()), ), BlocProvider( - create: (context) => getIt()..getSystemRegion()) + create: (context) => getIt()..init()) ], child: BlocBuilder( builder: (context, state) => MaterialApp( diff --git a/lib/presentation/settings/change_language_widget.dart b/lib/presentation/settings/change_language_widget.dart index 06215ab..abe5417 100644 --- a/lib/presentation/settings/change_language_widget.dart +++ b/lib/presentation/settings/change_language_widget.dart @@ -90,7 +90,7 @@ class RadioLanguageColumn extends StatelessWidget { title: Text(msg), onChanged: (_) { Navigator.pop(context); - context.read().changeLanguage(context, value); + context.read().changeLanguage(value); }, ); } diff --git a/pubspec.yaml b/pubspec.yaml index 30522fa..3c02d5b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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