From 891ab8985ba1b87016fce86f70500bfe8c6727f0 Mon Sep 17 00:00:00 2001 From: Anton Stubenbord Date: Fri, 12 May 2023 00:37:07 +0200 Subject: [PATCH] feat: Make account switching work again --- .../factory/paperless_api_factory_impl.dart | 2 +- .../view/pages/document_details_page.dart | 19 +-- lib/features/inbox/view/pages/inbox_page.dart | 5 +- .../login/cubit/authentication_cubit.dart | 4 +- .../cubit/authentication_cubit.freezed.dart | 141 ++++++++++++++++++ .../login/cubit/authentication_state.dart | 1 + lib/main.dart | 2 + 7 files changed, 162 insertions(+), 12 deletions(-) diff --git a/lib/core/factory/paperless_api_factory_impl.dart b/lib/core/factory/paperless_api_factory_impl.dart index 40b718f..fb66df5 100644 --- a/lib/core/factory/paperless_api_factory_impl.dart +++ b/lib/core/factory/paperless_api_factory_impl.dart @@ -42,7 +42,7 @@ class PaperlessApiFactoryImpl implements PaperlessApiFactory { PaperlessUserApi createUserApi(Dio dio, {required int apiVersion}) { if (apiVersion == 3) { return PaperlessUserApiV3Impl(dio); - } else if (apiVersion == 1 || apiVersion == 2) { + } else if (apiVersion < 3) { return PaperlessUserApiV2Impl(dio); } throw Exception("API $apiVersion not supported."); diff --git a/lib/features/document_details/view/pages/document_details_page.dart b/lib/features/document_details/view/pages/document_details_page.dart index e05af25..c9e4d04 100644 --- a/lib/features/document_details/view/pages/document_details_page.dart +++ b/lib/features/document_details/view/pages/document_details_page.dart @@ -233,15 +233,16 @@ class _DocumentDetailsPageState extends State { ), ], ), - CustomScrollView( - controller: _pagingScrollController, - slivers: [ - SliverOverlapInjector( - handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context), - ), - const DocumentPermissionsWidget(), - ], - ), + if (apiVersion.hasMultiUserSupport) + CustomScrollView( + controller: _pagingScrollController, + slivers: [ + SliverOverlapInjector( + handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context), + ), + const DocumentPermissionsWidget(), + ], + ), ], ), ), diff --git a/lib/features/inbox/view/pages/inbox_page.dart b/lib/features/inbox/view/pages/inbox_page.dart index aa6192a..6d12ec8 100644 --- a/lib/features/inbox/view/pages/inbox_page.dart +++ b/lib/features/inbox/view/pages/inbox_page.dart @@ -3,6 +3,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:intl/intl.dart'; import 'package:paperless_api/paperless_api.dart'; +import 'package:paperless_mobile/core/database/tables/local_user_account.dart'; import 'package:paperless_mobile/core/exception/server_message_exception.dart'; import 'package:paperless_mobile/core/widgets/dialog_utils/dialog_cancel_button.dart'; import 'package:paperless_mobile/core/widgets/dialog_utils/dialog_confirm_button.dart'; @@ -41,11 +42,13 @@ class _InboxPageState extends State with DocumentPagingViewMixin( builder: (context, state) { - if (!state.hasLoaded || state.documents.isEmpty) { + if (!state.hasLoaded || state.documents.isEmpty || !canEditDocument) { return const SizedBox.shrink(); } return FloatingActionButton.extended( diff --git a/lib/features/login/cubit/authentication_cubit.dart b/lib/features/login/cubit/authentication_cubit.dart index cdfb5d3..9fb94ed 100644 --- a/lib/features/login/cubit/authentication_cubit.dart +++ b/lib/features/login/cubit/authentication_cubit.dart @@ -66,6 +66,7 @@ class AuthenticationCubit extends Cubit { /// Switches to another account if it exists. Future switchAccount(String localUserId) async { + emit(const AuthenticationState.switchingAccounts()); final globalSettings = Hive.box(HiveBoxes.globalSettings).getValue()!; if (globalSettings.currentLoggedInUser == localUserId) { return; @@ -130,12 +131,13 @@ class AuthenticationCubit extends Cubit { }) async { assert(credentials.password != null && credentials.username != null); final localUserId = "${credentials.username}@$serverUrl"; + final sessionManager = SessionManager(); await _addUser( localUserId, serverUrl, credentials, clientCertificate, - _sessionManager, + sessionManager, ); return localUserId; diff --git a/lib/features/login/cubit/authentication_cubit.freezed.dart b/lib/features/login/cubit/authentication_cubit.freezed.dart index 629ef40..6dbc119 100644 --- a/lib/features/login/cubit/authentication_cubit.freezed.dart +++ b/lib/features/login/cubit/authentication_cubit.freezed.dart @@ -21,6 +21,7 @@ mixin _$AuthenticationState { required TResult Function() unauthenticated, required TResult Function() requriresLocalAuthentication, required TResult Function(String localUserId, int apiVersion) authenticated, + required TResult Function() switchingAccounts, }) => throw _privateConstructorUsedError; @optionalTypeArgs @@ -28,6 +29,7 @@ mixin _$AuthenticationState { TResult? Function()? unauthenticated, TResult? Function()? requriresLocalAuthentication, TResult? Function(String localUserId, int apiVersion)? authenticated, + TResult? Function()? switchingAccounts, }) => throw _privateConstructorUsedError; @optionalTypeArgs @@ -35,6 +37,7 @@ mixin _$AuthenticationState { TResult Function()? unauthenticated, TResult Function()? requriresLocalAuthentication, TResult Function(String localUserId, int apiVersion)? authenticated, + TResult Function()? switchingAccounts, required TResult orElse(), }) => throw _privateConstructorUsedError; @@ -44,6 +47,7 @@ mixin _$AuthenticationState { required TResult Function(_RequiresLocalAuthentication value) requriresLocalAuthentication, required TResult Function(_Authenticated value) authenticated, + required TResult Function(_SwitchingAccounts value) switchingAccounts, }) => throw _privateConstructorUsedError; @optionalTypeArgs @@ -52,6 +56,7 @@ mixin _$AuthenticationState { TResult? Function(_RequiresLocalAuthentication value)? requriresLocalAuthentication, TResult? Function(_Authenticated value)? authenticated, + TResult? Function(_SwitchingAccounts value)? switchingAccounts, }) => throw _privateConstructorUsedError; @optionalTypeArgs @@ -60,6 +65,7 @@ mixin _$AuthenticationState { TResult Function(_RequiresLocalAuthentication value)? requriresLocalAuthentication, TResult Function(_Authenticated value)? authenticated, + TResult Function(_SwitchingAccounts value)? switchingAccounts, required TResult orElse(), }) => throw _privateConstructorUsedError; @@ -124,6 +130,7 @@ class _$_Unauthenticated implements _Unauthenticated { required TResult Function() unauthenticated, required TResult Function() requriresLocalAuthentication, required TResult Function(String localUserId, int apiVersion) authenticated, + required TResult Function() switchingAccounts, }) { return unauthenticated(); } @@ -134,6 +141,7 @@ class _$_Unauthenticated implements _Unauthenticated { TResult? Function()? unauthenticated, TResult? Function()? requriresLocalAuthentication, TResult? Function(String localUserId, int apiVersion)? authenticated, + TResult? Function()? switchingAccounts, }) { return unauthenticated?.call(); } @@ -144,6 +152,7 @@ class _$_Unauthenticated implements _Unauthenticated { TResult Function()? unauthenticated, TResult Function()? requriresLocalAuthentication, TResult Function(String localUserId, int apiVersion)? authenticated, + TResult Function()? switchingAccounts, required TResult orElse(), }) { if (unauthenticated != null) { @@ -159,6 +168,7 @@ class _$_Unauthenticated implements _Unauthenticated { required TResult Function(_RequiresLocalAuthentication value) requriresLocalAuthentication, required TResult Function(_Authenticated value) authenticated, + required TResult Function(_SwitchingAccounts value) switchingAccounts, }) { return unauthenticated(this); } @@ -170,6 +180,7 @@ class _$_Unauthenticated implements _Unauthenticated { TResult? Function(_RequiresLocalAuthentication value)? requriresLocalAuthentication, TResult? Function(_Authenticated value)? authenticated, + TResult? Function(_SwitchingAccounts value)? switchingAccounts, }) { return unauthenticated?.call(this); } @@ -181,6 +192,7 @@ class _$_Unauthenticated implements _Unauthenticated { TResult Function(_RequiresLocalAuthentication value)? requriresLocalAuthentication, TResult Function(_Authenticated value)? authenticated, + TResult Function(_SwitchingAccounts value)? switchingAccounts, required TResult orElse(), }) { if (unauthenticated != null) { @@ -239,6 +251,7 @@ class _$_RequiresLocalAuthentication implements _RequiresLocalAuthentication { required TResult Function() unauthenticated, required TResult Function() requriresLocalAuthentication, required TResult Function(String localUserId, int apiVersion) authenticated, + required TResult Function() switchingAccounts, }) { return requriresLocalAuthentication(); } @@ -249,6 +262,7 @@ class _$_RequiresLocalAuthentication implements _RequiresLocalAuthentication { TResult? Function()? unauthenticated, TResult? Function()? requriresLocalAuthentication, TResult? Function(String localUserId, int apiVersion)? authenticated, + TResult? Function()? switchingAccounts, }) { return requriresLocalAuthentication?.call(); } @@ -259,6 +273,7 @@ class _$_RequiresLocalAuthentication implements _RequiresLocalAuthentication { TResult Function()? unauthenticated, TResult Function()? requriresLocalAuthentication, TResult Function(String localUserId, int apiVersion)? authenticated, + TResult Function()? switchingAccounts, required TResult orElse(), }) { if (requriresLocalAuthentication != null) { @@ -274,6 +289,7 @@ class _$_RequiresLocalAuthentication implements _RequiresLocalAuthentication { required TResult Function(_RequiresLocalAuthentication value) requriresLocalAuthentication, required TResult Function(_Authenticated value) authenticated, + required TResult Function(_SwitchingAccounts value) switchingAccounts, }) { return requriresLocalAuthentication(this); } @@ -285,6 +301,7 @@ class _$_RequiresLocalAuthentication implements _RequiresLocalAuthentication { TResult? Function(_RequiresLocalAuthentication value)? requriresLocalAuthentication, TResult? Function(_Authenticated value)? authenticated, + TResult? Function(_SwitchingAccounts value)? switchingAccounts, }) { return requriresLocalAuthentication?.call(this); } @@ -296,6 +313,7 @@ class _$_RequiresLocalAuthentication implements _RequiresLocalAuthentication { TResult Function(_RequiresLocalAuthentication value)? requriresLocalAuthentication, TResult Function(_Authenticated value)? authenticated, + TResult Function(_SwitchingAccounts value)? switchingAccounts, required TResult orElse(), }) { if (requriresLocalAuthentication != null) { @@ -386,6 +404,7 @@ class _$_Authenticated implements _Authenticated { required TResult Function() unauthenticated, required TResult Function() requriresLocalAuthentication, required TResult Function(String localUserId, int apiVersion) authenticated, + required TResult Function() switchingAccounts, }) { return authenticated(localUserId, apiVersion); } @@ -396,6 +415,7 @@ class _$_Authenticated implements _Authenticated { TResult? Function()? unauthenticated, TResult? Function()? requriresLocalAuthentication, TResult? Function(String localUserId, int apiVersion)? authenticated, + TResult? Function()? switchingAccounts, }) { return authenticated?.call(localUserId, apiVersion); } @@ -406,6 +426,7 @@ class _$_Authenticated implements _Authenticated { TResult Function()? unauthenticated, TResult Function()? requriresLocalAuthentication, TResult Function(String localUserId, int apiVersion)? authenticated, + TResult Function()? switchingAccounts, required TResult orElse(), }) { if (authenticated != null) { @@ -421,6 +442,7 @@ class _$_Authenticated implements _Authenticated { required TResult Function(_RequiresLocalAuthentication value) requriresLocalAuthentication, required TResult Function(_Authenticated value) authenticated, + required TResult Function(_SwitchingAccounts value) switchingAccounts, }) { return authenticated(this); } @@ -432,6 +454,7 @@ class _$_Authenticated implements _Authenticated { TResult? Function(_RequiresLocalAuthentication value)? requriresLocalAuthentication, TResult? Function(_Authenticated value)? authenticated, + TResult? Function(_SwitchingAccounts value)? switchingAccounts, }) { return authenticated?.call(this); } @@ -443,6 +466,7 @@ class _$_Authenticated implements _Authenticated { TResult Function(_RequiresLocalAuthentication value)? requriresLocalAuthentication, TResult Function(_Authenticated value)? authenticated, + TResult Function(_SwitchingAccounts value)? switchingAccounts, required TResult orElse(), }) { if (authenticated != null) { @@ -463,3 +487,120 @@ abstract class _Authenticated implements AuthenticationState { _$$_AuthenticatedCopyWith<_$_Authenticated> get copyWith => throw _privateConstructorUsedError; } + +/// @nodoc +abstract class _$$_SwitchingAccountsCopyWith<$Res> { + factory _$$_SwitchingAccountsCopyWith(_$_SwitchingAccounts value, + $Res Function(_$_SwitchingAccounts) then) = + __$$_SwitchingAccountsCopyWithImpl<$Res>; +} + +/// @nodoc +class __$$_SwitchingAccountsCopyWithImpl<$Res> + extends _$AuthenticationStateCopyWithImpl<$Res, _$_SwitchingAccounts> + implements _$$_SwitchingAccountsCopyWith<$Res> { + __$$_SwitchingAccountsCopyWithImpl( + _$_SwitchingAccounts _value, $Res Function(_$_SwitchingAccounts) _then) + : super(_value, _then); +} + +/// @nodoc + +class _$_SwitchingAccounts implements _SwitchingAccounts { + const _$_SwitchingAccounts(); + + @override + String toString() { + return 'AuthenticationState.switchingAccounts()'; + } + + @override + bool operator ==(dynamic other) { + return identical(this, other) || + (other.runtimeType == runtimeType && other is _$_SwitchingAccounts); + } + + @override + int get hashCode => runtimeType.hashCode; + + @override + @optionalTypeArgs + TResult when({ + required TResult Function() unauthenticated, + required TResult Function() requriresLocalAuthentication, + required TResult Function(String localUserId, int apiVersion) authenticated, + required TResult Function() switchingAccounts, + }) { + return switchingAccounts(); + } + + @override + @optionalTypeArgs + TResult? whenOrNull({ + TResult? Function()? unauthenticated, + TResult? Function()? requriresLocalAuthentication, + TResult? Function(String localUserId, int apiVersion)? authenticated, + TResult? Function()? switchingAccounts, + }) { + return switchingAccounts?.call(); + } + + @override + @optionalTypeArgs + TResult maybeWhen({ + TResult Function()? unauthenticated, + TResult Function()? requriresLocalAuthentication, + TResult Function(String localUserId, int apiVersion)? authenticated, + TResult Function()? switchingAccounts, + required TResult orElse(), + }) { + if (switchingAccounts != null) { + return switchingAccounts(); + } + return orElse(); + } + + @override + @optionalTypeArgs + TResult map({ + required TResult Function(_Unauthenticated value) unauthenticated, + required TResult Function(_RequiresLocalAuthentication value) + requriresLocalAuthentication, + required TResult Function(_Authenticated value) authenticated, + required TResult Function(_SwitchingAccounts value) switchingAccounts, + }) { + return switchingAccounts(this); + } + + @override + @optionalTypeArgs + TResult? mapOrNull({ + TResult? Function(_Unauthenticated value)? unauthenticated, + TResult? Function(_RequiresLocalAuthentication value)? + requriresLocalAuthentication, + TResult? Function(_Authenticated value)? authenticated, + TResult? Function(_SwitchingAccounts value)? switchingAccounts, + }) { + return switchingAccounts?.call(this); + } + + @override + @optionalTypeArgs + TResult maybeMap({ + TResult Function(_Unauthenticated value)? unauthenticated, + TResult Function(_RequiresLocalAuthentication value)? + requriresLocalAuthentication, + TResult Function(_Authenticated value)? authenticated, + TResult Function(_SwitchingAccounts value)? switchingAccounts, + required TResult orElse(), + }) { + if (switchingAccounts != null) { + return switchingAccounts(this); + } + return orElse(); + } +} + +abstract class _SwitchingAccounts implements AuthenticationState { + const factory _SwitchingAccounts() = _$_SwitchingAccounts; +} diff --git a/lib/features/login/cubit/authentication_state.dart b/lib/features/login/cubit/authentication_state.dart index 21c886f..2d91649 100644 --- a/lib/features/login/cubit/authentication_state.dart +++ b/lib/features/login/cubit/authentication_state.dart @@ -8,4 +8,5 @@ class AuthenticationState with _$AuthenticationState { required String localUserId, required int apiVersion, }) = _Authenticated; + const factory AuthenticationState.switchingAccounts() = _SwitchingAccounts; } diff --git a/lib/main.dart b/lib/main.dart index 69b10a2..f20fbd5 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -38,6 +38,7 @@ import 'package:paperless_mobile/features/login/model/login_form_credentials.dar import 'package:paperless_mobile/features/login/services/authentication_service.dart'; import 'package:paperless_mobile/features/login/view/login_page.dart'; import 'package:paperless_mobile/features/notifications/services/local_notification_service.dart'; +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/sharing/share_intent_queue.dart'; import 'package:paperless_mobile/generated/l10n/app_localizations.dart'; @@ -270,6 +271,7 @@ class _AuthenticationWrapperState extends State { paperlessProviderFactory: widget.paperlessProviderFactory, localUserId: localUserId, ), + switchingAccounts: () => const SwitchingAccountsPage(), ); }, );