2023-12-08 20:01:54 +07:00
|
|
|
import 'dart:async';
|
2023-11-25 01:18:31 -08:00
|
|
|
import 'dart:io';
|
|
|
|
|
2023-12-08 20:01:54 +07:00
|
|
|
import 'package:app_links/app_links.dart';
|
2023-11-25 01:18:31 -08:00
|
|
|
import 'package:appflowy/env/cloud_env.dart';
|
2023-10-12 20:25:00 +08:00
|
|
|
import 'package:appflowy/startup/startup.dart';
|
2023-11-29 12:55:13 -08:00
|
|
|
import 'package:appflowy/startup/tasks/app_widget.dart';
|
|
|
|
import 'package:appflowy/user/application/auth/auth_error.dart';
|
2023-12-08 20:01:54 +07:00
|
|
|
import 'package:appflowy/user/application/auth/auth_service.dart';
|
2023-11-29 12:55:13 -08:00
|
|
|
import 'package:appflowy/user/application/auth/device_id.dart';
|
2023-10-12 20:25:00 +08:00
|
|
|
import 'package:appflowy/user/application/user_auth_listener.dart';
|
2024-06-12 17:08:55 +02:00
|
|
|
import 'package:appflowy/workspace/application/subscription_success_listenable/subscription_success_listenable.dart';
|
2023-11-29 12:55:13 -08:00
|
|
|
import 'package:appflowy/workspace/presentation/home/toast.dart';
|
2024-09-17 13:19:15 +08:00
|
|
|
import 'package:appflowy/workspace/presentation/widgets/dialogs.dart';
|
2023-11-29 12:55:13 -08:00
|
|
|
import 'package:appflowy_backend/dispatch/dispatch.dart';
|
2023-12-08 20:01:54 +07:00
|
|
|
import 'package:appflowy_backend/log.dart';
|
2023-12-21 08:12:40 +08:00
|
|
|
import 'package:appflowy_backend/protobuf/flowy-error/code.pb.dart';
|
2023-11-29 12:55:13 -08:00
|
|
|
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
|
|
|
|
import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart';
|
2024-02-24 20:54:10 +07:00
|
|
|
import 'package:appflowy_result/appflowy_result.dart';
|
2024-09-17 13:19:15 +08:00
|
|
|
import 'package:flutter/material.dart';
|
2023-12-08 20:01:54 +07:00
|
|
|
import 'package:url_protocol/url_protocol.dart';
|
2023-11-29 12:55:13 -08:00
|
|
|
|
2024-08-18 05:16:42 +02:00
|
|
|
const appflowyDeepLinkSchema = 'appflowy-flutter';
|
|
|
|
|
2023-11-29 12:55:13 -08:00
|
|
|
class AppFlowyCloudDeepLink {
|
|
|
|
AppFlowyCloudDeepLink() {
|
2024-09-17 13:19:15 +08:00
|
|
|
_deepLinkSubscription = _AppLinkWrapper.instance.listen(
|
|
|
|
(Uri? uri) async {
|
|
|
|
Log.info('onDeepLink: ${uri.toString()}');
|
|
|
|
await _handleUri(uri);
|
|
|
|
},
|
|
|
|
onError: (Object err, StackTrace stackTrace) {
|
|
|
|
Log.error('on DeepLink stream error: ${err.toString()}', stackTrace);
|
|
|
|
_deepLinkSubscription.cancel();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
if (Platform.isWindows) {
|
|
|
|
// register deep link for Windows
|
|
|
|
registerProtocolHandler(appflowyDeepLinkSchema);
|
2023-11-29 12:55:13 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-25 16:37:36 +01:00
|
|
|
ValueNotifier<DeepLinkResult?>? _stateNotifier = ValueNotifier(null);
|
2024-09-17 13:19:15 +08:00
|
|
|
|
2024-02-24 20:54:10 +07:00
|
|
|
Completer<FlowyResult<UserProfilePB, FlowyError>>? _completer;
|
2024-01-25 16:37:36 +01:00
|
|
|
|
2024-09-17 13:19:15 +08:00
|
|
|
set completer(Completer<FlowyResult<UserProfilePB, FlowyError>>? value) {
|
|
|
|
Log.debug('AppFlowyCloudDeepLink: $hashCode completer');
|
|
|
|
_completer = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
late final StreamSubscription<Uri?> _deepLinkSubscription;
|
2024-01-25 16:37:36 +01:00
|
|
|
|
2023-11-29 12:55:13 -08:00
|
|
|
Future<void> dispose() async {
|
2024-09-17 13:19:15 +08:00
|
|
|
Log.debug('AppFlowyCloudDeepLink: $hashCode dispose');
|
|
|
|
await _deepLinkSubscription.cancel();
|
|
|
|
|
2023-12-21 08:12:40 +08:00
|
|
|
_stateNotifier?.dispose();
|
|
|
|
_stateNotifier = null;
|
2024-09-17 13:19:15 +08:00
|
|
|
completer = null;
|
2023-11-29 12:55:13 -08:00
|
|
|
}
|
|
|
|
|
2024-02-24 20:54:10 +07:00
|
|
|
void registerCompleter(
|
|
|
|
Completer<FlowyResult<UserProfilePB, FlowyError>> completer,
|
2023-11-29 12:55:13 -08:00
|
|
|
) {
|
2024-09-17 13:19:15 +08:00
|
|
|
this.completer = completer;
|
2023-11-29 12:55:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
VoidCallback subscribeDeepLinkLoadingState(
|
|
|
|
ValueChanged<DeepLinkResult> listener,
|
|
|
|
) {
|
2023-12-08 20:01:54 +07:00
|
|
|
void listenerFn() {
|
2023-12-21 08:12:40 +08:00
|
|
|
if (_stateNotifier?.value != null) {
|
|
|
|
listener(_stateNotifier!.value!);
|
2023-11-29 12:55:13 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-21 08:12:40 +08:00
|
|
|
_stateNotifier?.addListener(listenerFn);
|
2023-11-29 12:55:13 -08:00
|
|
|
return listenerFn;
|
|
|
|
}
|
|
|
|
|
2023-12-21 08:12:40 +08:00
|
|
|
void unsubscribeDeepLinkLoadingState(VoidCallback listener) =>
|
|
|
|
_stateNotifier?.removeListener(listener);
|
2023-11-29 12:55:13 -08:00
|
|
|
|
|
|
|
Future<void> _handleUri(
|
|
|
|
Uri? uri,
|
|
|
|
) async {
|
2023-12-21 08:12:40 +08:00
|
|
|
_stateNotifier?.value = DeepLinkResult(state: DeepLinkState.none);
|
|
|
|
|
2024-01-29 10:26:45 +08:00
|
|
|
if (uri == null) {
|
2024-02-24 20:54:10 +07:00
|
|
|
Log.error('onDeepLinkError: Unexpected empty deep link callback');
|
|
|
|
_completer?.complete(FlowyResult.failure(AuthError.emptyDeepLink));
|
2024-09-17 13:19:15 +08:00
|
|
|
completer = null;
|
2024-03-14 09:17:59 +08:00
|
|
|
return;
|
2023-11-29 12:55:13 -08:00
|
|
|
}
|
2024-03-14 09:17:59 +08:00
|
|
|
|
2024-06-12 17:08:55 +02:00
|
|
|
if (_isPaymentSuccessUri(uri)) {
|
2024-07-22 09:43:48 +02:00
|
|
|
Log.debug("Payment success deep link: ${uri.toString()}");
|
|
|
|
final plan = uri.queryParameters['plan'];
|
|
|
|
return getIt<SubscriptionSuccessListenable>().onPaymentSuccess(plan);
|
2024-06-12 17:08:55 +02:00
|
|
|
}
|
|
|
|
|
2024-03-14 09:17:59 +08:00
|
|
|
return _isAuthCallbackDeepLink(uri).fold(
|
2024-01-29 10:26:45 +08:00
|
|
|
(_) async {
|
|
|
|
final deviceId = await getDeviceId();
|
|
|
|
final payload = OauthSignInPB(
|
|
|
|
authenticator: AuthenticatorPB.AppFlowyCloud,
|
|
|
|
map: {
|
|
|
|
AuthServiceMapKeys.signInURL: uri.toString(),
|
|
|
|
AuthServiceMapKeys.deviceId: deviceId,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
_stateNotifier?.value = DeepLinkResult(state: DeepLinkState.loading);
|
2024-03-14 09:17:59 +08:00
|
|
|
final result = await UserEventOauthSignIn(payload).send();
|
2024-01-29 10:26:45 +08:00
|
|
|
|
|
|
|
_stateNotifier?.value = DeepLinkResult(
|
|
|
|
state: DeepLinkState.finish,
|
|
|
|
result: result,
|
|
|
|
);
|
|
|
|
// If there is no completer, runAppFlowy() will be called.
|
|
|
|
if (_completer == null) {
|
|
|
|
await result.fold(
|
2024-02-24 20:54:10 +07:00
|
|
|
(_) async {
|
|
|
|
await runAppFlowy();
|
|
|
|
},
|
2024-01-29 10:26:45 +08:00
|
|
|
(err) {
|
|
|
|
Log.error(err);
|
|
|
|
final context = AppGlobals.rootNavKey.currentState?.context;
|
|
|
|
if (context != null) {
|
2024-09-17 13:19:15 +08:00
|
|
|
showToastNotification(
|
2025-04-09 13:45:07 +08:00
|
|
|
|
2024-09-17 13:19:15 +08:00
|
|
|
message: err.msg,
|
2024-01-29 10:26:45 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
_completer?.complete(result);
|
2024-09-17 13:19:15 +08:00
|
|
|
completer = null;
|
2024-01-29 10:26:45 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
(err) {
|
2024-02-24 20:54:10 +07:00
|
|
|
Log.error('onDeepLinkError: Unexpected deep link: $err');
|
2024-01-29 10:26:45 +08:00
|
|
|
if (_completer == null) {
|
|
|
|
final context = AppGlobals.rootNavKey.currentState?.context;
|
|
|
|
if (context != null) {
|
|
|
|
showSnackBarMessage(
|
|
|
|
context,
|
|
|
|
err.msg,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
2024-02-24 20:54:10 +07:00
|
|
|
_completer?.complete(FlowyResult.failure(err));
|
2024-09-17 13:19:15 +08:00
|
|
|
completer = null;
|
2024-01-29 10:26:45 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2023-11-29 12:55:13 -08:00
|
|
|
}
|
|
|
|
|
2024-02-24 20:54:10 +07:00
|
|
|
FlowyResult<void, FlowyError> _isAuthCallbackDeepLink(Uri uri) {
|
2023-12-21 08:12:40 +08:00
|
|
|
if (uri.fragment.contains('access_token')) {
|
2024-02-24 20:54:10 +07:00
|
|
|
return FlowyResult.success(null);
|
2023-12-21 08:12:40 +08:00
|
|
|
}
|
|
|
|
|
2024-02-24 20:54:10 +07:00
|
|
|
return FlowyResult.failure(
|
2023-12-21 08:12:40 +08:00
|
|
|
FlowyError.create()
|
|
|
|
..code = ErrorCode.MissingAuthField
|
|
|
|
..msg = uri.path,
|
|
|
|
);
|
2023-11-29 12:55:13 -08:00
|
|
|
}
|
2024-06-12 17:08:55 +02:00
|
|
|
|
|
|
|
bool _isPaymentSuccessUri(Uri uri) {
|
|
|
|
return uri.host == 'payment-success';
|
|
|
|
}
|
2023-11-29 12:55:13 -08:00
|
|
|
}
|
2023-10-12 20:25:00 +08:00
|
|
|
|
|
|
|
class InitAppFlowyCloudTask extends LaunchTask {
|
2023-10-24 23:13:51 +08:00
|
|
|
UserAuthStateListener? _authStateListener;
|
2023-10-12 20:25:00 +08:00
|
|
|
bool isLoggingOut = false;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> initialize(LaunchContext context) async {
|
|
|
|
if (!isAppFlowyCloudEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
2023-10-24 23:13:51 +08:00
|
|
|
_authStateListener = UserAuthStateListener();
|
2023-10-12 20:25:00 +08:00
|
|
|
|
2023-10-24 23:13:51 +08:00
|
|
|
_authStateListener?.start(
|
2023-10-12 20:25:00 +08:00
|
|
|
didSignIn: () {
|
|
|
|
isLoggingOut = false;
|
|
|
|
},
|
|
|
|
onInvalidAuth: (message) async {
|
2023-10-24 20:11:06 +08:00
|
|
|
Log.error(message);
|
2023-10-12 20:25:00 +08:00
|
|
|
if (!isLoggingOut) {
|
|
|
|
await runAppFlowy();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2023-10-24 23:13:51 +08:00
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> dispose() async {
|
|
|
|
await _authStateListener?.stop();
|
|
|
|
_authStateListener = null;
|
|
|
|
}
|
2023-10-12 20:25:00 +08:00
|
|
|
}
|
2023-11-29 12:55:13 -08:00
|
|
|
|
|
|
|
class DeepLinkResult {
|
2024-04-11 16:33:28 +08:00
|
|
|
DeepLinkResult({
|
|
|
|
required this.state,
|
|
|
|
this.result,
|
|
|
|
});
|
2024-01-25 16:37:36 +01:00
|
|
|
|
2023-11-29 12:55:13 -08:00
|
|
|
final DeepLinkState state;
|
2024-02-24 20:54:10 +07:00
|
|
|
final FlowyResult<UserProfilePB, FlowyError>? result;
|
2023-11-29 12:55:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
enum DeepLinkState {
|
|
|
|
none,
|
|
|
|
loading,
|
|
|
|
finish,
|
|
|
|
}
|
2024-09-17 13:19:15 +08:00
|
|
|
|
|
|
|
// wrapper for AppLinks to support multiple listeners
|
|
|
|
class _AppLinkWrapper {
|
|
|
|
_AppLinkWrapper._() {
|
|
|
|
_appLinkSubscription = _appLinks.uriLinkStream.listen((event) {
|
|
|
|
_streamSubscription.sink.add(event);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static final _AppLinkWrapper instance = _AppLinkWrapper._();
|
|
|
|
|
|
|
|
final AppLinks _appLinks = AppLinks();
|
|
|
|
final _streamSubscription = StreamController<Uri?>.broadcast();
|
|
|
|
late final StreamSubscription<Uri?> _appLinkSubscription;
|
|
|
|
|
|
|
|
StreamSubscription<Uri?> listen(
|
|
|
|
void Function(Uri?) listener, {
|
|
|
|
Function? onError,
|
|
|
|
bool? cancelOnError,
|
|
|
|
}) {
|
|
|
|
return _streamSubscription.stream.listen(
|
|
|
|
listener,
|
|
|
|
onError: onError,
|
|
|
|
cancelOnError: cancelOnError,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dispose() {
|
|
|
|
_streamSubscription.close();
|
|
|
|
_appLinkSubscription.cancel();
|
|
|
|
}
|
|
|
|
}
|