mirror of
https://github.com/immich-app/immich.git
synced 2026-06-23 07:06:43 -07:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d56ee21e8c |
@@ -0,0 +1,23 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/domain/models/asset/base_asset.model.dart';
|
||||
|
||||
abstract class BaseAction {
|
||||
final IconData icon;
|
||||
|
||||
const BaseAction({required this.icon});
|
||||
|
||||
String label(BuildContext context);
|
||||
|
||||
bool isVisible(BuildContext context, WidgetRef ref);
|
||||
|
||||
Future<void> onAction(BuildContext context, WidgetRef ref);
|
||||
}
|
||||
|
||||
abstract class AssetAction<T extends BaseAsset> extends BaseAction {
|
||||
final List<T> assets;
|
||||
|
||||
const AssetAction({required super.icon, required this.assets});
|
||||
|
||||
List<T> assetsForAction(BuildContext context, WidgetRef ref);
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/presentation/actions/action.dart';
|
||||
import 'package:immich_mobile/utils/error_handler.dart';
|
||||
import 'package:immich_ui/immich_ui.dart';
|
||||
|
||||
abstract class BaseActionWidget extends ConsumerWidget {
|
||||
final BaseAction action;
|
||||
final void Function(BuildContext _, WidgetRef _)? postAction;
|
||||
|
||||
const BaseActionWidget({super.key, required this.action, this.postAction});
|
||||
|
||||
Widget buildAction(BuildContext context, Future<void> Function() onPressed);
|
||||
|
||||
Future<void> _onPressed(BuildContext context, WidgetRef ref) async {
|
||||
try {
|
||||
await action.onAction(context, ref);
|
||||
} catch (error, stackTrace) {
|
||||
handleError(context, error, stack: stackTrace, description: 'Action failed: ${action.runtimeType}');
|
||||
}
|
||||
|
||||
if (context.mounted) {
|
||||
postAction?.call(context, ref);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
if (!action.isVisible(context, ref)) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
|
||||
return buildAction(context, () => _onPressed(context, ref));
|
||||
}
|
||||
}
|
||||
|
||||
class ActionIconButtonWidget extends BaseActionWidget {
|
||||
final ImmichVariant variant;
|
||||
|
||||
const ActionIconButtonWidget({super.key, required super.action, this.variant = .ghost, super.postAction});
|
||||
|
||||
@override
|
||||
Widget buildAction(BuildContext context, Future<void> Function() onPressed) =>
|
||||
ImmichIconButton(icon: action.icon, onPressed: onPressed, variant: variant);
|
||||
}
|
||||
|
||||
class ActionButtonWidget extends BaseActionWidget {
|
||||
final ImmichVariant variant;
|
||||
|
||||
const ActionButtonWidget({super.key, required super.action, this.variant = .ghost, super.postAction});
|
||||
|
||||
@override
|
||||
Widget buildAction(BuildContext context, Future<void> Function() onPressed) =>
|
||||
ImmichTextButton(labelText: action.label(context), icon: action.icon, onPressed: onPressed, variant: variant);
|
||||
}
|
||||
|
||||
class ActionColumnButtonWidget extends BaseActionWidget {
|
||||
const ActionColumnButtonWidget({super.key, required super.action, super.postAction});
|
||||
|
||||
@override
|
||||
Widget buildAction(BuildContext context, Future<void> Function() onPressed) =>
|
||||
ImmichColumnButton(icon: action.icon, label: action.label(context), onPressed: onPressed);
|
||||
}
|
||||
|
||||
class ActionMenuItemWidget extends BaseActionWidget {
|
||||
const ActionMenuItemWidget({super.key, required super.action, super.postAction});
|
||||
|
||||
@override
|
||||
Widget buildAction(BuildContext context, Future<void> Function() onPressed) =>
|
||||
ImmichMenuItem(icon: action.icon, label: action.label(context), onPressed: onPressed);
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:immich_mobile/generated/translations.g.dart';
|
||||
import 'package:immich_mobile/utils/debug_print.dart';
|
||||
import 'package:immich_ui/immich_ui.dart';
|
||||
import 'package:openapi/api.dart';
|
||||
// ignore: depend_on_referenced_packages
|
||||
import 'package:stack_trace/stack_trace.dart';
|
||||
|
||||
void handleError(BuildContext context, Object error, {StackTrace? stack, String? description}) {
|
||||
String? stackTrace;
|
||||
if (stack != null) {
|
||||
final trace = Trace.from(stack);
|
||||
final clean = trace.foldFrames(
|
||||
(frame) => frame.package == 'flutter' || frame.package == 'flutter_test' || frame.isCore,
|
||||
terse: true,
|
||||
);
|
||||
stackTrace = clean.toString();
|
||||
}
|
||||
|
||||
dPrint(
|
||||
() => 'Error${description != null ? ' ($description)' : ''}: $error${stackTrace != null ? '\n$stackTrace' : ''}',
|
||||
);
|
||||
|
||||
if (!context.mounted) {
|
||||
return;
|
||||
}
|
||||
|
||||
final String message;
|
||||
if (serverErrorMessage(error) case String serverMessage) {
|
||||
message = serverMessage;
|
||||
} else if (isConnectionError(error)) {
|
||||
message = context.t.login_form_server_error;
|
||||
} else {
|
||||
message = context.t.scaffold_body_error_occurred;
|
||||
}
|
||||
|
||||
snackbar.error(message);
|
||||
}
|
||||
|
||||
@visibleForTesting
|
||||
String? serverErrorMessage(Object error) {
|
||||
if (error is! ApiException || error.innerException != null || error.message == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
final body = jsonDecode(error.message!);
|
||||
if (body is Map && body['message'] != null) {
|
||||
final message = body['message'];
|
||||
return message is List ? message.join(', ') : message.toString();
|
||||
}
|
||||
} catch (_) {
|
||||
// The body was not JSON; fall back to the raw payload below.
|
||||
}
|
||||
return error.message;
|
||||
}
|
||||
|
||||
@visibleForTesting
|
||||
bool isConnectionError(Object error) => error is ApiException && error.innerException != null;
|
||||
Reference in New Issue
Block a user