Skip to content

Commit

Permalink
[CastIt.Android] Added two blocs to avoid rebuilding the ui in the lists
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfteam committed Jul 3, 2021
1 parent e1a53cd commit 8417a95
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:castit/application/bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'played_file_item_bloc.freezed.dart';
part 'played_file_item_event.dart';
part 'played_file_item_state.dart';

class PlayedFileItemBloc extends Bloc<PlayedFileItemEvent, PlayedFileItemState> {
final ServerWsBloc _serverWsBloc;

PlayedFileItemBloc(this._serverWsBloc) : super(const PlayedFileItemState.notPlaying()) {
_serverWsBloc.fileChanged.stream.listen((event) {
final file = event.item2;
add(PlayedFileItemEvent.playing(
id: file.id,
playListId: file.playListId,
playedPercentage: file.playedPercentage,
fullTotalDuration: file.fullTotalDuration,
));
});
}

@override
Stream<PlayedFileItemState> mapEventToState(PlayedFileItemEvent event) async* {
final s = event.map(
playing: (e) => PlayedFileItemState.playing(
id: e.id,
playListId: e.playListId,
playedPercentage: e.playedPercentage,
fullTotalDuration: e.fullTotalDuration,
),
endReached: (_) => const PlayedFileItemState.notPlaying(),
);

yield s;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
part of 'played_file_item_bloc.dart';

@freezed
class PlayedFileItemEvent with _$PlayedFileItemEvent {
factory PlayedFileItemEvent.playing({
required int id,
required int playListId,
required double playedPercentage,
required String fullTotalDuration,
}) = _Playing;

factory PlayedFileItemEvent.endReached() = _EndReached;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
part of 'played_file_item_bloc.dart';

@freezed
class PlayedFileItemState with _$PlayedFileItemState {
const factory PlayedFileItemState.notPlaying() = _NotPlayingState;

const factory PlayedFileItemState.playing({
required int id,
required int playListId,
required double playedPercentage,
required String fullTotalDuration,
}) = _LoadedState;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:castit/application/bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'played_play_list_item_bloc.freezed.dart';
part 'played_play_list_item_event.dart';
part 'played_play_list_item_state.dart';

class PlayedPlayListItemBloc extends Bloc<PlayedPlayListItemEvent, PlayedPlayListItemState> {
final ServerWsBloc _serverWsBloc;

PlayedPlayListItemBloc(this._serverWsBloc) : super(const PlayedPlayListItemState.notPlaying()) {
_serverWsBloc.fileLoaded.stream.listen((file) {
add(PlayedPlayListItemEvent.playing(id: file.playListId, totalDuration: file.playListTotalDuration!));
});

_serverWsBloc.fileEndReached.stream.listen((file) {
add(PlayedPlayListItemEvent.endReached());
});
}

@override
Stream<PlayedPlayListItemState> mapEventToState(PlayedPlayListItemEvent event) async* {
final s = event.map(
playing: (e) => PlayedPlayListItemState.playing(id: e.id, totalDuration: e.totalDuration),
endReached: (_) => const PlayedPlayListItemState.notPlaying(),
);

yield s;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
part of 'played_play_list_item_bloc.dart';

@freezed
class PlayedPlayListItemEvent with _$PlayedPlayListItemEvent {
factory PlayedPlayListItemEvent.playing({
required int id,
required String totalDuration,
}) = _Playing;

factory PlayedPlayListItemEvent.endReached() = _EndReached;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
part of 'played_play_list_item_bloc.dart';

@freezed
class PlayedPlayListItemState with _$PlayedPlayListItemState {
const factory PlayedPlayListItemState.notPlaying() = _NotPlayingState;

const factory PlayedPlayListItemState.playing({
required int id,
required String totalDuration,
}) = _LoadedState;
}

0 comments on commit 8417a95

Please sign in to comment.