Skip to content

Instantly share code, notes, and snippets.

@pawelsa
pawelsa / CutHalfShapeType.kt
Created November 2, 2020 16:41
Shape class for Jetpack compose, which cuts off the specified part of the widget it was applied to. It is used using clip method in Modifier
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Outline
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.unit.Density
enum class CutHalfShapeType { TOP, BOTTOM, RIGHT, LEFT }
/**
@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)
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));
}
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));
}
@Freezed(
maybeMap: false,
maybeWhen: false,
)
class ContentListResponse
with _$ContentListResponse
implements SuccessfulResponse {
@JsonSerializable(explicitToJson: true)
@Implements<SuccessfulResponse>()
factory ContentListResponse({
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)));
}
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);
// 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
@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) =>
@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"),
);
});
}