84 lines
2.5 KiB
Dart
84 lines
2.5 KiB
Dart
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<WorkoutApiClient>((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, WorkoutBrowseState>(
|
|
WorkoutBrowseNotifier.new);
|
|
|
|
class WorkoutBrowseState {
|
|
final List<VideoState> videos;
|
|
final List<String> 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<VideoState>? videos,
|
|
List<String>? 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<WorkoutBrowseState> {
|
|
@override
|
|
Future<WorkoutBrowseState> build() async {
|
|
return _fetch('', WorkoutFilter.all);
|
|
}
|
|
|
|
Future<WorkoutBrowseState> _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<void> 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<void> 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);
|
|
}
|
|
}
|