Skip to content

Commit

Permalink
[Application] Updated bloc to the latest version
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfteam committed Aug 29, 2022
1 parent 523cba7 commit 4282c36
Show file tree
Hide file tree
Showing 14 changed files with 493 additions and 487 deletions.
49 changes: 23 additions & 26 deletions CastIt.Android/lib/application/intro/intro_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,30 @@ class IntroBloc extends Bloc<IntroEvent, IntroState> {
final SettingsBloc _settingsBloc;
late StreamSubscription _settingSubscription;

_LoadedState get currentState => state as _LoadedState;

IntroBloc(this._settings, this._settingsBloc) : super(const IntroState.loading()) {
on<_Load>((event, emit) => emit(IntroState.loaded(currentCastItUrl: _settings.castItUrl, currentLang: _settings.language)));

on<_ChangePage>((event, emit) {
final updatedState = !currentState.urlWasSet
? currentState.copyWith.call(page: event.newPage)
: currentState.copyWith.call(page: event.newPage, urlWasSet: false);
emit(updatedState);
});

on<_UrlSet>((event, emit) {
//This can happen when the user press the skip button
if (event.url.isNullEmptyOrWhitespace) {
_settings.castItUrl = AppConstants.baseCastItUrl;
}

final updatedState = currentState.copyWith.call(urlWasSet: true, currentCastItUrl: _settings.castItUrl);
emit(updatedState);
});

on<_LanguageChanged>((event, emit) => emit(currentState.copyWith.call(currentLang: event.newLang)));

_settingSubscription = _settingsBloc.stream.listen((e) {
e.maybeMap(
loaded: (s) {
Expand All @@ -33,32 +56,6 @@ class IntroBloc extends Bloc<IntroEvent, IntroState> {
});
}

_LoadedState get currentState => state as _LoadedState;

@override
Stream<IntroState> mapEventToState(
IntroEvent event,
) async* {
yield event.map(
load: (_) => IntroState.loaded(currentCastItUrl: _settings.castItUrl, currentLang: _settings.language),
changePage: (e) {
if (!currentState.urlWasSet) {
return currentState.copyWith.call(page: e.newPage);
}
return currentState.copyWith.call(page: e.newPage, urlWasSet: false);
},
urlWasSet: (e) {
//This can happen when the user press the skip button
if (e.url.isNullEmptyOrWhitespace) {
_settings.castItUrl = AppConstants.baseCastItUrl;
}

return currentState.copyWith.call(urlWasSet: true, currentCastItUrl: _settings.castItUrl);
},
languageChanged: (e) => currentState.copyWith.call(currentLang: e.newLang),
);
}

@override
Future<void> close() async {
await _settingSubscription.cancel();
Expand Down
57 changes: 31 additions & 26 deletions CastIt.Android/lib/application/main/main_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,41 @@ class MainBloc extends Bloc<MainEvent, MainState> {

_LoadedState get currentState => state as _LoadedState;

MainBloc(this._logger, this._settings, this._deviceInfoService, this._localeService) : super(MainState.loading());
MainBloc(this._logger, this._settings, this._deviceInfoService, this._localeService) : super(MainState.loading()) {
on<_Init>((event, emit) async {
final updatedState = await _init();
emit(updatedState);
});

@override
Stream<MainState> mapEventToState(
MainEvent event,
) async* {
final s = await event.when(
init: () async {
return _init();
},
themeChanged: (theme) async {
return _loadThemeData(currentState.appTitle, theme, _settings.accentColor, _settings.language);
},
accentColorChanged: (accentColor) async {
return _loadThemeData(currentState.appTitle, _settings.appTheme, accentColor, _settings.language);
},
goToTab: (index) async {
return currentState.copyWith(currentSelectedTab: index);
},
introCompleted: () async {
_settings.isFirstInstall = false;
return currentState.copyWith.call(firstInstall: false);
},
languageChanged: () async => state.map(
on<_ThemeChanged>((event, emit) {
final updatedState = _loadThemeData(currentState.appTitle, event.theme, _settings.accentColor, _settings.language);
emit(updatedState);
});

on<_AccentColorChanged>((event, emit) {
final updatedState = _loadThemeData(currentState.appTitle, _settings.appTheme, event.accentColor, _settings.language);
emit(updatedState);
});

on<_GoToTab>((event, emit) {
final updatedState = currentState.copyWith(currentSelectedTab: event.index);
emit(updatedState);
});

on<_IntroCompleted>((event, emit) {
_settings.isFirstInstall = false;
final updatedState = currentState.copyWith.call(firstInstall: false);
emit(updatedState);
});

on<_LanguageChanged>((event, emit) {
final updatedState = state.map(
loading: (s) => s,
loaded: (s) => s.copyWith.call(language: _localeService.getCurrentLocale()),
),
);
);

yield s;
emit(updatedState);
});
}

Future<MainState> _init() async {
Expand Down
235 changes: 133 additions & 102 deletions CastIt.Android/lib/application/play/play_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:castit/domain/models/models.dart';
import 'package:castit/domain/services/castit_hub_client_service.dart';
Expand All @@ -12,7 +10,118 @@ part 'play_state.dart';
class PlayBloc extends Bloc<PlayEvent, PlayState> {
final CastItHubClientService _castItHub;

bool get isPlaying => state is _PlayingState;

_PlayingState get currentState => state as _PlayingState;

PlayBloc(this._castItHub) : super(PlayState.connected()) {
on<_Connected>((event, emit) {
final updatedState = PlayState.connected();
emit(updatedState);
});

on<_FileLoading>((event, emit) {
final updatedState = PlayState.fileLoading();
emit(updatedState);
});

on<_FileLoadingError>((event, emit) {
final updatedState = PlayState.fileLoadingFailed(msg: event.msg);
emit(updatedState);
});

on<_FileLoaded>((event, emit) {
final updatedState = PlayState.playing(
id: event.file.id,
playListId: event.file.playListId,
filename: event.file.filename,
thumbPath: event.file.thumbnailUrl,
duration: event.file.duration,
isPaused: event.file.isPaused,
currentSeconds: event.file.currentSeconds,
playlistName: event.file.playListName,
loopFile: event.file.loopFile,
loopPlayList: event.file.loopPlayList,
shufflePlayList: event.file.shufflePlayList,
isDraggingSlider: false,
playListTotalDuration: event.file.playListTotalDuration,
playListPlayedTime: event.file.playListPlayedTime,
);
emit(updatedState);
});

on<_FileChanged>((event, emit) {
final updatedState = state.maybeMap(
playing: (state) => state.copyWith(
id: event.file.id,
playListId: event.file.playListId,
filename: event.file.filename,
thumbPath: event.file.thumbnailUrl,
loopFile: event.file.loop,
),
orElse: () => state,
);
emit(updatedState);
});

on<_PlayListChanged>((event, emit) {
final updatedState = state.maybeMap(
playing: (state) => state.copyWith(
playlistName: event.playList.name,
playListTotalDuration: event.playList.totalDuration,
playListPlayedTime: event.playList.playedTime,
shufflePlayList: event.playList.shuffle,
loopPlayList: event.playList.loop,
),
orElse: () => state,
);
emit(updatedState);
});

on<_TimeChanged>(_handleTimeChanged);

on<_Paused>((event, emit) {
if (!isPlaying) {
_emitDefaultState(emit);
return;
}
final updatedState = currentState.copyWith.call(isPaused: true);
emit(updatedState);
});

on<_Stopped>((event, emit) {
if (!isPlaying) {
_emitDefaultState(emit);
return;
}
emit(PlayState.connected());
});

on<_Disconnected>((event, emit) => emit(PlayState.connected()));

on<_SliderDragChanged>((event, emit) {
if (!isPlaying) {
_emitDefaultState(emit);
return;
}
final updatedState = currentState.copyWith.call(isDraggingSlider: event.isSliding);
emit(updatedState);
});

on<_SliderValueChanged>((event, emit) {
if (!isPlaying) {
_emitDefaultState(emit);
return;
}

if (event.triggerGoToSeconds) {
_castItHub.gotoSeconds(event.newValue);
}

final updatedState = currentState.copyWith.call(currentSeconds: event.newValue, isDraggingSlider: !event.triggerGoToSeconds);
emit(updatedState);
});

_castItHub.connected.stream.listen((_) => add(PlayEvent.connected()));

_castItHub.fileLoading.stream.listen((_) => add(PlayEvent.fileLoading()));
Expand Down Expand Up @@ -61,107 +170,29 @@ class PlayBloc extends Bloc<PlayEvent, PlayState> {
_castItHub.disconnected.stream.listen((_) => add(PlayEvent.disconnected()));
}

bool get isPlaying => state is _PlayingState;
void _handleTimeChanged(_TimeChanged event, Emitter<PlayState> emit) {
if (!isPlaying) {
_emitDefaultState(emit);
return;
}
if (currentState.isDraggingSlider!) {
final updatedState = currentState.copyWith.call(isPaused: false);
emit(updatedState);
return;
}
//A live stream is being played
if (currentState.duration! <= 0) {
final updatedState = currentState.copyWith.call(currentSeconds: event.seconds, isPaused: false);
emit(updatedState);
return;
}

_PlayingState get currentState => state as _PlayingState;
final s = event.seconds >= currentState.duration! ? currentState.duration : event.seconds;
final updatedState = currentState.copyWith.call(currentSeconds: s, isPaused: false);
emit(updatedState);
}

@override
Stream<PlayState> mapEventToState(PlayEvent event) async* {
final s = event.map(
connected: (_) => PlayState.connected(),
fileLoading: (_) => PlayState.fileLoading(),
fileLoadingError: (e) => PlayState.fileLoadingFailed(msg: e.msg),
fileLoaded: (e) => PlayState.playing(
id: e.file.id,
playListId: e.file.playListId,
filename: e.file.filename,
thumbPath: e.file.thumbnailUrl,
duration: e.file.duration,
isPaused: e.file.isPaused,
currentSeconds: e.file.currentSeconds,
playlistName: e.file.playListName,
loopFile: e.file.loopFile,
loopPlayList: e.file.loopPlayList,
shufflePlayList: e.file.shufflePlayList,
isDraggingSlider: false,
playListTotalDuration: e.file.playListTotalDuration,
playListPlayedTime: e.file.playListPlayedTime,
),
fileChanged: (e) => state.maybeMap(
playing: (state) => state.copyWith(
id: e.file.id,
playListId: e.file.playListId,
filename: e.file.filename,
thumbPath: e.file.thumbnailUrl,
loopFile: e.file.loop,
),
orElse: () => state,
),
playListChanged: (e) => state.maybeMap(
playing: (state) => state.copyWith(
playlistName: e.playList.name,
playListTotalDuration: e.playList.totalDuration,
playListPlayedTime: e.playList.playedTime,
shufflePlayList: e.playList.shuffle,
loopPlayList: e.playList.loop,
),
orElse: () => state,
),
timeChanged: (e) {
if (!isPlaying) {
return null;
}
if (currentState.isDraggingSlider!) {
return currentState.copyWith.call(isPaused: false);
}
//A live stream is being played
if (currentState.duration! <= 0) {
return currentState.copyWith.call(currentSeconds: e.seconds, isPaused: false);
}
final s = e.seconds >= currentState.duration! ? currentState.duration : e.seconds;
return currentState.copyWith.call(currentSeconds: s, isPaused: false);
},
paused: (_) {
if (!isPlaying) {
return null;
}
return currentState.copyWith.call(isPaused: true);
},
stopped: (_) {
if (!isPlaying) {
return null;
}
return PlayState.connected();
},
disconnected: (_) {
if (!isPlaying) {
return null;
}
return PlayState.connected();
},
sliderDragChanged: (e) {
if (!isPlaying) {
return null;
}
return currentState.copyWith.call(isDraggingSlider: e.isSliding);
},
sliderValueChanged: (e) {
if (!isPlaying) {
return null;
}

if (e.triggerGoToSeconds) {
_castItHub.gotoSeconds(e.newValue);
}

return currentState.copyWith.call(currentSeconds: e.newValue, isDraggingSlider: !e.triggerGoToSeconds);
},
);

if (s != null) {
yield s;
} else {
yield PlayState.connected();
}
void _emitDefaultState(Emitter<PlayState> emit) {
emit(PlayState.connected());
}
}
Loading

0 comments on commit 4282c36

Please sign in to comment.