fix
This commit is contained in:
@@ -1,22 +1,26 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:legacy_shared/legacy_shared.dart';
|
||||
import 'package:sf_shared/sf_shared.dart';
|
||||
|
||||
import '../../../../core/domain/repositories/background_image_repository.dart';
|
||||
import '../../../../core/providers/background_image_repository_provider.dart';
|
||||
import 'background_image_view_state.dart';
|
||||
|
||||
final backgroundImageViewModelProvider =
|
||||
NotifierProvider.autoDispose<BackgroundImageViewModel, BackgroundImageViewState>(
|
||||
BackgroundImageViewModel.new,
|
||||
);
|
||||
NotifierProvider.autoDispose<
|
||||
BackgroundImageViewModel,
|
||||
BackgroundImageViewState
|
||||
>(BackgroundImageViewModel.new);
|
||||
|
||||
class BackgroundImageViewModel extends Notifier<BackgroundImageViewState> {
|
||||
late final BackgroundImageRepository _repository;
|
||||
late final SharedDevicesRepository _devicesRepository;
|
||||
|
||||
@override
|
||||
BackgroundImageViewState build() {
|
||||
_repository = ref.read(backgroundImageRepositoryProvider);
|
||||
_devicesRepository = ref.read(sharedDevicesRepositoryProvider);
|
||||
Future.microtask(_load);
|
||||
return const BackgroundImageViewState();
|
||||
}
|
||||
@@ -62,7 +66,11 @@ class BackgroundImageViewModel extends Notifier<BackgroundImageViewState> {
|
||||
final device = ref.read(selectedDeviceProvider);
|
||||
if (device == null) return;
|
||||
|
||||
state = state.copyWith(isSaving: true, errorEvent: null, successEvent: null);
|
||||
state = state.copyWith(
|
||||
isSaving: true,
|
||||
errorEvent: null,
|
||||
successEvent: null,
|
||||
);
|
||||
|
||||
try {
|
||||
final photoId = await _repository.uploadImage(path: image.path);
|
||||
@@ -74,14 +82,11 @@ class BackgroundImageViewModel extends Notifier<BackgroundImageViewState> {
|
||||
);
|
||||
if (!ref.mounted) return;
|
||||
|
||||
ref.syncDeviceSettings(
|
||||
device,
|
||||
device.settings.copyWith(backgroundImageId: photoId),
|
||||
);
|
||||
await _refreshDevice(device);
|
||||
if (!ref.mounted) return;
|
||||
|
||||
state = state.copyWith(
|
||||
isSaving: false,
|
||||
currentBackgroundId: photoId,
|
||||
successEvent: BackgroundImageSuccessEvent.uploaded,
|
||||
);
|
||||
|
||||
@@ -99,7 +104,11 @@ class BackgroundImageViewModel extends Notifier<BackgroundImageViewState> {
|
||||
final device = ref.read(selectedDeviceProvider);
|
||||
if (device == null) return;
|
||||
|
||||
state = state.copyWith(isSaving: true, errorEvent: null, successEvent: null);
|
||||
state = state.copyWith(
|
||||
isSaving: true,
|
||||
errorEvent: null,
|
||||
successEvent: null,
|
||||
);
|
||||
|
||||
try {
|
||||
await _repository.setBackgroundImage(
|
||||
@@ -108,16 +117,15 @@ class BackgroundImageViewModel extends Notifier<BackgroundImageViewState> {
|
||||
);
|
||||
if (!ref.mounted) return;
|
||||
|
||||
ref.syncDeviceSettings(
|
||||
device,
|
||||
device.settings.copyWith(backgroundImageId: photoId),
|
||||
);
|
||||
await _refreshDevice(device);
|
||||
if (!ref.mounted) return;
|
||||
|
||||
state = state.copyWith(
|
||||
isSaving: false,
|
||||
currentBackgroundId: photoId,
|
||||
successEvent: BackgroundImageSuccessEvent.backgroundSet,
|
||||
);
|
||||
|
||||
await reload();
|
||||
} catch (e) {
|
||||
if (!ref.mounted) return;
|
||||
state = state.copyWith(
|
||||
@@ -126,4 +134,15 @@ class BackgroundImageViewModel extends Notifier<BackgroundImageViewState> {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _refreshDevice(DeviceEntity currentDevice) async {
|
||||
final devices = await _devicesRepository.getDevices();
|
||||
if (!ref.mounted) return;
|
||||
|
||||
final updated = devices.firstWhere(
|
||||
(d) => d.identificator == currentDevice.identificator,
|
||||
orElse: () => currentDevice,
|
||||
);
|
||||
ref.read(selectedDeviceProvider.notifier).setSelectedDevice(updated);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user