Skip to content

Commit

Permalink
Merge pull request #647 from micahmo/feature/error-improvements
Browse files Browse the repository at this point in the history
A few improvements to error handling
  • Loading branch information
ajsosa committed Aug 20, 2023
2 parents d766152 + 1729d1c commit bf07d13
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- Hide the gesture customize hint when the gestures are disabled - contribution from @micahmo
- Improvements to text post indicator preview - contribution from @micahmo
- Show taglines with markdown and cycle through all available taglines - contribution from @micahmo
- Errors blocking users are now shown as toasts - contribution from @micahmo

### Fixed
- Handle issue where some deferred comments won't load - contribution from @micahmo
Expand Down
3 changes: 2 additions & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,6 @@
"successfullyUnblocked": "Successfully unblocked!",
"successfullyBlocked": "Successfully blocked!",
"showMore": "Show more",
"showLess": "Show less"
"showLess": "Show less",
"cantBlockAdmin": "You may not block an instance administrator."
}
3 changes: 2 additions & 1 deletion lib/l10n/app_es.arb
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,6 @@
"successfullyUnblocked": "Successfully unblocked!",
"successfullyBlocked": "Successfully blocked!",
"showMore": "Show more",
"showLess": "Show less"
"showLess": "Show less",
"cantBlockAdmin": "You may not block an instance administrator."
}
3 changes: 2 additions & 1 deletion lib/l10n/app_fi.arb
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,6 @@
"successfullyUnblocked": "Successfully unblocked!",
"successfullyBlocked": "Successfully blocked!",
"showMore": "Show more",
"showLess": "Show less"
"showLess": "Show less",
"cantBlockAdmin": "You may not block an instance administrator."
}
3 changes: 2 additions & 1 deletion lib/l10n/app_sv.arb
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,6 @@
"successfullyUnblocked": "Successfully unblocked!",
"successfullyBlocked": "Successfully blocked!",
"showMore": "Show more",
"showLess": "Show less"
"showLess": "Show less",
"cantBlockAdmin": "You may not block an instance administrator."
}
2 changes: 2 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import 'package:thunder/core/enums/theme_type.dart';
import 'package:thunder/core/singletons/database.dart';
import 'package:thunder/core/theme/bloc/theme_bloc.dart';
import 'package:thunder/core/auth/bloc/auth_bloc.dart';
import 'package:thunder/utils/global_context.dart';

void main() async {
WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
Expand Down Expand Up @@ -101,6 +102,7 @@ class ThunderApp extends StatelessWidget {
theme: theme,
darkTheme: darkTheme,
debugShowCheckedModeBanner: false,
scaffoldMessengerKey: GlobalContext.scaffoldMessengerKey,
),
);
},
Expand Down
8 changes: 5 additions & 3 deletions lib/user/bloc/user_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import 'package:thunder/core/models/comment_view_tree.dart';
import 'package:thunder/core/models/post_view_media.dart';
import 'package:thunder/core/singletons/lemmy_client.dart';
import 'package:thunder/utils/comment.dart';
import 'package:thunder/utils/error_messages.dart';
import 'package:thunder/utils/global_context.dart';
import 'package:thunder/utils/post.dart';

part 'user_event.dart';
Expand Down Expand Up @@ -432,7 +434,7 @@ class UserBloc extends Bloc<UserEvent, UserState> {
if (account?.jwt == null) {
return emit(
state.copyWith(
status: UserStatus.failure,
status: UserStatus.failedToBlock,
errorMessage: 'You are not logged in. Cannot block user.',
userId: state.userId,
),
Expand All @@ -453,8 +455,8 @@ class UserBloc extends Bloc<UserEvent, UserState> {
} catch (e, s) {
return emit(
state.copyWith(
status: UserStatus.failure,
errorMessage: e.toString(),
status: UserStatus.failedToBlock,
errorMessage: e is LemmyApiException ? getErrorMessage(GlobalContext.context, e.message) : e.toString(),
personView: state.personView,
),
);
Expand Down
2 changes: 1 addition & 1 deletion lib/user/bloc/user_state.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
part of 'user_bloc.dart';

enum UserStatus { initial, loading, refreshing, success, empty, failure }
enum UserStatus { initial, loading, refreshing, success, empty, failure, failedToBlock }

class UserState extends Equatable {
const UserState({
Expand Down
6 changes: 6 additions & 0 deletions lib/user/pages/user_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:swipeable_page_route/swipeable_page_route.dart';
import 'package:thunder/account/utils/profiles.dart';
import 'package:thunder/community/bloc/community_bloc.dart' as community;
import 'package:thunder/core/auth/bloc/auth_bloc.dart';
import 'package:thunder/shared/snackbar.dart';
import 'package:thunder/user/pages/user_page_success.dart';
import 'package:thunder/shared/error_message.dart';
import 'package:thunder/user/bloc/user_bloc.dart';
Expand Down Expand Up @@ -125,6 +126,10 @@ class _UserPageState extends State<UserPage> {
child: BlocBuilder<UserBloc, UserState>(builder: (context, state) {
userBloc = context.read<UserBloc>();

if (state.status == UserStatus.failedToBlock) {
showSnackbar(context, state.errorMessage ?? AppLocalizations.of(context)!.missingErrorMessage);
}

switch (state.status) {
case UserStatus.initial:
context.read<UserBloc>().add(GetUserEvent(userId: widget.userId, isAccountUser: widget.isAccountUser, username: widget.username, reset: true));
Expand All @@ -134,6 +139,7 @@ class _UserPageState extends State<UserPage> {
return const Center(child: CircularProgressIndicator());
case UserStatus.refreshing:
case UserStatus.success:
case UserStatus.failedToBlock:
return UserPageSuccess(
userId: widget.userId,
isAccountUser: widget.isAccountUser,
Expand Down
11 changes: 11 additions & 0 deletions lib/utils/error_messages.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

/// Attempts to retrieve a localized error message for the given [lemmyApiErrorCode].
/// Returns null if not found.
String? getErrorMessage(BuildContext context, String lemmyApiErrorCode) {
return switch (lemmyApiErrorCode) {
"cant_block_admin" => AppLocalizations.of(context)!.cantBlockAdmin,
_ => null,
};
}
6 changes: 6 additions & 0 deletions lib/utils/global_context.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import 'package:flutter/material.dart';

class GlobalContext {
static GlobalKey<ScaffoldMessengerState> scaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();
static BuildContext get context => scaffoldMessengerKey.currentContext!;
}

0 comments on commit bf07d13

Please sign in to comment.