Skip to content

Commit

Permalink
respond to feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
twsouthwick committed Nov 17, 2024
1 parent 6ba9529 commit 460796a
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 109 deletions.
87 changes: 87 additions & 0 deletions lib/components/login_messages.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

class LoginMessages {
static Future<void> showErrorDialog(
BuildContext context,
Object e,
) {
return showInfoDialog(
context,
Text(
AppLocalizations.of(context)!.errorConnectingToServer,
),
content: Text(e.toString()));
}

static Future<void> showHttpErrorDialog(
BuildContext context,
DioException e,
) {
return showInfoDialog(
context,
Text(
AppLocalizations.of(context)!.errorConnectingToServer,
),
content: e.response?.statusCode == null
? Text(e.toString())
: Text(_formatHttpErrorCode(e.response)),
);
}

static Future<void> showInfoDialog(
BuildContext context,
Widget title, {
Widget? content,
}) async {
await showDialog(
context: context,
builder: (context) => CallbackShortcuts(
bindings: <ShortcutActivator, VoidCallback>{
const SingleActivator(LogicalKeyboardKey.enter): () {
Navigator.pop(context);
}
},
child: FocusScope(
autofocus: true,
child: AlertDialog(
title: title,
content: content,
actions: [
ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('Ok'),
)
],
),
),
),
);
}

static String _formatHttpErrorCode(Response? resp) {
// todo PLACEHOLDER MESSAGES NOT FINAL
var message = '';
switch (resp!.statusCode) {
case 400:
message =
'The server could not understand the request, if you are using proxies check the configuration, if the issue still persists let us know';
case 401:
message = 'Your username or password may be incorrect';
case 403:
message =
'The server is blocking request from this device, this probably means the device has been banned, please contact your admin to resolve this issue';
default:
message = '';
}

return '$message\n\n'
'Http Code: ${resp.statusCode ?? 'Unknown'}\n\n'
'Http Response: ${resp.statusMessage ?? 'Unknown'}\n\n'
.trim();
}
}
8 changes: 4 additions & 4 deletions lib/navigation/app_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import 'package:jellyflix/screens/download_screen.dart';
import 'package:jellyflix/screens/home_screen.dart';
import 'package:jellyflix/screens/library_screen.dart';
import 'package:jellyflix/screens/loading_screen.dart';
import 'package:jellyflix/screens/login_password.dart';
import 'package:jellyflix/screens/login_quickconnect.dart';
import 'package:jellyflix/screens/login_password_screen.dart';
import 'package:jellyflix/screens/login_quickconnect_screen.dart';
import 'package:jellyflix/screens/login_wrapper_screen.dart';
import 'package:jellyflix/screens/offline_player_screen.dart';
import 'package:jellyflix/screens/profile_screen.dart';
Expand Down Expand Up @@ -147,7 +147,7 @@ class AppRouter {
context: context,
state: state,
maintainState: false,
child: LoginWithPasswordScreen(serverAddress: state.pathParameters['server']!),
child: LoginPasswordScreen(serverAddress: state.pathParameters['server']!),
),
),
GoRoute(
Expand All @@ -157,7 +157,7 @@ class AppRouter {
context: context,
state: state,
maintainState: false,
child: LoginWithQuickConnectScreen(serverAddress: state.pathParameters['server']!),
child: LoginQuickConnectScreen(serverAddress: state.pathParameters['server']!),
),
),
],
Expand Down
86 changes: 0 additions & 86 deletions lib/screens/login_messages.dart

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:jellyflix/models/screen_paths.dart';
import 'package:jellyflix/models/user.dart';
import 'package:jellyflix/providers/auth_provider.dart';
import 'package:jellyflix/screens/login_messages.dart';
import 'package:jellyflix/components/login_messages.dart';

class LoginWithPasswordScreen extends HookConsumerWidget {
class LoginPasswordScreen extends HookConsumerWidget {
final String serverAddress;
const LoginWithPasswordScreen({super.key, required this.serverAddress});
const LoginPasswordScreen({super.key, required this.serverAddress});

@override
Widget build(BuildContext context, WidgetRef ref) {
Expand Down Expand Up @@ -145,7 +145,7 @@ class LoginWithPasswordScreen extends HookConsumerWidget {
);
if (missingFields.isNotEmpty) {
loadingListenable.value = false;
await showInfoDialog(
await LoginMessages.showInfoDialog(
context,
Text(
AppLocalizations.of(context)!.emptyFields,
Expand All @@ -169,12 +169,12 @@ class LoginWithPasswordScreen extends HookConsumerWidget {
} on DioException catch (e) {
if (!context.mounted) return;
loadingListenable.value = false;
await showHttpErrorDialog(context, e);
await LoginMessages.showHttpErrorDialog(context, e);
return;
} catch (e) {
if (!context.mounted) return;
loadingListenable.value = false;
await showErrorDialog(context, e);
await LoginMessages.showErrorDialog(context, e);
return;
}
loadingListenable.value = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import 'package:go_router/go_router.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:jellyflix/models/screen_paths.dart';
import 'package:jellyflix/providers/auth_provider.dart';
import 'package:jellyflix/screens/login_messages.dart';
import 'package:jellyflix/components/login_messages.dart';

class LoginWithQuickConnectScreen extends HookConsumerWidget {
class LoginQuickConnectScreen extends HookConsumerWidget {
final String serverAddress;
const LoginWithQuickConnectScreen({super.key, required this.serverAddress});
const LoginQuickConnectScreen({super.key, required this.serverAddress});

@override
Widget build(BuildContext context, WidgetRef ref) {
Expand All @@ -28,11 +28,11 @@ class LoginWithQuickConnectScreen extends HookConsumerWidget {
.loginByQuickConnect(serverAddress, (c) => code.value = c, token);
} on DioException catch (e) {
if (!token.isCancelled && context.mounted) {
await showHttpErrorDialog(context, e);
await LoginMessages.showHttpErrorDialog(context, e);
}
} on Exception catch (e) {
if (!token.isCancelled && context.mounted) {
await showErrorDialog(context, e);
await LoginMessages.showErrorDialog(context, e);
}
}

Expand Down
25 changes: 17 additions & 8 deletions lib/services/auth_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,19 @@ class AuthService {

try {
if (user != null) {
await _apiService.registerAccessToken(user);
if (user.password != null) {
await login(user);
} else {
await _apiService.registerAccessToken(user);
}

_authStateStream.add(true);
return true;
}
} catch (_) {
}
} catch (_) {}

_authStateStream.add(false);
return false;
_authStateStream.add(false);
return false;
}
}

Expand All @@ -75,8 +79,8 @@ class AuthService {
return null;
}

_databaseService.put(user.id! + serverAddress, user);
_databaseService.put("currentProfileId", user.id! + serverAddress);
_databaseService.put(user.id! + user.serverAdress!, user);
_databaseService.put("currentProfileId", user.id! + user.serverAdress!);

_authStateStream.add(true);
return user;
Expand Down Expand Up @@ -140,7 +144,12 @@ class AuthService {
Future switchProfile(String profileId) async {
User? user = _databaseService.get(profileId);
if (user != null && user.serverAdress != null && user.token != null) {
await _apiService.registerAccessToken(user);
if (user.password != null) {
await _apiService.login(user.serverAdress!, user.name!, user.password!);
} else {
await _apiService.registerAccessToken(user);
}

_authStateStream.add(true);
} else {
throw Exception("Profile not found");
Expand Down

0 comments on commit 460796a

Please sign in to comment.