Settings to disable face recognition support

This commit is contained in:
Ming Ming 2021-10-20 04:53:31 +08:00
parent 75262a2a9b
commit bf5845f832
7 changed files with 104 additions and 8 deletions

View file

@ -73,26 +73,42 @@ class Account with EquatableMixin {
}
class AccountSettings with EquatableMixin {
const AccountSettings();
const AccountSettings({
this.isEnableFaceRecognitionApp = true,
});
factory AccountSettings.fromJson(JsonObj json) {
return AccountSettings();
return AccountSettings(
isEnableFaceRecognitionApp: json["isEnableFaceRecognitionApp"] ?? true,
);
}
JsonObj toJson() => {};
JsonObj toJson() => {
"isEnableFaceRecognitionApp": isEnableFaceRecognitionApp,
};
@override
toString() {
return "$runtimeType {"
"isEnableFaceRecognitionApp: $isEnableFaceRecognitionApp, "
"}";
}
AccountSettings copyWith() {
return AccountSettings();
AccountSettings copyWith({
bool? isEnableFaceRecognitionApp,
}) {
return AccountSettings(
isEnableFaceRecognitionApp:
isEnableFaceRecognitionApp ?? this.isEnableFaceRecognitionApp,
);
}
@override
get props => [];
get props => [
isEnableFaceRecognitionApp,
];
final bool isEnableFaceRecognitionApp;
}
extension AccountExtension on Account {

View file

@ -322,6 +322,10 @@
"@settingsIncludedFoldersTitle": {
"description": "Change the included folders of an account"
},
"settingsServerAppSectionTitle": "Server app support",
"@settingsServerAppSectionTitle": {
"description": "Enable/disable various server apps"
},
"settingsViewerTitle": "Viewer",
"settingsViewerDescription": "Customize the image/video viewer",
"settingsViewerPageTitle": "Viewer settings",

View file

@ -3,6 +3,7 @@
"settingsAccountTitle",
"settingsAccountPageTitle",
"settingsIncludedFoldersTitle",
"settingsServerAppSectionTitle",
"settingsAlbumTitle",
"settingsAlbumDescription",
"settingsAlbumPageTitle",
@ -45,6 +46,7 @@
"settingsAccountTitle",
"settingsAccountPageTitle",
"settingsIncludedFoldersTitle",
"settingsServerAppSectionTitle",
"settingsAlbumTitle",
"settingsAlbumDescription",
"settingsAlbumPageTitle",
@ -102,6 +104,7 @@
"settingsAccountTitle",
"settingsAccountPageTitle",
"settingsIncludedFoldersTitle",
"settingsServerAppSectionTitle",
"settingsViewerTitle",
"settingsViewerDescription",
"settingsViewerPageTitle",
@ -212,6 +215,7 @@
"settingsAccountTitle",
"settingsAccountPageTitle",
"settingsIncludedFoldersTitle",
"settingsServerAppSectionTitle",
"settingsAlbumTitle",
"settingsAlbumDescription",
"settingsAlbumPageTitle",
@ -255,6 +259,7 @@
"settingsAccountTitle",
"settingsAccountPageTitle",
"settingsIncludedFoldersTitle",
"settingsServerAppSectionTitle",
"settingsViewerTitle",
"settingsViewerDescription",
"settingsViewerPageTitle",
@ -345,6 +350,7 @@
"settingsAccountTitle",
"settingsAccountPageTitle",
"settingsIncludedFoldersTitle",
"settingsServerAppSectionTitle",
"settingsAlbumTitle",
"settingsAlbumDescription",
"settingsAlbumPageTitle",

View file

@ -284,4 +284,11 @@ extension PrefExtension on Pref {
return null;
}
}
AccountSettings getAccountSettings(Account account) {
return Pref.inst()
.getAccounts2()!
.firstWhere((element) => element.account == account)
.settings;
}
}

View file

@ -22,7 +22,9 @@ class CompatV32 {
for (final j in jsons) {
newJsons.add(<String, dynamic>{
"account": jsonDecode(j),
"settings": <String, dynamic>{},
"settings": <String, dynamic>{
"isEnableFaceRecognitionApp": true,
},
});
}
if (await pref.setStringList(

View file

@ -442,7 +442,11 @@ class _HomeAlbumsState extends State<HomeAlbums>
}
}).map((e) => e.item2);
itemStreamListItems = [
_buildPersonItem(context),
if (Pref.inst()
.getAccountSettings(widget.account)
.isEnableFaceRecognitionApp ==
true)
_buildPersonItem(context),
_buildSharingItem(context),
_buildArchiveItem(context),
_buildTrashbinItem(context),

View file

@ -385,6 +385,9 @@ class _AccountSettingsState extends State<AccountSettingsWidget> {
initState() {
super.initState();
_account = widget.account;
final settings = Pref.inst().getAccountSettings(_account);
_isEnableFaceRecognitionApp = settings.isEnableFaceRecognitionApp;
}
@override
@ -420,6 +423,13 @@ class _AccountSettingsState extends State<AccountSettingsWidget> {
subtitle: Text(_account.roots.map((e) => "/$e").join("; ")),
onTap: _onIncludedFoldersPressed,
),
_buildCaption(
context, L10n.global().settingsServerAppSectionTitle),
SwitchListTile(
title: const Text("Face Recognition"),
value: _isEnableFaceRecognitionApp,
onChanged: _onEnableFaceRecognitionAppChanged,
),
],
),
),
@ -496,6 +506,52 @@ class _AccountSettingsState extends State<AccountSettingsWidget> {
}
}
Future<void> _onEnableFaceRecognitionAppChanged(bool value) async {
final oldValue = _isEnableFaceRecognitionApp;
setState(() {
_isEnableFaceRecognitionApp = value;
});
if (!await _modifyAccountSettings(
_account,
isEnableFaceRecognitionApp: value,
)) {
_log.severe("[_onEnableFaceRecognitionAppChanged] Failed writing pref");
SnackBarManager().showSnackBar(SnackBar(
content: Text(L10n.global().writePreferenceFailureNotification),
duration: k.snackBarDurationNormal,
));
setState(() {
_isEnableFaceRecognitionApp = oldValue;
});
} else {
setState(() {
_hasModified = true;
});
}
}
static Future<bool> _modifyAccountSettings(
Account account, {
bool? isEnableFaceRecognitionApp,
}) {
try {
final accounts = Pref.inst().getAccounts2()!;
final index = _findAccount(account, accounts);
accounts[index] = accounts[index].copyWith(
settings: accounts[index].settings.copyWith(
isEnableFaceRecognitionApp: isEnableFaceRecognitionApp,
),
);
return Pref.inst().setAccounts2(accounts);
} catch (e, stackTrace) {
_log.severe(
"[_modifyAccountSettings] Failed while setting account settings",
e,
stackTrace);
return Future.value(false);
}
}
/// Return the index of [account] in [Pref.getAccounts2]
static int _findAccount(Account account, [List<PrefAccount>? accounts]) {
final from = accounts ?? Pref.inst().getAccounts2Or([]);
@ -504,6 +560,7 @@ class _AccountSettingsState extends State<AccountSettingsWidget> {
bool _hasModified = false;
late Account _account;
late bool _isEnableFaceRecognitionApp;
static final _log = Logger("widget.settings._AccountSettingsState");
}