fishpi-sdk 0.2.0

A Rust SDK for interacting with the FishPi community API
Documentation
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
package dev.fishpi.mobile

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn

import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.rounded.Article
import androidx.compose.material.icons.rounded.AlternateEmail
import androidx.compose.material.icons.rounded.Campaign
import androidx.compose.material.icons.rounded.ChatBubble
import androidx.compose.material.icons.rounded.Notifications
import androidx.compose.material.icons.rounded.Person
import androidx.compose.material.icons.rounded.Refresh
import androidx.compose.material.icons.rounded.Star
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import dev.fishpi.mobile.data.FishPiApiClient
import dev.fishpi.mobile.data.NoticeItemView
import dev.fishpi.mobile.data.NoticeUnreadCount
import dev.fishpi.mobile.utils.NoticeDestination
import dev.fishpi.mobile.utils.NoticePresentationCategory
import dev.fishpi.mobile.utils.noticeCategoryLabel
import dev.fishpi.mobile.utils.noticeDisplayTitle
import dev.fishpi.mobile.utils.noticePresentationCategory
import dev.fishpi.mobile.utils.noticePrimaryDestination
import dev.fishpi.mobile.utils.noticeSummaryText
import dev.fishpi.mobile.utils.noticeTimeLabel
import dev.fishpi.mobile.ui.components.ActionChipButton
import dev.fishpi.mobile.ui.components.ContentCardSurface
import dev.fishpi.mobile.ui.components.ControlSurface
import dev.fishpi.mobile.ui.components.FishPiBrandLoadingLogo
import dev.fishpi.mobile.ui.components.IconActionButton
import dev.fishpi.mobile.ui.components.LoadingScreen
import dev.fishpi.mobile.ui.components.PlainBackButton
import dev.fishpi.mobile.ui.components.UiLayerScaffold
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

private enum class NoticeCategory(
    val label: String,
    val icon: ImageVector,
    val presentation: NoticePresentationCategory,
) {
    All("全部", Icons.Rounded.Notifications, NoticePresentationCategory.All),
    Reply("回复", Icons.Rounded.ChatBubble, NoticePresentationCategory.Reply),
    AtMe("@我", Icons.Rounded.AlternateEmail, NoticePresentationCategory.AtMe),
    Points("积分", Icons.Rounded.Star, NoticePresentationCategory.Points),
    System("系统", Icons.Rounded.Campaign, NoticePresentationCategory.System),
    Follow("关注", Icons.Rounded.Person, NoticePresentationCategory.Follow),
}

private data class NoticeUiItem(
    val raw: NoticeItemView,
    val displayTitle: String,
    val summary: String,
    val categoryLabel: String,
    val timeText: String,
)

@Composable
internal fun NoticeScreen(
    session: AppSession,
    unread: Long,
    onUnreadChange: (Long) -> Unit,
    onDismiss: () -> Unit,
    onJumpToChatRoom: (String?) -> Unit,
    onJumpToArticle: (String) -> Unit,
) {
    val api = remember { FishPiApiClient.shared }
    val scope = rememberCoroutineScope()
    var notices by remember { mutableStateOf<List<NoticeItemView>>(emptyList()) }
    var count by remember { mutableStateOf<NoticeUnreadCount?>(null) }
    var isLoading by remember { mutableStateOf(true) }
    var error by remember { mutableStateOf<String?>(null) }
    var status by remember { mutableStateOf("通知中心") }
    var selectedCategory by remember { mutableStateOf(NoticeCategory.All) }

    fun refresh() {
        val hasLoaded = notices.isNotEmpty()
        if (!hasLoaded) {
            isLoading = true
        }
        error = null
        scope.launch {
            runCatching {
                withContext(Dispatchers.IO) {
                    val nextNotices = api.getNotices(session.apiKey)
                    withContext(Dispatchers.Main) {
                        notices = nextNotices
                        status = "已加载 ${nextNotices.size} 条通知,正在同步未读数..."
                    }
                    val nextCount = api.getNoticeUnreadCount(session.apiKey)
                    nextCount to nextNotices
                }
            }.onSuccess { (nextCount, nextNotices) ->
                count = nextCount
                onUnreadChange(nextCount.total)
                status = "已同步 ${nextNotices.size} 条通知"
            }.onFailure {
                error = it.message ?: "加载通知失败"
            }
            isLoading = false
        }
    }

    LaunchedEffect(session.apiKey) {
        refresh()
    }

    val filteredNotices = remember(notices, selectedCategory) {
        if (selectedCategory == NoticeCategory.All) notices
        else notices.filter { it.noticePresentationCategory() == selectedCategory.presentation }
    }
    val uiNotices = remember(filteredNotices) {
        filteredNotices.map { item ->
            NoticeUiItem(
                raw = item,
                displayTitle = item.noticeDisplayTitle(),
                summary = item.noticeSummaryText(),
                categoryLabel = item.noticeCategoryLabel(),
                timeText = item.noticeTimeLabel(),
            )
        }
    }

    val categoryUnreadCounts = remember(notices) {
        NoticeCategory.entries.associateWith { cat ->
            if (cat == NoticeCategory.All) notices.count { !it.read }.toLong()
            else notices.count { !it.read && it.noticePresentationCategory() == cat.presentation }.toLong()
        }
    }

    UiLayerScaffold {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .statusBarsPadding(),
        ) {
            ControlSurface(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 14.dp, vertical = 10.dp),
            ) {
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.spacedBy(8.dp),
                ) {
            PlainBackButton(onClick = onDismiss, tint = MaterialTheme.colorScheme.onSurfaceVariant)
            Column(modifier = Modifier.weight(1f)) {
                Text(
                    text = "通知",
                    color = MaterialTheme.colorScheme.onSurface,
                    fontWeight = FontWeight.Bold,
                    fontSize = 18.sp,
                )
                Text(
                    text = "未读 ${count?.total ?: unread} · $status",
                    color = MaterialTheme.colorScheme.onSurfaceVariant,
                    fontSize = 12.sp,
                    maxLines = 1,
                    overflow = TextOverflow.Ellipsis,
                )
            }
            IconActionButton(
                icon = Icons.Rounded.Refresh,
                contentDescription = "刷新通知",
                onClick = { refresh() },
            )
            ActionChipButton(
                text = "已读",
                selected = true,
                onClick = {
                        scope.launch {
                            runCatching {
                                withContext(Dispatchers.IO) { api.markAllNoticesRead(session.apiKey) }
                            }.onSuccess {
                                onUnreadChange(0)
                                count = count?.copy(total = 0)
                                notices = notices.map { it.copy(read = true) }
                                status = "已全部标记为已读"
                            }.onFailure {
                                error = it.message ?: "全部已读失败"
                            }
                        }
                },
            )
        }
        }

        NoticeCategoryTabs(
            selected = selectedCategory,
            unreadCounts = categoryUnreadCounts,
            onSelect = { selectedCategory = it },
        )

        when {
            isLoading && notices.isEmpty() -> LoadingScreen("加载通知...")
            error != null && notices.isEmpty() -> NoticeStateMessage(error ?: "加载通知失败", "请稍后刷新重试", isError = true)
            notices.isEmpty() -> NoticeStateMessage("暂无通知", "新的回复、积分和系统消息会显示在这里")
            filteredNotices.isEmpty() -> NoticeStateMessage("该分类暂无通知", "切换到全部可以查看其它通知")
            else -> {
                LazyColumn(
                    modifier = Modifier.fillMaxSize(),
                    contentPadding = PaddingValues(horizontal = 14.dp, vertical = 8.dp),
                    verticalArrangement = Arrangement.spacedBy(10.dp),
                ) {
                    error?.let { message ->
                        item(key = "notice-inline-error") {
                            NoticeInlineError(message)
                        }
                    }
                    itemsIndexed(
                        uiNotices,
                        key = { _, it -> it.raw.id.ifBlank { it.raw.time + it.raw.title } },
                    ) { _, item ->
                        NoticeCard(
                            item = item,
                            onJumpToChatRoom = onJumpToChatRoom,
                            onJumpToArticle = onJumpToArticle,
                        )
                    }
                }
            }
        }
    }
    }
}

@Composable
private fun NoticeCategoryTabs(
    selected: NoticeCategory,
    unreadCounts: Map<NoticeCategory, Long>,
    onSelect: (NoticeCategory) -> Unit,
) {
    ControlSurface(
        modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = 14.dp),
        shape = RoundedCornerShape(16.dp),
        contentPadding = PaddingValues(horizontal = 8.dp, vertical = 8.dp),
    ) {
        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.spacedBy(6.dp),
        ) {
            NoticeCategory.entries.forEach { category ->
                val isSelected = category == selected
                val unread = unreadCounts[category] ?: 0L

                Column(
                    modifier = Modifier
                        .weight(1f)
                        .clip(RoundedCornerShape(12.dp))
                        .background(
                            if (isSelected) MaterialTheme.colorScheme.primary
                            else MaterialTheme.colorScheme.surface,
                        )
                        .clickable { onSelect(category) }
                        .padding(horizontal = 6.dp, vertical = 8.dp),
                    horizontalAlignment = Alignment.CenterHorizontally,
                ) {
                    Box {
                        Icon(
                            imageVector = category.icon,
                            contentDescription = category.label,
                            tint = if (isSelected) MaterialTheme.colorScheme.onPrimary
                            else MaterialTheme.colorScheme.onSurfaceVariant,
                            modifier = Modifier.size(18.dp),
                        )
                        if (unread > 0 && !isSelected) {
                            Box(
                                modifier = Modifier
                                    .size(6.dp)
                                    .offset(x = 2.dp, y = (-1).dp)
                                    .clip(CircleShape)
                                    .background(MaterialTheme.colorScheme.error)
                                    .align(Alignment.TopEnd),
                            )
                        }
                    }
                    Spacer(Modifier.height(3.dp))
                    Text(
                        text = category.label,
                        color = if (isSelected) MaterialTheme.colorScheme.onPrimary
                        else MaterialTheme.colorScheme.onSurfaceVariant,
                        fontWeight = if (isSelected) FontWeight.Bold else FontWeight.Medium,
                        fontSize = 11.sp,
                    )
                }
            }
        }
    }
}

@Composable
private fun NoticeStateMessage(
    title: String,
    subtitle: String,
    isError: Boolean = false,
) {
    Box(
        modifier = Modifier.fillMaxSize(),
        contentAlignment = Alignment.Center,
    ) {
        ContentCardSurface(
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 28.dp),
            contentPadding = PaddingValues(horizontal = 18.dp, vertical = 22.dp),
        ) {
            Column(
                modifier = Modifier.fillMaxWidth(),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.spacedBy(10.dp),
            ) {
                FishPiBrandLoadingLogo()
                Text(
                    text = title,
                    color = if (isError) MaterialTheme.colorScheme.error else MaterialTheme.colorScheme.onSurface,
                    fontWeight = FontWeight.SemiBold,
                    fontSize = 15.sp,
                    maxLines = 2,
                    overflow = TextOverflow.Ellipsis,
                )
                Text(
                    text = subtitle,
                    color = MaterialTheme.colorScheme.onSurfaceVariant,
                    fontSize = 12.sp,
                    maxLines = 2,
                    overflow = TextOverflow.Ellipsis,
                )
            }
        }
    }
}

@Composable
private fun NoticeInlineError(message: String) {
    ContentCardSurface(
        modifier = Modifier.fillMaxWidth(),
        contentPadding = PaddingValues(horizontal = 14.dp, vertical = 10.dp),
    ) {
        Text(
            text = message,
            color = MaterialTheme.colorScheme.error,
            fontSize = 13.sp,
            fontWeight = FontWeight.SemiBold,
        )
    }
}

@Composable
private fun NoticeCard(
    item: NoticeUiItem,
    onJumpToChatRoom: (String?) -> Unit,
    onJumpToArticle: (String) -> Unit,
) {
    val raw = item.raw

    ContentCardSurface(
        modifier = Modifier.fillMaxWidth(),
        contentPadding = PaddingValues(horizontal = 14.dp, vertical = 12.dp),
    ) {
        Column(verticalArrangement = Arrangement.spacedBy(6.dp)) {
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(
                    text = item.displayTitle,
                    color = MaterialTheme.colorScheme.onSurface,
                    fontWeight = FontWeight.SemiBold,
                    fontSize = 14.sp,
                    maxLines = 1,
                    overflow = TextOverflow.Ellipsis,
                    modifier = Modifier.weight(1f),
                )
                if (!raw.read) {
                    Spacer(Modifier.width(6.dp))
                    Box(
                        modifier = Modifier
                            .size(6.dp)
                            .clip(CircleShape)
                            .background(MaterialTheme.colorScheme.error),
                    )
                }
            }
            if (item.summary.isNotBlank()) {
                Text(
                    text = item.summary,
                    color = MaterialTheme.colorScheme.onSurfaceVariant,
                    fontSize = 13.sp,
                    lineHeight = 18.sp,
                    maxLines = 2,
                    overflow = TextOverflow.Ellipsis,
                )
            }
            Row(
                verticalAlignment = Alignment.CenterVertically,
            ) {
                Text(
                    text = item.categoryLabel,
                    color = MaterialTheme.colorScheme.primary,
                    fontWeight = FontWeight.SemiBold,
                    fontSize = 12.sp,
                )
                if (!raw.author.isNullOrBlank()) {
                    Spacer(Modifier.width(8.dp))
                    Text(
                        text = raw.author,
                        color = MaterialTheme.colorScheme.onSurfaceVariant,
                        fontSize = 12.sp,
                        maxLines = 1,
                        overflow = TextOverflow.Ellipsis,
                    )
                }
                Spacer(Modifier.weight(1f))
                val destination = raw.noticePrimaryDestination()
                when (destination) {
                    NoticeDestination.ChatRoom -> NoticeActionChip(
                        icon = Icons.Rounded.ChatBubble,
                        text = "去聊天室",
                        onClick = { onJumpToChatRoom(raw.jumpId.ifBlank { null }) },
                    )
                    NoticeDestination.Article -> {
                        if (raw.jumpId.isNotBlank()) {
                            NoticeActionChip(
                                icon = Icons.AutoMirrored.Rounded.Article,
                                text = "帖子 #${raw.jumpId}",
                                onClick = { onJumpToArticle(raw.jumpId) },
                            )
                        }
                    }
                    NoticeDestination.None -> Unit
                }
                if (destination != NoticeDestination.None) {
                    Spacer(Modifier.width(8.dp))
                }
                Text(
                    text = item.timeText,
                    color = MaterialTheme.colorScheme.outline,
                    fontSize = 11.sp,
                )
            }
        }
    }
}

@Composable
private fun NoticeActionChip(
    icon: ImageVector,
    text: String,
    onClick: () -> Unit,
) {
    Row(
        modifier = Modifier.clickable(onClick = onClick),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.spacedBy(2.dp),
    ) {
        Icon(
            imageVector = icon,
            contentDescription = null,
            tint = MaterialTheme.colorScheme.primary.copy(alpha = 0.7f),
            modifier = Modifier.size(12.dp),
        )
        Text(
            text = text,
            color = MaterialTheme.colorScheme.primary.copy(alpha = 0.7f),
            fontSize = 11.sp,
            maxLines = 1,
            overflow = TextOverflow.Ellipsis,
        )
    }
}