1+ package com.courier.courier_flutter
2+
3+ import com.courier.android.Courier
4+ import com.courier.android.models.CourierDevice
5+ import com.courier.android.models.CourierPreferenceChannel
6+ import com.courier.android.models.CourierPreferenceStatus
7+ import com.courier.android.models.CourierTrackingEvent
8+ import com.courier.android.modules.inboxPaginationLimit
9+ import com.courier.courier_flutter.CourierPlugin.Companion.TAG
10+ import io.flutter.embedding.engine.plugins.FlutterPlugin
11+ import io.flutter.plugin.common.MethodCall
12+ import io.flutter.plugin.common.MethodChannel
13+
14+ internal class ClientMethodHandler (channel : CourierFlutterChannel , private val binding : FlutterPlugin .FlutterPluginBinding ) : CourierMethodHandler(channel, binding) {
15+
16+ override suspend fun handleMethod (call : MethodCall , result : MethodChannel .Result ) {
17+
18+ try {
19+
20+ val params = call.arguments as ? HashMap <* , * >
21+
22+ val client = params?.toClient() ? : throw MissingParameter (" client" )
23+
24+ when (call.method) {
25+
26+ // == Brand ==
27+
28+ " brands.get_brand" -> {
29+
30+ val (brandId) = listOf<String >(
31+ params.extract(" brandId" ),
32+ )
33+
34+ val brand = client.brands.getBrand(brandId)
35+ val json = brand.toJson()
36+ result.success(json)
37+
38+ }
39+
40+ // == Token Management ==
41+
42+ " tokens.put_user_token" -> {
43+
44+ val (token, provider) = listOf<String >(
45+ params.extract(" token" ),
46+ params.extract(" provider" ),
47+ )
48+
49+ val deviceParams = params[" device" ] as ? Map <* , * >
50+ val device = deviceParams?.toCourierDevice()
51+
52+ client.tokens.putUserToken(
53+ token = token,
54+ provider = provider,
55+ device = device ? : CourierDevice .current(binding.applicationContext),
56+ )
57+
58+ result.success(null )
59+
60+ }
61+
62+ " tokens.delete_user_token" -> {
63+
64+ val (token) = listOf<String >(
65+ params.extract(" token" ),
66+ )
67+
68+ client.tokens.deleteUserToken(
69+ token = token,
70+ )
71+
72+ result.success(null )
73+
74+ }
75+
76+ // == Inbox ==
77+
78+ " inbox.get_messages" -> {
79+
80+ val paginationLimit = params[" paginationLimit" ] as ? Int
81+ val startCursor = params[" startCursor" ] as ? String
82+
83+ val res = client.inbox.getMessages(
84+ paginationLimit = paginationLimit ? : Courier .shared.inboxPaginationLimit,
85+ startCursor = startCursor
86+ )
87+
88+ val json = res.toJson()
89+ result.success(json)
90+
91+ }
92+
93+ " inbox.get_archived_messages" -> {
94+
95+ val paginationLimit = params[" paginationLimit" ] as ? Int
96+ val startCursor = params[" startCursor" ] as ? String
97+
98+ val res = client.inbox.getArchivedMessages(
99+ paginationLimit = paginationLimit ? : Courier .shared.inboxPaginationLimit,
100+ startCursor = startCursor
101+ )
102+
103+ val json = res.toJson()
104+ result.success(json)
105+
106+ }
107+
108+ " inbox.get_unread_message_count" -> {
109+
110+ val count = client.inbox.getUnreadMessageCount()
111+ result.success(count)
112+
113+ }
114+
115+ " inbox.get_message_by_id" -> {
116+
117+ val (messageId) = listOf<String >(
118+ params.extract(" messageId" ),
119+ )
120+
121+ val res = client.inbox.getMessage(
122+ messageId = messageId
123+ )
124+
125+ val json = res.toJson()
126+ result.success(json)
127+
128+ }
129+
130+ " inbox.click_message" -> {
131+
132+ val (messageId, trackingId) = listOf<String >(
133+ params.extract(" messageId" ),
134+ params.extract(" trackingId" ),
135+ )
136+
137+ client.inbox.click(
138+ messageId = messageId,
139+ trackingId = trackingId
140+ )
141+
142+ result.success(null )
143+
144+ }
145+
146+ " inbox.unread_message" -> {
147+
148+ val (messageId) = listOf<String >(
149+ params.extract(" messageId" ),
150+ )
151+
152+ client.inbox.unread(
153+ messageId = messageId,
154+ )
155+
156+ result.success(null )
157+
158+ }
159+
160+ " inbox.read_message" -> {
161+
162+ val (messageId) = listOf<String >(
163+ params.extract(" messageId" ),
164+ )
165+
166+ client.inbox.read(
167+ messageId = messageId,
168+ )
169+
170+ result.success(null )
171+
172+ }
173+
174+ " inbox.open_message" -> {
175+
176+ val (messageId) = listOf<String >(
177+ params.extract(" messageId" ),
178+ )
179+
180+ client.inbox.open(
181+ messageId = messageId,
182+ )
183+
184+ result.success(null )
185+
186+ }
187+
188+ " inbox.archive_message" -> {
189+
190+ val (messageId) = listOf<String >(
191+ params.extract(" messageId" ),
192+ )
193+
194+ client.inbox.archive(
195+ messageId = messageId,
196+ )
197+
198+ result.success(null )
199+
200+ }
201+
202+ " inbox.read_all_messages" -> {
203+
204+ client.inbox.readAll()
205+
206+ result.success(null )
207+
208+ }
209+
210+ // == Preferences ==
211+
212+ " preferences.get_user_preferences" -> {
213+
214+ val paginationCursor = params[" paginationCursor" ] as ? String
215+
216+ val res = client.preferences.getUserPreferences(
217+ paginationCursor = paginationCursor
218+ )
219+
220+ val json = res.toJson()
221+ result.success(json)
222+
223+ }
224+
225+ " preferences.get_user_preference_topic" -> {
226+
227+ val (topicId) = listOf<String >(
228+ params.extract(" topicId" ),
229+ )
230+
231+ val res = client.preferences.getUserPreferenceTopic(
232+ topicId = topicId
233+ )
234+
235+ val json = res.toJson()
236+ result.success(json)
237+
238+ }
239+
240+ " preferences.put_user_preference_topic" -> {
241+
242+ val topicId = params.extract<String >(" topicId" )
243+ val status = params.extract<String >(" status" )
244+ val hasCustomRouting = params.extract<Boolean >(" hasCustomRouting" )
245+ val customRouting = params.extract<List <String >>(" customRouting" )
246+
247+ client.preferences.putUserPreferenceTopic(
248+ topicId = topicId,
249+ status = CourierPreferenceStatus .valueOf(status),
250+ hasCustomRouting = hasCustomRouting,
251+ customRouting = customRouting.map { CourierPreferenceChannel .fromString(it) },
252+ )
253+
254+ result.success(null )
255+
256+ }
257+
258+ // == Tracking ==
259+
260+ " tracking.post_tracking_url" -> {
261+
262+ val (url, event) = listOf<String >(
263+ params.extract(" url" ),
264+ params.extract(" event" ),
265+ )
266+
267+ client.tracking.postTrackingUrl(
268+ url = url,
269+ event = CourierTrackingEvent .valueOf(event)
270+ )
271+
272+ result.success(null )
273+
274+ }
275+
276+ else -> {
277+ result.notImplemented()
278+ }
279+
280+ }
281+
282+ } catch (e: Exception ) {
283+
284+ result.error(TAG , e.message, e)
285+
286+ }
287+
288+ }
289+
290+ }
0 commit comments