Skip to content

Commit

Permalink
refactor: refactor Either to FlowyResult
Browse files Browse the repository at this point in the history
  • Loading branch information
Jayaprakash-dev committed Feb 27, 2024
1 parent 1569675 commit 09b1330
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'package:appflowy/workspace/application/view/view_service.dart';
import 'package:appflowy/workspace/application/workspace/overview/overview_listener.dart';
import 'package:appflowy_backend/log.dart';
import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart';
import 'package:dartz/dartz.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

Expand Down Expand Up @@ -69,18 +68,18 @@ class WorkspaceOverviewBloc

if (update.deleteChildViews.isNotEmpty) {
final res = update.parentViewId == state.view.id
? Tuple2(state.view, 0)
? (state.view, 0)
: _getView(update.parentViewId, state.view);

if (res != null) {
final ViewPB view = res.value1;
final int idx = res.value2;
final ViewPB view = res.$1;
final int idx = res.$2;

ViewPB? parentView;
if (view.id != state.view.id && view.hasParentViewId()) {
parentView = view.parentViewId == state.view.id
? state.view
: _getView(view.parentViewId, state.view)?.value1;
: _getView(view.parentViewId, state.view)?.$1;
}

final childViews = [...view.childViews];
Expand All @@ -103,7 +102,7 @@ class WorkspaceOverviewBloc
// Retrieve the view specified in `update.parentViewId` from the cached views
// stored in this state to determine if a rebuild is necessary.
if (update.updateChildViews.isNotEmpty) {
final view = _getView(update.parentViewId, state.view)?.value1;
final view = _getView(update.parentViewId, state.view)?.$1;
final childViews = view != null ? view.childViews : <ViewPB>[];

if (_isRebuildRequired(childViews, update.updateChildViews)) {
Expand Down Expand Up @@ -142,11 +141,11 @@ class WorkspaceOverviewBloc
return false;
}

Tuple2? _getView(String viewId, ViewPB view) {
(ViewPB, int)? _getView(String viewId, ViewPB view) {
for (int i = 0; i < view.childViews.length; i++) {
final child = view.childViews[i];
if (child.id == viewId) {
return Tuple2(child, i);
return (child, i);
}

final result = _getView(viewId, child);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-folder/icon.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart';
import 'package:appflowy_editor/appflowy_editor.dart' hide Log;
import 'package:appflowy_result/appflowy_result.dart';
import 'package:collection/collection.dart';
import 'package:dartz/dartz.dart' hide State;
import 'package:easy_localization/easy_localization.dart' hide TextDirection;
import 'package:flowy_infra_ui/flowy_infra_ui.dart';
import 'package:flowy_infra_ui/style_widget/hover.dart';
Expand Down Expand Up @@ -104,7 +104,7 @@ class _OverviewBlockWidgetState extends State<WorkspaceOverviewBlockWidget>
late final Animation<double> _curvedAnimationExpansionController;
static const _expansionAnimationDuration = Duration(milliseconds: 400);

late Future<Either<ViewPB, FlowyError>>? _future;
late Future<FlowyResult<ViewPB, FlowyError>>? _future;

@override
void initState() {
Expand All @@ -129,13 +129,13 @@ class _OverviewBlockWidgetState extends State<WorkspaceOverviewBlockWidget>

@override
Widget build(BuildContext context) {
return FutureBuilder<Either<ViewPB, FlowyError>>(
return FutureBuilder<FlowyResult<ViewPB, FlowyError>>(
future: _future,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done &&
snapshot.hasData &&
snapshot.data != null) {
final view = snapshot.data!.getLeftOrNull<ViewPB>();
final view = snapshot.data!.toNullable();
if (view == null) return _buildErrorWidget(viewId);

return BlocProvider<WorkspaceOverviewBloc>(
Expand Down Expand Up @@ -397,7 +397,7 @@ class OverviewItemWidget extends StatelessWidget {

Future<ViewPB?> _fetchView(String id) async {
final view = await ViewBackendService.getView(id).then(
(value) => value.swap().toOption().toNullable(),
(value) => value.toNullable(),
);

if (view == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ class ViewBackendService {
return result;
}

static Future<Either<ViewPB, FlowyError>> getAllLevelOfViews(
static Future<FlowyResult<ViewPB, FlowyError>> getAllLevelOfViews(
String viewID,
) async {
final payload = ViewIdPB.create()..value = viewID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'package:appflowy_backend/protobuf/flowy-folder/notification.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-notification/subject.pb.dart';
import 'package:appflowy_backend/rust_stream.dart';
import 'package:dartz/dartz.dart';
import 'package:appflowy_result/appflowy_result.dart';

// parent view of overview block updated
typedef ParentViewUpdateNotifier = void Function(ViewPB);
Expand Down Expand Up @@ -55,7 +55,7 @@ class WorkspaceOverviewListener {

void _handleObservableType(
FolderNotification ty,
Either<Uint8List, FlowyError> result,
FlowyResult<Uint8List, FlowyError> result,
) {
switch (ty) {
case FolderNotification.DidUpdateWorkspaceOverviewParentView:
Expand Down Expand Up @@ -84,12 +84,12 @@ class WorkspaceOverviewListener {
/// Registers an overview block listener Id in the backend, allowing us to receive
/// notifications of [FolderNotification.DidUpdateWorkspaceOverviewChildViews] from
/// all levels of child views to the specified parent view Id listener.
static Future<Either<Unit, FlowyError>> addListenerId(String viewId) {
static Future<FlowyResult<void, FlowyError>> addListenerId(String viewId) {
final payload = ViewIdPB.create()..value = viewId;
return FolderEventRegisterWorkspaceOverviewListenerId(payload).send();
}

static Future<Either<Unit, FlowyError>> removeListener(String viewId) {
static Future<FlowyResult<void, FlowyError>> removeListener(String viewId) {
final payload = ViewIdPB.create()..value = viewId;
return FolderEventRemoveWorkspaceOverviewListenerId(payload).send();
}
Expand Down

0 comments on commit 09b1330

Please sign in to comment.