Skip to content

Instantly share code, notes, and snippets.

View movie_page.dart
Widget _buildBody(StreamData<List<ContentData>> streamData){
return Stack(
children: [
ListView.builder(
itemCount: streamData.data.length,
itemBuilder: (context, index) {
final item = streamData.data[index];
return CardItem(
onTap: () => _onCardTap(context, item),
content: item,
View movie_repository.dart
final LoadingStatus _upcomingMoviesLoadingStatus = LoadingStatus();
Future<Result> getUpcomingMovies(int page) {
_upcomingMoviesLoadingStatus.isLoading = true;
return _movieApi.getUpcoming(page).then((content) {
if (content is ContentListResponse) {
final result = _getMoviesWithResp(content, false);
_upcomingMoviesLoadingStatus.isLoading = false;
return result;
}
View loading_status.dart
extension LoadingStatusStreamExtension<T> on Stream<T> {
DataStream<T> withLoading(LoadingStatus loadingStatus) {
return combineLatest<bool, StreamData<T>>(loadingStatus.listen(),
(data, isLoading) {
if (isLoading) {
return LoadingData(data);
}
return ReadyData(data);
});
}
View movie_repository.dart
final LoadingStatus _upcomingMoviesLoadingStatus = LoadingStatus();
DataStream<List<ContentDetailData>> observeDetailedUpcoming() =>
_movieDao.observeAllUpcoming().asyncMap(getDetailsFromDb).combineLatest<bool,
StreamData<List<ContentDetailData>>>(_upcomingMoviesLoadingStatus.listen(),
(data, isLoading) {
if (isLoading) {
return LoadingData(data);
}
return ReadyData(data);
});
View loading_status.dart
class LoadingStatus {
BehaviorSubject<bool>? _loadingController;
bool _isLoading = false;
bool _streamedInitialEvent = false;
set isLoading(bool isLoading) {
_isLoading = isLoading;
_publishLoadingState();
}
View StreamData.dart
@freezed
class StreamData<T> with _$StreamData<T> {
factory StreamData.ready(T data) = ReadyData<T>;
factory StreamData.loading(T data) = LoadingData<T>;
}
@pawelsa
pawelsa / result.dart
Created Jun 26, 2022
Results that can be returned from repositories
View result.dart
@freezed
class ErrorCause with _$ErrorCause {
const factory ErrorCause.noInternet() = NoInternetError;
const factory ErrorCause.databaseSave() = DatabaseSaveError;
const factory ErrorCause.unknown() = UnknownError;
}
@freezed
@pawelsa
pawelsa / movie_repository.dart
Last active Jun 26, 2022
Methods to observe the database and map models to view data
View movie_repository.dart
Stream<List<ContentDetailData>> observeDetailedUpcoming() =>
_movieDao.observeAllUpcoming().asyncMap(getDetailsFromDb);
Stream<List<ContentDetailData>> observeDetailedPopular() =>
_movieDao.observeAllPopular().asyncMap(getDetailsFromDb);
@pawelsa
pawelsa / movie_page.dart
Created Jun 26, 2022
Showing snackbar from the error cause obtained by executing usecase
View movie_page.dart
final loadMoviesResult = await loadPopularMoviesUsecase();
if(result is ErrorResult){
final messenger = (String msg) => ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(msg)));
result.errorCause.whenOrNull(error: (cause) {
return cause.when(
noInternet: () => messenger("Lost connection when loading popular movies"),
unknown: () => messenger("Unexpected error happen when loading popular movies"),
);
});
}
@pawelsa
pawelsa / movie_repository.dart
Created Jun 26, 2022
Before and after change to use Result
View movie_repository.dart
// Before
Future<List<ContentDetailData>> getPopularMovies(int page) =>
_movieApi.getPopular(page).then((content) {
if (content is ContentListResponse) {
return _getMovies(content, true);
}
return <ContentDetailData>[];
});
// After
Future<Result> getPopularMovies(int page) =>