import 'dart:async'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:mirror_core/mirror_core.dart'; import 'package:mirror_protocol/mirror_protocol.dart'; import 'settings_provider.dart'; final workoutApiProvider = Provider((ref) { final settings = ref.watch(settingsProvider).valueOrNull; final url = settings?.workoutPlayerUrl ?? 'http://192.168.86.172:8091'; return WorkoutApiClient(baseUrl: url); }); final workoutBrowseProvider = AsyncNotifierProvider( WorkoutBrowseNotifier.new); class WorkoutBrowseState { final List videos; final List folders; final WorkoutStats? stats; final String currentPath; final WorkoutFilter filter; const WorkoutBrowseState({ this.videos = const [], this.folders = const [], this.stats, this.currentPath = '', this.filter = WorkoutFilter.all, }); WorkoutBrowseState copyWith({ List? videos, List? folders, WorkoutStats? stats, String? currentPath, WorkoutFilter? filter, }) => WorkoutBrowseState( videos: videos ?? this.videos, folders: folders ?? this.folders, stats: stats ?? this.stats, currentPath: currentPath ?? this.currentPath, filter: filter ?? this.filter, ); } class WorkoutBrowseNotifier extends AsyncNotifier { @override Future build() async { return _fetch('', WorkoutFilter.all); } Future _fetch(String path, WorkoutFilter filter) async { final client = ref.read(workoutApiProvider); final result = await client.fetchVideos(folder: path.isEmpty ? null : path, filter: filter); final stats = await client.fetchStats(); return WorkoutBrowseState( videos: result.videos, folders: result.folders, stats: stats, currentPath: path, filter: filter, ); } Future browse(String path, {WorkoutFilter? filter}) async { final currentFilter = filter ?? state.valueOrNull?.filter ?? WorkoutFilter.all; state = const AsyncValue.loading(); state = await AsyncValue.guard(() => _fetch(path, currentFilter)); } Future setFilter(WorkoutFilter filter) async { final path = state.valueOrNull?.currentPath ?? ''; state = const AsyncValue.loading(); state = await AsyncValue.guard(() => _fetch(path, filter)); } String streamUrl(String videoPath) { final client = ref.read(workoutApiProvider); return client.streamUrl(videoPath); } }