|
| 1 | +import 'package:equatable/equatable.dart'; |
| 2 | + |
| 3 | +/// Source of the audio track |
| 4 | +enum BeatsSource { youtube, soundcloud, local, unknown } |
| 5 | + |
| 6 | +/// Represents a track from Beats (YouTube/SoundCloud) |
| 7 | +class BeatsTrack extends Equatable { |
| 8 | + final String id; |
| 9 | + final String title; |
| 10 | + final String artist; |
| 11 | + final String? thumbnailUrl; |
| 12 | + final Duration duration; |
| 13 | + final BeatsSource source; |
| 14 | + final String? sourceUrl; |
| 15 | + final String? streamUrl; // HLS stream URL (populated after resolution) |
| 16 | + |
| 17 | + const BeatsTrack({ |
| 18 | + required this.id, |
| 19 | + required this.title, |
| 20 | + required this.artist, |
| 21 | + this.thumbnailUrl, |
| 22 | + this.duration = Duration.zero, |
| 23 | + this.source = BeatsSource.unknown, |
| 24 | + this.sourceUrl, |
| 25 | + this.streamUrl, |
| 26 | + }); |
| 27 | + |
| 28 | + BeatsTrack copyWith({ |
| 29 | + String? id, |
| 30 | + String? title, |
| 31 | + String? artist, |
| 32 | + String? thumbnailUrl, |
| 33 | + Duration? duration, |
| 34 | + BeatsSource? source, |
| 35 | + String? sourceUrl, |
| 36 | + String? streamUrl, |
| 37 | + }) { |
| 38 | + return BeatsTrack( |
| 39 | + id: id ?? this.id, |
| 40 | + title: title ?? this.title, |
| 41 | + artist: artist ?? this.artist, |
| 42 | + thumbnailUrl: thumbnailUrl ?? this.thumbnailUrl, |
| 43 | + duration: duration ?? this.duration, |
| 44 | + source: source ?? this.source, |
| 45 | + sourceUrl: sourceUrl ?? this.sourceUrl, |
| 46 | + streamUrl: streamUrl ?? this.streamUrl, |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + @override |
| 51 | + List<Object?> get props => [id, title, artist, thumbnailUrl, duration, source, sourceUrl, streamUrl]; |
| 52 | +} |
| 53 | + |
| 54 | +/// Search result containing tracks and pagination info |
| 55 | +class BeatsSearchResult extends Equatable { |
| 56 | + final List<BeatsTrack> tracks; |
| 57 | + final String? nextPageToken; |
| 58 | + final int totalResults; |
| 59 | + |
| 60 | + const BeatsSearchResult({ |
| 61 | + required this.tracks, |
| 62 | + this.nextPageToken, |
| 63 | + this.totalResults = 0, |
| 64 | + }); |
| 65 | + |
| 66 | + @override |
| 67 | + List<Object?> get props => [tracks, nextPageToken, totalResults]; |
| 68 | +} |
| 69 | + |
| 70 | +/// Stream session with HLS manifest URL |
| 71 | +class BeatsStreamSession extends Equatable { |
| 72 | + final String sessionId; |
| 73 | + final String trackId; |
| 74 | + final String hlsManifestUrl; |
| 75 | + final DateTime createdAt; |
| 76 | + final DateTime expiresAt; |
| 77 | + |
| 78 | + const BeatsStreamSession({ |
| 79 | + required this.sessionId, |
| 80 | + required this.trackId, |
| 81 | + required this.hlsManifestUrl, |
| 82 | + required this.createdAt, |
| 83 | + required this.expiresAt, |
| 84 | + }); |
| 85 | + |
| 86 | + bool get isExpired => DateTime.now().isAfter(expiresAt); |
| 87 | + |
| 88 | + Duration get remainingTime => expiresAt.difference(DateTime.now()); |
| 89 | + |
| 90 | + @override |
| 91 | + List<Object?> get props => [sessionId, trackId, hlsManifestUrl, createdAt, expiresAt]; |
| 92 | +} |
| 93 | + |
| 94 | +/// Search state for UI |
| 95 | +enum BeatsSearchState { idle, searching, resolving, success, error } |
| 96 | + |
| 97 | +/// UI state for Beats search |
| 98 | +class BeatsSearchUiState extends Equatable { |
| 99 | + final BeatsSearchState state; |
| 100 | + final String query; |
| 101 | + final List<BeatsTrack> results; |
| 102 | + final String? errorMessage; |
| 103 | + final BeatsTrack? resolvingTrack; |
| 104 | + |
| 105 | + const BeatsSearchUiState({ |
| 106 | + this.state = BeatsSearchState.idle, |
| 107 | + this.query = '', |
| 108 | + this.results = const [], |
| 109 | + this.errorMessage, |
| 110 | + this.resolvingTrack, |
| 111 | + }); |
| 112 | + |
| 113 | + BeatsSearchUiState copyWith({ |
| 114 | + BeatsSearchState? state, |
| 115 | + String? query, |
| 116 | + List<BeatsTrack>? results, |
| 117 | + String? errorMessage, |
| 118 | + BeatsTrack? resolvingTrack, |
| 119 | + }) { |
| 120 | + return BeatsSearchUiState( |
| 121 | + state: state ?? this.state, |
| 122 | + query: query ?? this.query, |
| 123 | + results: results ?? this.results, |
| 124 | + errorMessage: errorMessage ?? this.errorMessage, |
| 125 | + resolvingTrack: resolvingTrack ?? this.resolvingTrack, |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + @override |
| 130 | + List<Object?> get props => [state, query, results, errorMessage, resolvingTrack]; |
| 131 | +} |
| 132 | + |
| 133 | +/// Result wrapper for repository operations |
| 134 | +class BeatsResult<T> { |
| 135 | + final T? data; |
| 136 | + final String? error; |
| 137 | + |
| 138 | + const BeatsResult.success(this.data) : error = null; |
| 139 | + const BeatsResult.failure(this.error) : data = null; |
| 140 | + |
| 141 | + bool get isSuccess => data != null; |
| 142 | + bool get isFailure => error != null; |
| 143 | +} |
| 144 | + |
0 commit comments