feat: Add new translations, add list of existing accounts to login page

This commit is contained in:
Anton Stubenbord
2023-05-30 01:19:27 +02:00
parent cd56f5fdb8
commit 1dc7d22d3a
19 changed files with 694 additions and 381 deletions
@@ -47,9 +47,9 @@ class _SelectFileTypeDialogState extends State<SelectFileTypeDialog> {
value: _rememberSelection,
onChanged: (value) => setState(() => _rememberSelection = value ?? false),
title: Text(
"Remember my decision",
S.of(context)!.rememberDecision,
style: Theme.of(context).textTheme.labelMedium,
), //TODO: INTL
),
),
],
),
@@ -316,7 +316,7 @@ class _DocumentDetailsPageState extends State<DocumentDetailsPage> {
).paddedOnly(right: 4.0),
DocumentShareButton(document: state.document),
IconButton(
tooltip: "Print", //TODO: INTL
tooltip: S.of(context)!.print, //TODO: INTL
onPressed: () => context.read<DocumentDetailsCubit>().printDocument(),
icon: const Icon(Icons.print),
),
@@ -115,10 +115,7 @@ class _DocumentSearchBarState extends State<DocumentSearchBar> {
valueListenable: Hive.box<LocalUserAccount>(HiveBoxes.localUserAccount).listenable(),
builder: (context, box, _) {
final account = box.get(settings.currentLoggedInUser!)!;
return UserAvatar(
userId: settings.currentLoggedInUser!,
account: account,
);
return UserAvatar(account: account);
},
);
},
@@ -140,20 +140,14 @@ class AuthenticationCubit extends Cubit<AuthenticationState> {
}
Future<void> removeAccount(String userId) async {
final globalSettings = Hive.box<GlobalSettings>(HiveBoxes.globalSettings).getValue()!;
final userAccountBox = Hive.box<LocalUserAccount>(HiveBoxes.localUserAccount);
final userAppStateBox = Hive.box<LocalUserAppState>(HiveBoxes.localUserAppState);
final currentUser = globalSettings.currentLoggedInUser;
await userAccountBox.delete(userId);
await userAppStateBox.delete(userId);
await withEncryptedBox(HiveBoxes.localUserCredentials, (box) {
await withEncryptedBox<UserCredentials, void>(HiveBoxes.localUserCredentials, (box) {
box.delete(userId);
});
if (currentUser == userId) {
return logout();
}
}
///
@@ -209,10 +203,10 @@ class AuthenticationCubit extends Cubit<AuthenticationState> {
Future<void> logout() async {
await _resetExternalState();
final globalSettings = Hive.box<GlobalSettings>(HiveBoxes.globalSettings).getValue()!;
emit(const AuthenticationState.unauthenticated());
globalSettings
..currentLoggedInUser = null
..save();
emit(const AuthenticationState.unauthenticated());
}
Future<void> _resetExternalState() async {
+47
View File
@@ -1,5 +1,10 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:hive_flutter/adapters.dart';
import 'package:paperless_mobile/core/config/hive/hive_config.dart';
import 'package:paperless_mobile/core/database/tables/local_user_account.dart';
import 'package:paperless_mobile/features/login/cubit/authentication_cubit.dart';
import 'package:paperless_mobile/features/login/model/client_certificate.dart';
import 'package:paperless_mobile/features/login/model/client_certificate_form_model.dart';
import 'package:paperless_mobile/features/login/model/login_form_credentials.dart';
@@ -7,6 +12,8 @@ import 'package:paperless_mobile/features/login/view/widgets/form_fields/client_
import 'package:paperless_mobile/features/login/view/widgets/form_fields/server_address_form_field.dart';
import 'package:paperless_mobile/features/login/view/widgets/form_fields/user_credentials_form_field.dart';
import 'package:paperless_mobile/features/login/view/widgets/login_pages/server_connection_page.dart';
import 'package:paperless_mobile/features/users/view/widgets/user_account_list_tile.dart';
import 'package:paperless_mobile/generated/l10n/app_localizations.dart';
import 'widgets/login_pages/server_login_page.dart';
import 'widgets/never_scrollable_scroll_behavior.dart';
@@ -23,11 +30,14 @@ class LoginPage extends StatefulWidget {
final String submitText;
final String titleString;
final bool showLocalAccounts;
const LoginPage({
Key? key,
required this.onSubmit,
required this.submitText,
required this.titleString,
this.showLocalAccounts = false,
}) : super(key: key);
@override
@@ -41,6 +51,7 @@ class _LoginPageState extends State<LoginPage> {
@override
Widget build(BuildContext context) {
final localAccounts = Hive.box<LocalUserAccount>(HiveBoxes.localUserAccount);
return Scaffold(
resizeToAvoidBottomInset: false,
body: FormBuilder(
@@ -49,6 +60,42 @@ class _LoginPageState extends State<LoginPage> {
controller: _pageController,
scrollBehavior: NeverScrollableScrollBehavior(),
children: [
if (widget.showLocalAccounts && localAccounts.isNotEmpty)
Scaffold(
appBar: AppBar(
title: Text(S.of(context)!.logInToExistingAccount),
),
bottomNavigationBar: BottomAppBar(
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
FilledButton(
child: Text(S.of(context)!.goToLogin),
onPressed: () {
_pageController.nextPage(
duration: const Duration(milliseconds: 300),
curve: Curves.easeInOut,
);
},
),
],
),
),
body: ListView.builder(
itemBuilder: (context, index) {
final account = localAccounts.values.elementAt(index);
return Card(
child: UserAccountListTile(
account: account,
onTap: () {
context.read<AuthenticationCubit>().switchAccount(account.id);
},
),
);
},
itemCount: localAccounts.length,
),
),
ServerConnectionPage(
titleString: widget.titleString,
formBuilderKey: _formKey,
@@ -12,6 +12,7 @@ import 'package:paperless_mobile/features/settings/view/dialogs/switch_account_d
import 'package:paperless_mobile/features/settings/view/pages/switching_accounts_page.dart';
import 'package:paperless_mobile/features/settings/view/widgets/global_settings_builder.dart';
import 'package:paperless_mobile/features/settings/view/widgets/user_avatar.dart';
import 'package:paperless_mobile/features/users/view/widgets/user_account_list_tile.dart';
import 'package:paperless_mobile/generated/l10n/app_localizations.dart';
import 'package:provider/provider.dart';
@@ -22,6 +23,11 @@ class ManageAccountsPage extends StatelessWidget {
Widget build(BuildContext context) {
return GlobalSettingsBuilder(
builder: (context, globalSettings) {
// This is one of the few places where the currentLoggedInUser can be null
// (exactly after loggin out as the current user to be precise).
if (globalSettings.currentLoggedInUser == null) {
return SizedBox.shrink();
}
return ValueListenableBuilder(
valueListenable: Hive.box<LocalUserAccount>(HiveBoxes.localUserAccount).listenable(),
builder: (context, box, _) {
@@ -46,16 +52,78 @@ class ManageAccountsPage extends StatelessWidget {
borderRadius: BorderRadius.circular(24),
),
children: [
_buildAccountTile(context, globalSettings.currentLoggedInUser!,
box.get(globalSettings.currentLoggedInUser!)!, globalSettings),
Card(
child: UserAccountListTile(
account: box.get(globalSettings.currentLoggedInUser!)!,
trailing: PopupMenuButton(
icon: const Icon(Icons.more_vert),
itemBuilder: (context) => [
PopupMenuItem(
child: ListTile(
title: Text(S.of(context)!.logout),
leading: const Icon(
Icons.person_remove,
color: Colors.red,
),
),
value: 0,
),
],
onSelected: (value) async {
if (value == 0) {
await context
.read<AuthenticationCubit>()
.removeAccount(globalSettings.currentLoggedInUser!);
Navigator.of(context).pop();
context.read<AuthenticationCubit>().logout();
}
},
),
),
),
Column(
children: [
for (int index = 0; index < otherAccounts.length; index++)
_buildAccountTile(
context,
otherAccounts[index],
box.get(otherAccounts[index])!,
globalSettings,
UserAccountListTile(
account: box.get(otherAccounts[index])!,
trailing: PopupMenuButton(
icon: const Icon(Icons.more_vert),
itemBuilder: (context) {
return [
PopupMenuItem(
child: ListTile(
title: Text(S.of(context)!.switchAccount),
leading: const Icon(Icons.switch_account_rounded),
),
value: 0,
),
PopupMenuItem(
child: ListTile(
title: Text(S.of(context)!.remove),
leading: const Icon(
Icons.person_remove,
color: Colors.red,
),
),
value: 1,
)
];
},
onSelected: (value) async {
if (value == 0) {
// Switch
_onSwitchAccount(
context,
globalSettings.currentLoggedInUser!,
otherAccounts[index],
);
} else if (value == 1) {
await context
.read<AuthenticationCubit>()
.removeAccount(otherAccounts[index]);
}
},
),
),
],
),
@@ -68,9 +136,9 @@ class ManageAccountsPage extends StatelessWidget {
},
),
if (context.watch<ApiVersion>().hasMultiUserSupport)
const ListTile(
leading: Icon(Icons.admin_panel_settings),
title: Text("Manage permissions"), //TODO: INTL
ListTile(
leading: const Icon(Icons.admin_panel_settings),
title: Text(S.of(context)!.managePermissions),
),
],
);
@@ -80,93 +148,6 @@ class ManageAccountsPage extends StatelessWidget {
);
}
Widget _buildAccountTile(
BuildContext context,
String userId,
LocalUserAccount account,
GlobalSettings settings,
) {
final isLoggedIn = userId == settings.currentLoggedInUser;
final theme = Theme.of(context);
final child = SizedBox(
width: double.maxFinite,
child: ListTile(
title: Text(account.paperlessUser.username),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (account.paperlessUser.fullName != null) Text(account.paperlessUser.fullName!),
Text(
account.serverUrl.replaceFirst(RegExp(r'https://?'), ''),
style: TextStyle(color: theme.colorScheme.primary),
),
],
),
isThreeLine: account.paperlessUser.fullName != null,
leading: UserAvatar(
account: account,
userId: userId,
),
trailing: PopupMenuButton(
icon: const Icon(Icons.more_vert),
itemBuilder: (context) {
return [
if (!isLoggedIn)
PopupMenuItem(
child: ListTile(
title: Text(S.of(context)!.switchAccount),
leading: const Icon(Icons.switch_account_rounded),
),
value: 0,
),
if (!isLoggedIn)
PopupMenuItem(
child: ListTile(
title: Text(S.of(context)!.remove),
leading: const Icon(
Icons.person_remove,
color: Colors.red,
),
),
value: 1,
)
else
PopupMenuItem(
child: ListTile(
title: Text(S.of(context)!.logout),
leading: const Icon(
Icons.person_remove,
color: Colors.red,
),
),
value: 1,
),
];
},
onSelected: (value) async {
if (value == 0) {
// Switch
_onSwitchAccount(context, settings.currentLoggedInUser!, userId);
} else if (value == 1) {
// Remove
final shouldPop = userId == settings.currentLoggedInUser;
await context.read<AuthenticationCubit>().removeAccount(userId);
if (shouldPop) {
Navigator.pop(context);
}
}
},
),
),
);
if (isLoggedIn) {
return Card(
child: child,
);
}
return child;
}
Future<void> _onAddAccount(BuildContext context, String currentUser) async {
final userId = await Navigator.push(
context,
@@ -202,9 +183,10 @@ class ManageAccountsPage extends StatelessWidget {
}
}
_onSwitchAccount(BuildContext context, String currentUser, String newUser) async {
void _onSwitchAccount(BuildContext context, String currentUser, String newUser) async {
if (currentUser == newUser) return;
Navigator.of(context).pop();
context.read<AuthenticationCubit>().switchAccount(newUser);
await context.read<AuthenticationCubit>().switchAccount(newUser);
}
}
@@ -30,7 +30,7 @@ class SettingsPage extends StatelessWidget {
builder: (context, snapshot) {
if (snapshot.hasError) {
return Text(
"Something went wrong while retrieving server data.", //TODO: INTL
S.of(context)!.errorRetrievingServerVersion,
style: Theme.of(context)
.textTheme
.labelSmall
@@ -40,7 +40,7 @@ class SettingsPage extends StatelessWidget {
}
if (!snapshot.hasData) {
return Text(
"Loading server information...", //TODO: INTL
S.of(context)!.resolvingServerVersion,
style: Theme.of(context).textTheme.labelSmall,
textAlign: TextAlign.center,
);
@@ -2,18 +2,16 @@ import 'package:flutter/material.dart';
import 'package:paperless_mobile/core/database/tables/local_user_account.dart';
class UserAvatar extends StatelessWidget {
final String userId;
final LocalUserAccount account;
const UserAvatar({
super.key,
required this.userId,
required this.account,
});
@override
Widget build(BuildContext context) {
final backgroundColor = Colors.primaries[userId.hashCode % Colors.primaries.length];
final backgroundColor = Colors.primaries[account.id.hashCode % Colors.primaries.length];
final foregroundColor = backgroundColor.computeLuminance() > 0.5 ? Colors.black : Colors.white;
return CircleAvatar(
child: Text((account.paperlessUser.fullName ?? account.paperlessUser.username)
@@ -0,0 +1,41 @@
import 'package:flutter/material.dart';
import 'package:paperless_mobile/core/database/tables/local_user_account.dart';
import 'package:paperless_mobile/features/settings/view/widgets/user_avatar.dart';
class UserAccountListTile extends StatelessWidget {
final LocalUserAccount account;
final Widget? trailing;
final VoidCallback? onTap;
const UserAccountListTile({
super.key,
required this.account,
this.trailing,
this.onTap,
});
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return SizedBox(
width: double.maxFinite,
child: ListTile(
onTap: onTap,
title: Text(account.paperlessUser.username),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (account.paperlessUser.fullName != null) Text(account.paperlessUser.fullName!),
Text(
account.serverUrl.replaceFirst(RegExp(r'https://?'), ''),
style: TextStyle(color: theme.colorScheme.primary),
),
],
),
isThreeLine: account.paperlessUser.fullName != null,
leading: UserAvatar(account: account),
trailing: trailing,
),
);
}
}