-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlm_chat_callback.dart
More file actions
89 lines (75 loc) · 3.13 KB
/
Copy pathlm_chat_callback.dart
File metadata and controls
89 lines (75 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import 'package:flutter/foundation.dart';
import 'package:likeminds_chat_fl/likeminds_chat_fl.dart';
import 'package:likeminds_chat_flutter_core/likeminds_chat_flutter_core.dart';
/// Callback class for the core module
/// This class is used to handle the callback events from the core module
/// The core module will call the methods in this class when the corresponding events are triggered
class LMChatCoreCallback {
/// This method is called when the access token is expired and refreshed
Function(String accessToken, String refreshToken)?
onAccessTokenExpiredAndRefreshed;
/// This method is called when the refresh token is expired
Future<LMAuthToken> Function()? onRefreshTokenExpired;
/// Constructor for the LMChatCoreCallback
LMChatCoreCallback({
this.onAccessTokenExpiredAndRefreshed,
this.onRefreshTokenExpired,
});
}
/// Implementation of the LMChatSDKCallback
/// This class is used to handle the callback events from the core module
/// The core module will call the methods in this class when the corresponding events are triggered
class LMChatSDKCallbackImpl implements LMChatSDKCallback {
final LMChatCoreCallback? _lmChatCoreCallback;
/// Constructor for the LMChatSDKCallbackImpl
LMChatSDKCallbackImpl({LMChatCoreCallback? lmChatCallback})
: _lmChatCoreCallback = lmChatCallback;
@override
void eventFiredCallback(
String eventKey, Map<String, dynamic> propertiesMap) {}
@override
void loginRequiredCallback() {}
@override
void logoutCallback() {}
@override
void onAccessTokenExpiredAndRefreshed(
String accessToken, String refreshToken) {
debugPrint("onAccessTokenExpiredAndRefreshed: $accessToken, $refreshToken");
//Redirecting from core to example app
_lmChatCoreCallback?.onAccessTokenExpiredAndRefreshed
?.call(accessToken, refreshToken);
}
@override
Future<LMAuthToken> onRefreshTokenExpired() async {
String? apiKey = LMChatLocalPreference.instance
.fetchCache(LMChatStringConstants.apiKey)
?.value as String?;
if (apiKey != null) {
User user = LMChatLocalPreference.instance.getUser();
InitiateUserRequest initiateUserRequest = (InitiateUserRequestBuilder()
..apiKey(apiKey)
..userName(user.name)
..userId(user.sdkClientInfo!.uuid ?? ""))
.build();
LMResponse<InitiateUserResponse> initiateUserResponse = await LMChatCore
.instance
.initiateUser(initiateUserRequest: initiateUserRequest);
if (initiateUserResponse.success) {
return (LMAuthTokenBuilder()
..accessToken(initiateUserResponse.data!.accessToken!)
..refreshToken(initiateUserResponse.data!.refreshToken!))
.build();
} else {
throw Exception(initiateUserResponse.errorMessage);
}
} else {
final onRefreshTokenExpired = _lmChatCoreCallback?.onRefreshTokenExpired;
if (onRefreshTokenExpired == null) {
throw Exception("onRefreshTokenExpired callback is not implemented");
}
return onRefreshTokenExpired();
}
}
@override
void profileRouteCallback({required String lmUserId}) {}
}