fix: Improve receiving shares

This commit is contained in:
Anton Stubenbord
2023-10-03 17:49:38 +02:00
parent 37ed8bbb04
commit ad23df4f89
29 changed files with 529 additions and 348 deletions

View File

@@ -20,6 +20,7 @@ import 'package:paperless_mobile/core/service/connectivity_status_service.dart';
import 'package:paperless_mobile/core/service/file_service.dart';
import 'package:paperless_mobile/features/login/model/client_certificate.dart';
import 'package:paperless_mobile/features/login/model/login_form_credentials.dart';
import 'package:paperless_mobile/features/login/model/reachability_status.dart';
import 'package:paperless_mobile/features/login/services/authentication_service.dart';
import 'package:paperless_mobile/generated/l10n/app_localizations.dart';
@@ -44,34 +45,35 @@ class AuthenticationCubit extends Cubit<AuthenticationState> {
ClientCertificate? clientCertificate,
}) async {
assert(credentials.username != null && credentials.password != null);
emit(const CheckingLoginState());
final localUserId = "${credentials.username}@$serverUrl";
_debugPrintMessage(
"login",
"Trying to login $localUserId...",
);
await _addUser(
localUserId,
serverUrl,
credentials,
clientCertificate,
_sessionManager,
);
try {
await _addUser(
localUserId,
serverUrl,
credentials,
clientCertificate,
_sessionManager,
);
// Mark logged in user as currently active user.
final globalSettings =
Hive.box<GlobalSettings>(HiveBoxes.globalSettings).getValue()!;
globalSettings.loggedInUserId = localUserId;
await globalSettings.save();
// Mark logged in user as currently active user.
final globalSettings =
Hive.box<GlobalSettings>(HiveBoxes.globalSettings).getValue()!;
globalSettings.loggedInUserId = localUserId;
await globalSettings.save();
emit(
AuthenticatedState(
localUserId: localUserId,
),
);
_debugPrintMessage(
"login",
"User successfully logged in.",
);
emit(AuthenticatedState(localUserId: localUserId));
_debugPrintMessage(
"login",
"User successfully logged in.",
);
} catch (error) {
emit(const UnauthenticatedState());
}
}
/// Switches to another account if it exists.
@@ -156,10 +158,8 @@ class AuthenticationCubit extends Cubit<AuthenticationState> {
}
Future<void> removeAccount(String userId) async {
final userAccountBox =
Hive.box<LocalUserAccount>(HiveBoxes.localUserAccount);
final userAppStateBox =
Hive.box<LocalUserAppState>(HiveBoxes.localUserAppState);
final userAccountBox = Hive.localUserAccountBox;
final userAppStateBox = Hive.localUserAppStateBox;
await FileService.clearUserData(userId: userId);
await userAccountBox.delete(userId);
await userAppStateBox.delete(userId);
@@ -263,9 +263,13 @@ class AuthenticationCubit extends Cubit<AuthenticationState> {
"restoreSessionState",
"Current session state successfully updated.",
);
final hasInternetConnection =
await _connectivityService.isConnectedToInternet();
if (hasInternetConnection) {
final isPaperlessServerReachable =
await _connectivityService.isPaperlessServerReachable(
localUserAccount.serverUrl,
authentication.clientCertificate,
) ==
ReachabilityStatus.reachable;
if (isPaperlessServerReachable) {
_debugPrintMessage(
"restoreSessionMState",
"Updating server user...",
@@ -283,7 +287,7 @@ class AuthenticationCubit extends Cubit<AuthenticationState> {
} else {
_debugPrintMessage(
"restoreSessionMState",
"Skipping update of server user (no internet connection).",
"Skipping update of server user (server could not be reached).",
);
}
@@ -295,14 +299,18 @@ class AuthenticationCubit extends Cubit<AuthenticationState> {
);
}
Future<void> logout() async {
Future<void> logout([bool removeAccount = false]) async {
emit(const LogginOutState());
_debugPrintMessage(
"logout",
"Trying to log out current user...",
);
await _resetExternalState();
final globalSettings =
Hive.box<GlobalSettings>(HiveBoxes.globalSettings).getValue()!;
final globalSettings = Hive.globalSettingsBox.getValue()!;
final userId = globalSettings.loggedInUserId!;
if (removeAccount) {
this.removeAccount(userId);
}
globalSettings.loggedInUserId = null;
await globalSettings.save();
@@ -459,19 +467,32 @@ class AuthenticationCubit extends Cubit<AuthenticationState> {
return serverUser.id;
}
Future<int> _getApiVersion(Dio dio) async {
Future<int> _getApiVersion(
Dio dio, {
Duration? timeout,
int defaultValue = 2,
}) async {
_debugPrintMessage(
"_getApiVersion",
"Trying to fetch API version...",
);
final response = await dio.get("/api/");
final apiVersion =
int.parse(response.headers.value('x-api-version') ?? "3");
_debugPrintMessage(
"_getApiVersion",
"API version ($apiVersion) successfully retrieved.",
);
return apiVersion;
try {
final response = await dio.get(
"/api/",
options: Options(
sendTimeout: timeout,
),
);
final apiVersion =
int.parse(response.headers.value('x-api-version') ?? "3");
_debugPrintMessage(
"_getApiVersion",
"API version ($apiVersion) successfully retrieved.",
);
return apiVersion;
} on DioException catch (e) {
return defaultValue;
}
}
/// Fetches possibly updated (permissions, name, updated server version and thus new user model, ...) remote user data.

View File

@@ -15,6 +15,14 @@ class RequiresLocalAuthenticationState extends AuthenticationState {
const RequiresLocalAuthenticationState();
}
class CheckingLoginState extends AuthenticationState {
const CheckingLoginState();
}
class LogginOutState extends AuthenticationState {
const LogginOutState();
}
class AuthenticatedState extends AuthenticationState {
final String localUserId;

View File

@@ -57,72 +57,76 @@ class _AddAccountPageState extends State<AddAccountPage> {
@override
Widget build(BuildContext context) {
final localAccounts =
Hive.box<LocalUserAccount>(HiveBoxes.localUserAccount);
return Scaffold(
resizeToAvoidBottomInset: false,
body: FormBuilder(
key: _formKey,
child: PageView(
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,
);
},
return ValueListenableBuilder(
valueListenable:
Hive.box<LocalUserAccount>(HiveBoxes.localUserAccount).listenable(),
builder: (context, localAccounts, child) {
return Scaffold(
resizeToAvoidBottomInset: false,
body: FormBuilder(
key: _formKey,
child: PageView(
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,
),
),
),
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);
},
),
ServerConnectionPage(
titleText: widget.titleString,
formBuilderKey: _formKey,
onContinue: () {
_pageController.nextPage(
duration: const Duration(milliseconds: 300),
curve: Curves.easeInOut,
);
},
itemCount: localAccounts.length,
),
),
ServerConnectionPage(
titleText: widget.titleString,
formBuilderKey: _formKey,
onContinue: () {
_pageController.nextPage(
duration: const Duration(milliseconds: 300),
curve: Curves.easeInOut,
);
},
ServerLoginPage(
formBuilderKey: _formKey,
submitText: widget.submitText,
onSubmit: _login,
),
],
),
ServerLoginPage(
formBuilderKey: _formKey,
submitText: widget.submitText,
onSubmit: _login,
),
],
),
),
),
);
},
);
}

View File

@@ -5,6 +5,7 @@ import 'package:hive_flutter/adapters.dart';
import 'package:paperless_api/paperless_api.dart';
import 'package:paperless_mobile/core/config/hive/hive_config.dart';
import 'package:paperless_mobile/core/database/tables/global_settings.dart';
import 'package:paperless_mobile/core/model/info_message_exception.dart';
import 'package:paperless_mobile/features/app_intro/application_intro_slideshow.dart';
import 'package:paperless_mobile/features/login/cubit/authentication_cubit.dart';
import 'package:paperless_mobile/features/login/model/client_certificate.dart';
@@ -71,6 +72,8 @@ class LoginPage extends StatelessWidget {
stackTrace,
); //TODO: Check if we can show error message directly on field here.
}
} on InfoMessageException catch (error) {
showInfoMessage(context, error);
} catch (unknownError, stackTrace) {
showGenericError(context, unknownError.toString(), stackTrace);
}