notifications P1: лента с действиями + «Скрыть все»
- NotificationsRepository: парс actions[] (label/link/type/primary), dismissAll (DELETE /api/v2/notifications), executeAction (GET/POST/PUT/DELETE по action.link) - F7NotificationRow: FlowRow кнопок-действий (primary=заливка, прочие=контур) - NotificationsSheet: заголовок «Уведомления» + «Скрыть все», проброс действий, оптимистичное скрытие после действия/скрыть-все Контракты сверены с исходником notifications на forbion (deleteAllNotifications, actionToArray).
This commit is contained in:
@@ -1,8 +1,19 @@
|
||||
package ru.forbion.f7cloud.core.network
|
||||
|
||||
import okhttp3.MediaType.Companion.toMediaType
|
||||
import okhttp3.Request
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
|
||||
/** Кнопка-действие уведомления (NC notifications: actions[]={label,link,type,primary}). */
|
||||
data class F7NotificationAction(
|
||||
val label: String,
|
||||
val link: String,
|
||||
val type: String,
|
||||
val primary: Boolean,
|
||||
)
|
||||
|
||||
data class F7Notification(
|
||||
val id: Long,
|
||||
val subject: String,
|
||||
@@ -11,6 +22,7 @@ data class F7Notification(
|
||||
val link: String,
|
||||
val app: String,
|
||||
val icon: String = "",
|
||||
val actions: List<F7NotificationAction> = emptyList(),
|
||||
)
|
||||
|
||||
class NotificationsRepository {
|
||||
@@ -64,6 +76,25 @@ class NotificationsRepository {
|
||||
link = obj.optString("link"),
|
||||
app = obj.optString("app"),
|
||||
icon = obj.optString("icon"),
|
||||
actions = parseActions(obj.optJSONArray("actions")),
|
||||
)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
private fun parseActions(array: JSONArray?): List<F7NotificationAction> {
|
||||
if (array == null) return emptyList()
|
||||
val out = mutableListOf<F7NotificationAction>()
|
||||
for (i in 0 until array.length()) {
|
||||
val obj = array.optJSONObject(i) ?: continue
|
||||
val link = obj.optString("link")
|
||||
val label = obj.optString("label")
|
||||
if (link.isBlank() || label.isBlank()) continue
|
||||
out += F7NotificationAction(
|
||||
label = label,
|
||||
link = link,
|
||||
type = obj.optString("type").ifBlank { "GET" }.uppercase(),
|
||||
primary = obj.optBoolean("primary", false),
|
||||
)
|
||||
}
|
||||
return out
|
||||
@@ -90,4 +121,56 @@ class NotificationsRepository {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** «Скрыть все» — NC deleteAllNotifications (DELETE .../api/v2/notifications). */
|
||||
fun dismissAll(
|
||||
serverUrl: String,
|
||||
username: String,
|
||||
appPassword: String,
|
||||
trustAllCerts: Boolean = false,
|
||||
) {
|
||||
val client = NetworkFactory.newAuthedClient(username, appPassword, trustAllCerts)
|
||||
val url = "${serverUrl.trimEnd('/')}/ocs/v2.php/apps/notifications/api/v2/notifications"
|
||||
val request = Request.Builder()
|
||||
.url(url)
|
||||
.delete()
|
||||
.header("OCS-APIRequest", "true")
|
||||
.header("Accept", "application/json")
|
||||
.build()
|
||||
client.newCall(request).execute().use { response ->
|
||||
if (!response.isSuccessful) {
|
||||
error("Уведомления HTTP ${response.code}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Выполнить кнопку-действие уведомления. link — абсолютный URL от сервера,
|
||||
* type — GET/POST/PUT/DELETE. После действия сервер сам гасит уведомление.
|
||||
*/
|
||||
fun executeAction(
|
||||
serverUrl: String,
|
||||
username: String,
|
||||
appPassword: String,
|
||||
action: F7NotificationAction,
|
||||
trustAllCerts: Boolean = false,
|
||||
) {
|
||||
val client = NetworkFactory.newAuthedClient(username, appPassword, trustAllCerts)
|
||||
val empty = "".toRequestBody("application/json".toMediaType())
|
||||
val builder = Request.Builder()
|
||||
.url(action.link)
|
||||
.header("OCS-APIRequest", "true")
|
||||
.header("Accept", "application/json")
|
||||
when (action.type.uppercase()) {
|
||||
"POST" -> builder.post(empty)
|
||||
"PUT" -> builder.put(empty)
|
||||
"DELETE" -> builder.delete()
|
||||
else -> builder.get()
|
||||
}
|
||||
client.newCall(builder.build()).execute().use { response ->
|
||||
if (!response.isSuccessful) {
|
||||
error("Уведомления HTTP ${response.code}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user