Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Issue#69] Update Preference page UI + clean up preference code reduce boilerplate #72

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 19 additions & 25 deletions lib/data/datasource/account_preference_local_data_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,26 @@ class AccountPreferenceLocalDataSourceImpl
@override
Future<AccountPreferenceEntity> getAccountPrefs() async {
final sharedPrefs = await _prefs;
_allowSearchAccName =
sharedPrefs.getBool(AccountPreferenceEntity.keyAllowSearchAccName) ??
AccountPreference.allowSearchAccNameDefault;

return AccountPreferenceEntity(
requireLogin:
sharedPrefs.getBool(AccountPreferenceEntity.keyRequireLogin) ??
AccountPreference.requireLoginDefault,
enableDarkMode:
sharedPrefs.getBool(AccountPreferenceEntity.keyEnableDarkMode) ??
AccountPreference.enableDarkModeDefault,
languageCode:
sharedPrefs.getString(AccountPreferenceEntity.keyLanguageCode) ??
AccountPreference.languageCodeDefault,
showAccName:
sharedPrefs.getBool(AccountPreferenceEntity.keyShowAccName) ??
AccountPreference.showAccountNameDefault,
allowSearchAccName: _allowSearchAccName,
requirePassOnForeground: sharedPrefs
.getBool(AccountPreferenceEntity.keyRequirePassOnForeground) ??
AccountPreference.requirePassOnForeground);
items: AppPreferenceEnum.values
.map((e) => AccountPreferenceItem(
name: e, value: getOrDefault(sharedPrefs, e)))
.toList(),
);
}

dynamic getOrDefault(
SharedPreferences sharedPref,
AppPreferenceEnum prefEnum,
) {
return sharedPref.get(prefEnum.name) ?? prefEnum.defaultValue;
}

@override
Future<void> saveRequireLogin(bool require) async {
final sharedPrefs = await _prefs;
sharedPrefs.setBool(AccountPreferenceEntity.keyRequireLogin, require);
sharedPrefs.setBool(AppPreferenceEnum.requireReLogin.name, require);
}

@override
Expand All @@ -71,32 +65,32 @@ class AccountPreferenceLocalDataSourceImpl
@override
Future<void> enableDarkMode(bool enable) async {
final sharedPrefs = await _prefs;
sharedPrefs.setBool(AccountPreferenceEntity.keyEnableDarkMode, enable);
sharedPrefs.setBool(AppPreferenceEnum.enableDarkMode.name, enable);
}

@override
Future<void> setLanguageCode(String value) async {
final sharedPrefs = await _prefs;
sharedPrefs.setString(AccountPreferenceEntity.keyLanguageCode, value);
sharedPrefs.setString(AppPreferenceEnum.languageCode.name, value);
}

@override
Future<void> saveShowAccName(bool value) async {
final sharedPrefs = await _prefs;
sharedPrefs.setBool(AccountPreferenceEntity.keyShowAccName, value);
sharedPrefs.setBool(AppPreferenceEnum.showAccName.name, value);
}

@override
Future<void> saveRequirePassOnForeground(bool value) async {
final sharedPrefs = await _prefs;
sharedPrefs.setBool(AccountPreferenceEntity.keyRequirePassOnForeground, value);
sharedPrefs.setBool(AppPreferenceEnum.requirePassOnForeground.name, value);
}

@override
Future<void> saveAllowSearchAccName(bool value) async {
final sharedPrefs = await _prefs;
_allowSearchAccName = value;
sharedPrefs.setBool(AccountPreferenceEntity.keyAllowSearchAccName, value);
sharedPrefs.setBool(AppPreferenceEnum.allowSearchAccName.name, value);
}

@override
Expand Down
81 changes: 10 additions & 71 deletions lib/data/entity/account_preference_entity.dart
Original file line number Diff line number Diff line change
@@ -1,84 +1,23 @@
import 'package:collection/collection.dart';
import 'package:copy_with_extension/copy_with_extension.dart';
import 'package:flutter_password_saver/domain/model/account_preference.dart';

part 'account_preference_entity.g.dart';

@CopyWith()
class AccountPreferenceEntity {
static const keyRequireLogin = 'requireLogin';
static const keyEnableDarkMode = 'enableDarkMode';
static const keyLanguageCode = 'languageCode';
static const keyShowAccName = 'showAccName';
static const keyRequirePassOnForeground = 'requirePassOnForeground';
static const keyAllowSearchAccName = 'allowSearchAccName';

AccountPreferenceEntity({
required this.requireLogin,
required this.enableDarkMode,
required this.languageCode,
required this.showAccName,
required this.allowSearchAccName,
required this.requirePassOnForeground,
required this.items,
});

final bool requireLogin;
final bool enableDarkMode;
final String languageCode;
final bool showAccName;
final bool allowSearchAccName;
final bool requirePassOnForeground;
final List<AccountPreferenceItem> items;

factory AccountPreferenceEntity.fromAccountPreference(
AccountPreference preference) {
return AccountPreferenceEntity(
// Require login
requireLogin: preference.getItemValue(PreferenceName.requirePass) ??
AccountPreference.requireLoginDefault,
// Dark mode
enableDarkMode: preference.getItemValue(PreferenceName.enableDarkMode) ??
AccountPreference.enableDarkModeDefault,
// Language code
languageCode: preference.getItemValue(PreferenceName.languageCode) ??
AccountPreference.languageCodeDefault,
// Show acc name always
showAccName: preference.getItemValue(PreferenceName.showAccName) ??
AccountPreference.showAccountNameDefault,
// Allow user to search account name too
allowSearchAccName:
preference.getItemValue(PreferenceName.allowSearchAccName) ??
AccountPreference.allowSearchAccNameDefault,
requirePassOnForeground:
preference.getItemValue(PreferenceName.requirePassOnForeground) ??
AccountPreference.requirePassOnForeground,
);
}
AccountPreference toModel() => AccountPreference(items: items);

AccountPreference toModel() => AccountPreference(
items: [
AccountPreferenceItem(
name: PreferenceName.requirePass,
value: requireLogin,
),
AccountPreferenceItem(
name: PreferenceName.enableDarkMode,
value: enableDarkMode,
),
AccountPreferenceItem(
name: PreferenceName.languageCode,
value: languageCode,
),
AccountPreferenceItem(
name: PreferenceName.showAccName,
value: showAccName,
),
AccountPreferenceItem(
name: PreferenceName.allowSearchAccName,
value: allowSearchAccName,
),
AccountPreferenceItem(
name: PreferenceName.requirePassOnForeground,
value: requirePassOnForeground,
),
],
);
dynamic getItemValue(AppPreferenceEnum prefEnum) {
return items
.firstWhereOrNull((element) => element.name == prefEnum)
?.value ??
prefEnum.defaultValue;
}
}
84 changes: 8 additions & 76 deletions lib/data/entity/account_preference_entity.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 10 additions & 8 deletions lib/data/repository/account_pref_repo_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,25 @@ class AccountPrefRepoImpl extends AccountPrefRepo {
}

@override
Future<void> saveAccountPreference(
{required PreferenceName name, required value}) {
Future<void> saveAccountPreference({
required AppPreferenceEnum name,
required value,
}) {
switch (name) {
case PreferenceName.requirePass:
case AppPreferenceEnum.requireReLogin:
return _accountPreferenceLocalDataSource
.saveRequireLogin(value as bool);
case PreferenceName.enableDarkMode:
case AppPreferenceEnum.enableDarkMode:
return _accountPreferenceLocalDataSource.enableDarkMode(value as bool);
case PreferenceName.languageCode:
case AppPreferenceEnum.languageCode:
return _accountPreferenceLocalDataSource
.setLanguageCode(value as String);
case PreferenceName.showAccName:
case AppPreferenceEnum.showAccName:
return _accountPreferenceLocalDataSource.saveShowAccName(value as bool);
case PreferenceName.allowSearchAccName:
case AppPreferenceEnum.allowSearchAccName:
return _accountPreferenceLocalDataSource
.saveAllowSearchAccName(value as bool);
case PreferenceName.requirePassOnForeground:
case AppPreferenceEnum.requirePassOnForeground:
return _accountPreferenceLocalDataSource
.saveRequirePassOnForeground(value as bool);
default:
Expand Down
Loading
Loading