Skip to content

Commit

Permalink
refactor(types): spit the types from types.dart into separate types
Browse files Browse the repository at this point in the history
Also renames `FutureRes` to a more generic functional programming name
`Result`.
  • Loading branch information
gruvw committed May 27, 2024
1 parent 945ff5f commit f7a504a
Show file tree
Hide file tree
Showing 22 changed files with 56 additions and 53 deletions.
14 changes: 7 additions & 7 deletions lib/views/components/async/circular_value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import "package:flutter_hooks/flutter_hooks.dart";
import "package:proxima/views/components/async/error_alert.dart";
import "package:proxima/views/components/async/logo_progress_indicator.dart";
import "package:proxima/views/components/async/offline_alert.dart";
import "package:proxima/views/helpers/types.dart";
import "package:proxima/views/helpers/types/result.dart";

/// Utility widget used to display a [LogoProgressIndicator] while waiting for a
/// [Future] of type [FutureRes] to complete; and another widget once the data resolves.
/// [Future] of type [Result] to complete; and another widget once the data resolves.
/// In case the data resolves to an error, an [ErrorAlert] dialog is shown, and
/// a fallback widget is displayed. The default fallback widget is empty, but it
/// can be overridden.
class CircularValue<T> extends HookWidget {
final Future<FutureRes<T, Object?>> future;
final Future<Result<T, Object?>> future;
final Widget Function(BuildContext context, T data) builder;
final Widget Function(BuildContext context, Object error) fallbackBuilder;

Expand All @@ -36,18 +36,18 @@ class CircularValue<T> extends HookWidget {
/// Constructor for the [CircularValue] widget.
/// [future] is the underlying [Future] that controls the display.
/// [builder] is the widget to display when the [future] completes
/// with valid [FutureRes.value].
/// with valid [Result.value].
/// [fallbackBuilder] is the widget to display when the [future] errors
/// or completes with [FutureRes.error].
/// or completes with [Result.error].
/// The default [fallbackBuilder] is an empty [SizedBox].
CircularValue({
super.key,
required Future<FutureRes<T, Object?>> future,
required Future<Result<T, Object?>> future,
required this.builder,
this.fallbackBuilder = defaultFallback,
}) : future = future
.timeout(offlineTimeout)
.onError((error, stackTrace) => FutureRes.error(timeoutErrorTag));
.onError((error, stackTrace) => Result.error(timeoutErrorTag));

@override
Widget build(BuildContext context) {
Expand Down
2 changes: 1 addition & 1 deletion lib/views/components/async/loading_icon_button.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "package:flutter/material.dart";
import "package:flutter_hooks/flutter_hooks.dart";
import "package:proxima/views/helpers/types.dart";
import "package:proxima/views/helpers/types/future_void_callback.dart";

enum LoadingState {
still,
Expand Down
28 changes: 0 additions & 28 deletions lib/views/helpers/types.dart

This file was deleted.

1 change: 1 addition & 0 deletions lib/views/helpers/types/future_void_callback.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
typedef FutureVoidCallback = Future<void> Function();
30 changes: 30 additions & 0 deletions lib/views/helpers/types/result.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/// Wrapper class to represent the result of a computation.
/// It can take the form of a value [Result.value]
/// or the form of an error [Result.error].
/// We use it to wrap a future instead of throwing an exception.
/// Useful to wrap futures coming from a provider.
class Result<V, E> {
final V? value;
final E? error;

Result._(this.value, this.error);

factory Result.value(V value) {
return Result._(value, null);
}

factory Result.error(E error) {
return Result._(null, error);
}

bool get isError => error != null;
}

/// Extention on [Future] to automatically wrap them in a [Result]
/// instead of throwing an error.
extension MapFutureRes<T> on Future<T> {
Future<Result<T, Object?>> mapRes() {
return then((value) => Result.value(value))
.onError((error, stackTrace) => Result<T, Object?>.error(error));
}
}
2 changes: 1 addition & 1 deletion lib/views/pages/create_account/create_account_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "package:proxima/services/authentication/auth_login_service.dart";
import "package:proxima/viewmodels/create_account_view_model.dart";
import "package:proxima/viewmodels/login_view_model.dart";
import "package:proxima/views/components/async/circular_value.dart";
import "package:proxima/views/helpers/types.dart";
import "package:proxima/views/helpers/types/result.dart";
import "package:proxima/views/navigation/leading_back_button/leading_back_button.dart";
import "package:proxima/views/navigation/routes.dart";
import "package:proxima/views/pages/create_account/create_account_form.dart";
Expand Down
2 changes: 1 addition & 1 deletion lib/views/pages/home/content/challenge/challenge_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import "package:hooks_riverpod/hooks_riverpod.dart";
import "package:proxima/viewmodels/challenge_view_model.dart";
import "package:proxima/views/components/async/circular_value.dart";
import "package:proxima/views/components/async/error_refresh_page.dart";
import "package:proxima/views/helpers/types.dart";
import "package:proxima/views/helpers/types/result.dart";
import "package:proxima/views/pages/home/content/challenge/challenge_card.dart";

class ChallengeList extends ConsumerWidget {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "package:flutter/material.dart";
import "package:proxima/models/ui/post_details.dart";
import "package:proxima/views/helpers/types.dart";
import "package:proxima/views/helpers/types/future_void_callback.dart";
import "package:proxima/views/pages/home/content/feed/components/post_card.dart";

class PostList extends StatelessWidget {
Expand Down
2 changes: 1 addition & 1 deletion lib/views/pages/home/content/feed/post_feed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "package:proxima/viewmodels/posts_feed_view_model.dart";
import "package:proxima/views/components/async/circular_value.dart";
import "package:proxima/views/components/async/error_refresh_page.dart";
import "package:proxima/views/components/options/feed/feed_sort_option_chips.dart";
import "package:proxima/views/helpers/types.dart";
import "package:proxima/views/helpers/types/result.dart";
import "package:proxima/views/navigation/routes.dart";
import "package:proxima/views/pages/home/content/feed/components/post_list.dart";

Expand Down
2 changes: 1 addition & 1 deletion lib/views/pages/home/content/map/map_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "package:proxima/viewmodels/map/map_view_model.dart";
import "package:proxima/views/components/async/circular_value.dart";
import "package:proxima/views/components/async/error_refresh_page.dart";
import "package:proxima/views/components/options/map/map_selection_option_chips.dart";
import "package:proxima/views/helpers/types.dart";
import "package:proxima/views/helpers/types/result.dart";
import "package:proxima/views/pages/home/content/map/components/post_map.dart";

/// This widget displays a map with chips to select the type of map.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "package:flutter/material.dart";
import "package:proxima/models/ui/ranking/ranking_details.dart";
import "package:proxima/views/helpers/types.dart";
import "package:proxima/views/helpers/types/future_void_callback.dart";
import "package:proxima/views/pages/home/content/ranking/components/ranking_card.dart";

/// A widget that displays a list of ranking cards.
Expand Down
2 changes: 1 addition & 1 deletion lib/views/pages/home/content/ranking/ranking_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "package:flutter/material.dart";
import "package:hooks_riverpod/hooks_riverpod.dart";
import "package:proxima/viewmodels/users_ranking_view_model.dart";
import "package:proxima/views/components/async/circular_value.dart";
import "package:proxima/views/helpers/types.dart";
import "package:proxima/views/helpers/types/result.dart";
import "package:proxima/views/pages/home/content/ranking/components/ranking_widget.dart";

/// The Ranking page home content that is accessible via the bottom
Expand Down
2 changes: 1 addition & 1 deletion lib/views/pages/new_post/new_post_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import "package:flutter_hooks/flutter_hooks.dart";
import "package:hooks_riverpod/hooks_riverpod.dart";
import "package:proxima/viewmodels/new_post_view_model.dart";
import "package:proxima/views/components/async/circular_value.dart";
import "package:proxima/views/helpers/types.dart";
import "package:proxima/views/helpers/types/result.dart";

class NewPostForm extends HookConsumerWidget {
const NewPostForm({super.key});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import "package:hooks_riverpod/hooks_riverpod.dart";
import "package:proxima/models/database/post/post_id_firestore.dart";
import "package:proxima/viewmodels/new_comment_view_model.dart";
import "package:proxima/views/components/async/circular_value.dart";
import "package:proxima/views/helpers/types.dart";
import "package:proxima/views/helpers/types/result.dart";
import "package:proxima/views/pages/post/components/new_comment/new_comment_button.dart";
import "package:proxima/views/pages/post/components/new_comment/new_comment_textfield.dart";
import "package:proxima/views/pages/post/components/new_comment/new_comment_user_avatar.dart";
Expand Down
2 changes: 1 addition & 1 deletion lib/views/pages/post/post_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import "package:hooks_riverpod/hooks_riverpod.dart";
import "package:proxima/models/ui/post_details.dart";
import "package:proxima/viewmodels/comments_view_model.dart";
import "package:proxima/views/components/async/circular_value.dart";
import "package:proxima/views/helpers/types.dart";
import "package:proxima/views/helpers/types/result.dart";
import "package:proxima/views/navigation/leading_back_button/leading_back_button.dart";
import "package:proxima/views/pages/post/components/bottom_bar_add_comment.dart";
import "package:proxima/views/pages/post/components/comment/comment_list.dart";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "package:flutter/material.dart";
import "package:proxima/views/components/async/loading_icon_button.dart";
import "package:proxima/views/helpers/types.dart";
import "package:proxima/views/helpers/types/future_void_callback.dart";
import "package:proxima/views/pages/profile/components/info_cards/profile_info_pop_up.dart";

/// Info card for the profile page (post or comment)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "package:flutter/material.dart";
import "package:proxima/views/helpers/types.dart";
import "package:proxima/views/helpers/types/future_void_callback.dart";

/// This widget defines the style of the columns in the profile page,
/// such as the posts and comments columns
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "package:flutter/material.dart";
import "package:proxima/views/components/async/loading_icon_button.dart";
import "package:proxima/views/components/content/info_pop_up.dart";
import "package:proxima/views/helpers/types.dart";
import "package:proxima/views/helpers/types/future_void_callback.dart";

class ProfileInfoPopUp extends StatelessWidget {
//key of the button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "package:flutter/material.dart";
import "package:hooks_riverpod/hooks_riverpod.dart";
import "package:proxima/viewmodels/user_comments_view_model.dart";
import "package:proxima/views/components/async/circular_value.dart";
import "package:proxima/views/helpers/types.dart";
import "package:proxima/views/helpers/types/result.dart";
import "package:proxima/views/pages/profile/components/info_cards/profile_info_card.dart";
import "package:proxima/views/pages/profile/components/info_cards/profile_info_column.dart";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "package:flutter/widgets.dart";
import "package:hooks_riverpod/hooks_riverpod.dart";
import "package:proxima/viewmodels/user_posts_view_model.dart";
import "package:proxima/views/components/async/circular_value.dart";
import "package:proxima/views/helpers/types.dart";
import "package:proxima/views/helpers/types/result.dart";
import "package:proxima/views/pages/profile/components/info_cards/profile_info_card.dart";
import "package:proxima/views/pages/profile/components/info_cards/profile_info_column.dart";

Expand Down
2 changes: 1 addition & 1 deletion lib/views/pages/profile/profile_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "package:flutter/material.dart";
import "package:hooks_riverpod/hooks_riverpod.dart";
import "package:proxima/viewmodels/profile_view_model.dart";
import "package:proxima/views/components/async/circular_value.dart";
import "package:proxima/views/helpers/types.dart";
import "package:proxima/views/helpers/types/result.dart";
import "package:proxima/views/pages/profile/components/profile_app_bar.dart";
import "package:proxima/views/pages/profile/components/profile_data/profile_user_comments.dart";
import "package:proxima/views/pages/profile/components/profile_data/profile_user_posts.dart";
Expand Down
2 changes: 1 addition & 1 deletion test/mocks/providers/provider_circular_value.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "package:flutter/material.dart";
import "package:hooks_riverpod/hooks_riverpod.dart";
import "package:proxima/views/components/async/circular_value.dart";
import "package:proxima/views/helpers/types.dart";
import "package:proxima/views/helpers/types/result.dart";

Widget _defaultBuilder(BuildContext context, int value) {
return const Text("Completed");
Expand Down

0 comments on commit f7a504a

Please sign in to comment.