This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Stream<List<ContentDetailData>> observeDetailedUpcoming() => | |
_movieDao.observeAllUpcoming().asyncMap(getDetailsFromDb); | |
Stream<List<ContentDetailData>> observeDetailedPopular() => | |
_movieDao.observeAllPopular().asyncMap(getDetailsFromDb); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | |
); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) => |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Before | |
Future<List<ContentDetailData>> getPopularMovies(int page) => | |
_movieApi.getPopular(page).then((content) => _getMovies(content, true)); | |
Future<List<ContentDetailData>> _getMovies( | |
PageContent content, bool arePopular) async { | |
// save, map and perform other operations | |
} | |
// After |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class Api { | |
static const baseUrl = "https://api.themoviedb.org/3/movie/"; | |
Future<Response> get({ | |
required Uri uri, | |
required Response Function(String body) mapper, | |
Map<String, String>? headers, | |
}) { | |
uri = _prepareUri(uri); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Future<Response> getPopular(int page) { | |
return get( | |
Uri.parse( | |
"${baseUrl}popular?api_key=${Secret.apiKey}&language=en-US&page=$page", | |
), | |
(value) => ContentListResponse.fromJson(jsonDecode(value))); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Freezed( | |
maybeMap: false, | |
maybeWhen: false, | |
) | |
class ContentListResponse | |
with _$ContentListResponse | |
implements SuccessfulResponse { | |
@JsonSerializable(explicitToJson: true) | |
@Implements<SuccessfulResponse>() | |
factory ContentListResponse({ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class Response {} | |
abstract class SuccessfulResponse extends Response {} | |
abstract class ErrorResponse extends Response {} | |
abstract class Api { | |
Future<Response> get(Uri uri, Response Function(String body) mapper) { | |
return http.get(uri).then((value) => mapper(value.body)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Future<PageContent> getPopular(int page) { | |
return http.get( | |
Uri.parse( | |
"${baseUrl}popular?api_key=${Secret.apiKey}&language=en-US&page=$page", | |
), | |
) | |
.then((value) => apiMovieContentFromJson(value.body)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.compose.runtime.Composable | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.layout.Layout | |
import androidx.compose.ui.layout.Placeable | |
import androidx.compose.ui.unit.Constraints | |
import androidx.compose.ui.unit.Dp | |
import androidx.compose.ui.unit.dp | |
fun <T> List<T>.skipLast(skip: Int) = this.take(this.size - skip) |