Skip to content

Instantly share code, notes, and snippets.

@pawelsa
pawelsa / movie_repository.dart
Last active June 26, 2022 17:57
Methods to observe the database and map models to view data
Stream<List<ContentDetailData>> observeDetailedUpcoming() =>
_movieDao.observeAllUpcoming().asyncMap(getDetailsFromDb);
Stream<List<ContentDetailData>> observeDetailedPopular() =>
_movieDao.observeAllPopular().asyncMap(getDetailsFromDb);
@pawelsa
pawelsa / movie_page.dart
Created June 26, 2022 17:48
Showing snackbar from the error cause obtained by executing usecase
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 June 26, 2022 17:44
Before and after change to use Result
// 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) =>
// 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
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);
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)));
}
@Freezed(
maybeMap: false,
maybeWhen: false,
)
class ContentListResponse
with _$ContentListResponse
implements SuccessfulResponse {
@JsonSerializable(explicitToJson: true)
@Implements<SuccessfulResponse>()
factory ContentListResponse({
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));
}
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));
}
@pawelsa
pawelsa / OverflowRow
Created November 18, 2020 15:52
This is used when items in a row cannot fit and we want to indicate it by using some overflow indicator. In text we often have '…', so this does the same for row @composables
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)