cachedDeviceFamily top-level property

AutoDisposeProviderFamily<AsyncValue<Device>, int> cachedDeviceFamily
final

Implementation

final cachedDeviceFamily = Provider.autoDispose.family<AsyncValue<Device>, int>(
  (ref, int id) {
    final generatorDetails = ref.watch(generatorDetailsFamily(id));
    final detailsDevice = generatorDetails.valueOrNull?.device;

    final cachedDevices = ref.watch(cachedDevicesProvider);
    final cachedDevice = cachedDevices.valueOrNull
        ?.where((device) => device.id == id)
        .singleOrNull;

    AsyncValue<Device?> asyncValue;
    if (detailsDevice?.latestParameterUpdateDate != null &&
        cachedDevice?.latestParameterUpdateDate != null) {
      asyncValue = detailsDevice!.latestParameterUpdateDate!
              .isAfter(cachedDevice!.latestParameterUpdateDate!)
          ? generatorDetails.mapValue((_) => detailsDevice)
          : cachedDevices.mapValue((_) => cachedDevice);
    } else {
      asyncValue = detailsDevice != null
          ? generatorDetails.mapValue((_) => detailsDevice)
          : cachedDevices.mapValue((_) => cachedDevice);
    }

    final device = asyncValue.valueOrNull;

    if (asyncValue.isLoading) {
      if (device != null) {
        return const AsyncLoading<Device>().copyWithPrevious(AsyncData(device));
      } else {
        return const AsyncLoading<Device>();
      }
    } else if (device != null) {
      return asyncValue.mapValue((_) => device);
    } else {
      return AsyncError(UnauthorizedDeviceException(id), StackTrace.current);
    }
  },
);