Skip to content

High performance self-hosted photo and video management solution.

4.7 KB
Raw Blame
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:immich_mobile/extensions/build_context_extensions.dart';
import 'package:immich_mobile/generated/translations.g.dart';
import 'package:immich_mobile/models/cast/cast_manager_state.dart';
import 'package:immich_mobile/providers/cast.provider.dart';

class CastDialog extends ConsumerWidget {
  const CastDialog({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final castManager = ref.watch(castProvider);

    bool isCurrentDevice(String deviceName) {
      return castManager.receiverName == deviceName && castManager.isCasting;
    }

    bool isDeviceConnecting(String deviceName) {
      return castManager.receiverName == deviceName && !castManager.isCasting;
    }

    return AlertDialog(
      title: Text(context.t.cast, style: const TextStyle(fontWeight: FontWeight.bold)),
      content: SizedBox(
        width: 250,
        height: 250,
        child: FutureBuilder<List<(String, CastDestinationType, dynamic)>>(
          future: ref.watch(castProvider.notifier).getDevices(),
          builder: (context, snapshot) {
            if (snapshot.hasError) {
              return Text(context.t.error_saving_image(error: snapshot.error.toString()));
            } else if (!snapshot.hasData) {
              return const SizedBox(height: 48, child: Center(child: CircularProgressIndicator()));
            }

            if (snapshot.data!.isEmpty) {
              return Text(context.t.no_cast_devices_found);
            }

            final devices = snapshot.data!;
            final connected = devices.where((d) => isCurrentDevice(d.$1)).toList();
            final others = devices.where((d) => !isCurrentDevice(d.$1)).toList();

            final List<dynamic> sectionedList = [];

            if (connected.isNotEmpty) {
              sectionedList.add(context.t.connected_device);
              sectionedList.addAll(connected);
            }

            if (others.isNotEmpty) {
              sectionedList.add(context.t.discovered_devices);
              sectionedList.addAll(others);
            }

            return ListView.builder(
              shrinkWrap: true,
              itemCount: sectionedList.length,
              itemBuilder: (context, index) {
                final item = sectionedList[index];

                if (item is String) {
                  // It's a section header
                  return Padding(
                    padding: const EdgeInsets.symmetric(vertical: 8.0),
                    child: Text(item, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16)),
                  );
                } else {
                  final (deviceName, type, deviceObj) = item as (String, CastDestinationType, dynamic);

                  return ListTile(
                    title: Text(
                      deviceName,
                      style: TextStyle(color: isCurrentDevice(deviceName) ? context.colorScheme.primary : null),
                    ),
                    leading: Icon(
                      type == CastDestinationType.googleCast ? Icons.cast : Icons.cast_connected,
                      color: isCurrentDevice(deviceName) ? context.colorScheme.primary : null,
                    ),
                    trailing: isCurrentDevice(deviceName)
                        ? Icon(Icons.check, color: context.colorScheme.primary)
                        : isDeviceConnecting(deviceName)
                        ? const CircularProgressIndicator()
                        : null,
                    onTap: () async {
                      if (isDeviceConnecting(deviceName)) {
                        return;
                      }

                      if (castManager.isCasting) {
                        await ref.read(castProvider.notifier).disconnect();
                      }

                      if (!isCurrentDevice(deviceName)) {
                        unawaited(ref.read(castProvider.notifier).connect(type, deviceObj));
                      }
                    },
                  );
                }
              },
            );
          },
        ),
      ),
      actions: [
        if (castManager.isCasting)
          TextButton(
            onPressed: () => ref.read(castProvider.notifier).disconnect(),
            child: Text(
              context.t.stop_casting,
              style: TextStyle(color: context.colorScheme.secondary, fontWeight: FontWeight.bold),
            ),
          ),
        TextButton(
          onPressed: () => context.pop(),
          child: Text(
            context.t.close,
            style: TextStyle(color: context.colorScheme.primary, fontWeight: FontWeight.bold),
          ),
        ),
      ],
    );
  }
}