Skip to content

Commit

Permalink
#224 - Fixed can not start print if printer is in cancelled state
Browse files Browse the repository at this point in the history
  • Loading branch information
Clon1998 committed Aug 20, 2023
1 parent abf67d9 commit 4ad3bf5
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 22 deletions.
29 changes: 18 additions & 11 deletions common/lib/service/moonraker/file_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,35 @@ Uri? previewImageUri(PreviewImageUriRef ref) {
var clientType =
(machine != null) ? ref.watch(jrpcClientTypeProvider(machine.uuid)) : ClientType.local;
if (machine != null) {
if (clientType == ClientType.local) {
return machine.httpUri;
} else {
var octoEverywhere = machine.octoEverywhere;
return octoEverywhere!.uri;
}
return switch (clientType) {
ClientType.octo => machine.octoEverywhere?.uri,
ClientType.manual => machine.remoteInterface?.remoteUri,
ClientType.local || _ => machine.httpUri,
};
}
return null;
}

@riverpod
Map<String, String> previewImageHttpHeader(PreviewImageHttpHeaderRef ref) {
var machine = ref.watch(selectedMachineProvider).valueOrFullNull;
Map<String, String> headers = machine?.headerWithApiKey ?? {};

var clientType =
(machine != null) ? ref.watch(jrpcClientTypeProvider(machine.uuid)) : ClientType.local;
if (machine != null) {
if (clientType == ClientType.octo) {
headers[HttpHeaders.authorizationHeader] = machine.octoEverywhere!.basicAuthorizationHeader;
}
return switch (clientType) {
ClientType.manual => {
...machine.remoteInterface!.httpHeaders,
if (machine.apiKey?.isNotEmpty == true) 'X-Api-Key': machine.apiKey!
},
ClientType.octo => {
...machine.headerWithApiKey,
HttpHeaders.authorizationHeader: machine.octoEverywhere!.basicAuthorizationHeader,
},
_ => machine.headerWithApiKey,
};
}
return headers;
return {};
}

@riverpod
Expand Down
22 changes: 21 additions & 1 deletion common/lib/util/extensions/async_ext.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,23 @@ extension AlwaysAliveAsyncDataSelector<Input> on ProviderListenable<AsyncValue<I
ProviderListenable<AsyncValue<Output>> selectAs<Output>(
Output Function(Input data) selector,
) {
return select((AsyncValue<Input> value) => value.whenData<Output>(selector));
return select((AsyncValue<Input> value) {
/// This block of code handles transformation of AsyncValue instances, ensuring consistent behavior.
/// We explicitly differentiate between data, error, and loading states while considering the previous value.
/// If the previous value existed, we apply the selector function to transform the data, preventing issues with
/// empty/new error/loading AsyncValue instances that lack values and might lead to unexpected behavior.
return value.map(
data: (data) => data.whenData(selector),
error: (error) => error.hasValue
? AsyncData<Output>(selector(value.value as Input))
.toError(error.error, error.stackTrace)
: AsyncValue<Output>.error(error.error, error.stackTrace),
loading: (loading) => loading.hasValue
? AsyncData<Output>(selector(value.value as Input)).toLoading()
: AsyncValue<Output>.loading(),
);
});
}
}

Expand All @@ -22,4 +38,8 @@ extension AsyncValueX<T> on AsyncValue<T> {
AsyncValue<T> toLoading() {
return AsyncValue<T>.loading().copyWithPrevious(this);
}

AsyncValue<T> toError(Object error, StackTrace stackTrace) {
return AsyncValue<T>.error(error, stackTrace).copyWithPrevious(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,15 @@ extension UriExtension on GCodeFile {
...bigImagePath!.split('/')
]);
}

Uri? constructSmallImageUri(Uri? baseUri) {
if (baseUri == null || smallImagePath == null) return null;
return baseUri.replace(pathSegments: [
...baseUri.pathSegments,
'server',
'files',
...parentPath.split('/'),
...smallImagePath!.split('/')
]);
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
* All rights reserved.
*/

import 'package:common/service/setting_service.dart';
import 'package:common/util/extensions/object_extension.dart';
import 'package:common/util/time_util.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_icons/flutter_icons.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:mobileraker/service/date_format_service.dart';
import 'package:common/service/setting_service.dart';
import 'package:mobileraker/ui/screens/dashboard/components/toolhead_info/toolhead_info_table_controller.dart';
import 'package:mobileraker/util/time_util.dart';

class ToolheadInfoTable extends ConsumerWidget {
static const String POS_ROW = "p";
Expand Down
3 changes: 2 additions & 1 deletion lib/ui/screens/dashboard/tabs/general_tab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ class PrintCard extends ConsumerWidget {
progressColor: (printState == PrintState.complete) ? Colors.green : Colors.deepOrange,
);
case PrintState.complete:
case PrintState.cancelled:
return PopupMenuButton(
enabled: klippyCanReceiveCommands,
padding: EdgeInsets.zero,
Expand All @@ -291,7 +292,7 @@ class PrintCard extends ConsumerWidget {
width: 8,
),
Text('pages.dashboard.general.print_card.reset',
style: TextStyle(color: themeData.colorScheme.primary))
style: TextStyle(color: themeData.colorScheme.primary))
.tr()
],
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ bool canStartPrint(CanStartPrintRef ref) {
var canPrint = ref.watch(printerSelectedProvider.select((value) => {
PrintState.complete,
PrintState.error,
PrintState.standby
PrintState.standby,
PrintState.cancelled,
}.contains(value.valueOrFullNull?.print.state)));

var klippyCanReceiveCommands = ref.watch(klipperSelectedProvider
Expand Down
10 changes: 4 additions & 6 deletions lib/ui/screens/files/details/gcode_file_details_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,24 @@
import 'package:cached_network_image/cached_network_image.dart';
import 'package:common/data/dto/files/gcode_file.dart';
import 'package:common/service/moonraker/file_service.dart';
import 'package:common/util/extensions/gcode_file_extension.dart';
import 'package:common/util/time_util.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:flutter_icons/flutter_icons.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:mobileraker/service/date_format_service.dart';
import 'package:mobileraker/ui/screens/files/details/gcode_file_details_controller.dart';
import 'package:mobileraker/util/extensions/gcode_file_extension.dart';
import 'package:mobileraker/util/time_util.dart';

class GCodeFileDetailPage extends ConsumerWidget {
const GCodeFileDetailPage({Key? key, required this.gcodeFile})
: super(key: key);
const GCodeFileDetailPage({Key? key, required this.gcodeFile}) : super(key: key);
final GCodeFile gcodeFile;

@override
Widget build(BuildContext context, WidgetRef ref) {
return ProviderScope(
overrides: [
gcodeProvider.overrideWithValue(gcodeFile),
overrides: [gcodeProvider.overrideWithValue(gcodeFile),
gCodeFileDetailsControllerProvider
],
child: const _GCodeFileDetailPage(),
Expand Down

0 comments on commit 4ad3bf5

Please sign in to comment.