feat: use result instead of either (#4724)

* feat: use result instead of either

* chore: remove dartz
This commit is contained in:
Lucas.Xu
2024-02-24 20:54:10 +07:00
committed by GitHub
parent 236b5bfe90
commit 2abb396467
190 changed files with 1813 additions and 1527 deletions

View File

@ -32,7 +32,7 @@ class WindowSizeManager {
final defaultWindowSize = jsonEncode({height: 600.0, width: 800.0});
final windowSize = await getIt<KeyValueStorage>().get(KVKeys.windowSize);
final size = json.decode(
windowSize.getOrElse(() => defaultWindowSize),
windowSize ?? defaultWindowSize,
);
return Size(size[width]!, size[height]!);
}
@ -49,12 +49,10 @@ class WindowSizeManager {
Future<Offset?> getPosition() async {
final position = await getIt<KeyValueStorage>().get(KVKeys.windowPosition);
return position.fold(
() => null,
(r) {
final offset = json.decode(r);
return Offset(offset[dx], offset[dy]);
},
);
if (position == null) {
return null;
}
final offset = json.decode(position);
return Offset(offset[dx], offset[dy]);
}
}

View File

@ -16,7 +16,7 @@ import 'package:appflowy_backend/log.dart';
import 'package:appflowy_backend/protobuf/flowy-error/code.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart';
import 'package:dartz/dartz.dart';
import 'package:appflowy_result/appflowy_result.dart';
import 'package:flutter/material.dart';
import 'package:url_protocol/url_protocol.dart';
@ -29,7 +29,7 @@ class AppFlowyCloudDeepLink {
await _handleUri(uri);
},
onError: (Object err, StackTrace stackTrace) {
Log.error('on deeplink stream error: ${err.toString()}', stackTrace);
Log.error('on DeepLink stream error: ${err.toString()}', stackTrace);
_deeplinkSubscription?.cancel();
_deeplinkSubscription = null;
},
@ -46,7 +46,7 @@ class AppFlowyCloudDeepLink {
final _appLinks = AppLinks();
ValueNotifier<DeepLinkResult?>? _stateNotifier = ValueNotifier(null);
Completer<Either<FlowyError, UserProfilePB>>? _completer;
Completer<FlowyResult<UserProfilePB, FlowyError>>? _completer;
// The AppLinks is a singleton, so we need to cancel the previous subscription
// before creating a new one.
@ -58,8 +58,8 @@ class AppFlowyCloudDeepLink {
_stateNotifier = null;
}
void resigerCompleter(
Completer<Either<FlowyError, UserProfilePB>> completer,
void registerCompleter(
Completer<FlowyResult<UserProfilePB, FlowyError>> completer,
) {
_completer = completer;
}
@ -86,11 +86,11 @@ class AppFlowyCloudDeepLink {
_stateNotifier?.value = DeepLinkResult(state: DeepLinkState.none);
if (uri == null) {
Log.error('onDeepLinkError: Unexpect empty deep link callback');
_completer?.complete(left(AuthError.emptyDeeplink));
Log.error('onDeepLinkError: Unexpected empty deep link callback');
_completer?.complete(FlowyResult.failure(AuthError.emptyDeepLink));
_completer = null;
}
return _isAuthCallbackDeeplink(uri!).fold(
return _isAuthCallbackDeepLink(uri!).fold(
(_) async {
final deviceId = await getDeviceId();
final payload = OauthSignInPB(
@ -101,9 +101,8 @@ class AppFlowyCloudDeepLink {
},
);
_stateNotifier?.value = DeepLinkResult(state: DeepLinkState.loading);
final result = await UserEventOauthSignIn(payload)
.send()
.then((value) => value.swap());
final result =
await UserEventOauthSignIn(payload).send().then((value) => value);
_stateNotifier?.value = DeepLinkResult(
state: DeepLinkState.finish,
@ -112,6 +111,9 @@ class AppFlowyCloudDeepLink {
// If there is no completer, runAppFlowy() will be called.
if (_completer == null) {
await result.fold(
(_) async {
await runAppFlowy();
},
(err) {
Log.error(err);
final context = AppGlobals.rootNavKey.currentState?.context;
@ -122,9 +124,6 @@ class AppFlowyCloudDeepLink {
);
}
},
(_) async {
await runAppFlowy();
},
);
} else {
_completer?.complete(result);
@ -132,7 +131,7 @@ class AppFlowyCloudDeepLink {
}
},
(err) {
Log.error('onDeepLinkError: Unexpect deep link: $err');
Log.error('onDeepLinkError: Unexpected deep link: $err');
if (_completer == null) {
final context = AppGlobals.rootNavKey.currentState?.context;
if (context != null) {
@ -142,19 +141,19 @@ class AppFlowyCloudDeepLink {
);
}
} else {
_completer?.complete(left(err));
_completer?.complete(FlowyResult.failure(err));
_completer = null;
}
},
);
}
Either<(), FlowyError> _isAuthCallbackDeeplink(Uri uri) {
FlowyResult<void, FlowyError> _isAuthCallbackDeepLink(Uri uri) {
if (uri.fragment.contains('access_token')) {
return left(());
return FlowyResult.success(null);
}
return right(
return FlowyResult.failure(
FlowyError.create()
..code = ErrorCode.MissingAuthField
..msg = uri.path,
@ -197,7 +196,7 @@ class DeepLinkResult {
DeepLinkResult({required this.state, this.result});
final DeepLinkState state;
final Either<FlowyError, UserProfilePB>? result;
final FlowyResult<UserProfilePB, FlowyError>? result;
}
enum DeepLinkState {

View File

@ -580,8 +580,8 @@ GoRoute _rootRoute(Widget child) {
// Every time before navigating to splash screen, we check if user is already logged in in desktop. It is used to skip showing splash screen when user just changes apperance settings like theme mode.
final userResponse = await getIt<AuthService>().getUser();
final routeName = userResponse.fold(
(error) => null,
(user) => DesktopHomeScreen.routeName,
(error) => null,
);
if (routeName != null && !PlatformExtension.isMobile) return routeName;