Skip to content

Commit

Permalink
当没有设置数据源图标时, 动态生成图标 (ui-avatars)
Browse files Browse the repository at this point in the history
  • Loading branch information
Him188 committed Sep 25, 2024
1 parent 45f21d7 commit fb2227c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*
* Copyright (C) 2024 OpenAni and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license, which can be found at the following link.
*
* https://github.com/open-ani/ani/blob/main/LICENSE
*/

@file:Suppress("MemberVisibilityCanBePrivate")

package me.him188.ani.app.ui.settings.rendering
Expand All @@ -7,6 +16,9 @@ import androidx.compose.material.icons.rounded.DownloadDone
import androidx.compose.material.icons.rounded.Public
import androidx.compose.material.icons.rounded.Radar
import androidx.compose.runtime.Stable
import io.ktor.http.URLBuilder
import io.ktor.http.takeFrom
import me.him188.ani.datasources.api.source.MediaSourceInfo
import me.him188.ani.datasources.api.source.MediaSourceKind
import me.him188.ani.datasources.api.source.MediaSourceLocation

Expand Down Expand Up @@ -34,4 +46,12 @@ object MediaSourceIcons {
MediaSourceLocation.Lan -> LocationLan
MediaSourceLocation.Online -> kind(kind)
}

@Stable
fun getDefaultIconUrl(info: MediaSourceInfo): String {
return URLBuilder().apply {
takeFrom("https://ui-avatars.com/api")
parameters.append("name", info.displayName)
}.buildString()
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*
* Copyright (C) 2024 OpenAni and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license, which can be found at the following link.
*
* https://github.com/open-ani/ani/blob/main/LICENSE
*/

package me.him188.ani.app.ui.settings.rendering

import androidx.compose.foundation.Image
Expand Down Expand Up @@ -64,9 +73,9 @@ fun MediaSourceIcon(
)
}

sourceInfo?.iconUrl != null -> {
sourceInfo != null -> {
AsyncImage(
sourceInfo.iconUrl,
sourceInfo.iconUrl?.takeIf { it.isNotEmpty() } ?: MediaSourceIcons.getDefaultIconUrl(sourceInfo),
null,
modifier,
contentScale = ContentScale.Crop,
Expand Down Expand Up @@ -108,19 +117,9 @@ fun SmallMediaSourceIcon(
)
}

info.iconUrl != null -> {
AsyncImage(
info.iconUrl,
null, Modifier.size(24.dp),
contentScale = ContentScale.Crop,
alignment = Alignment.Center,
colorFilter = null,
)
}

else -> {
Image(
rememberVectorPainter(Icons.Rounded.DisplaySettings),
AsyncImage(
info.iconUrl?.takeIf { it.isNotEmpty() } ?: MediaSourceIcons.getDefaultIconUrl(info),
null, Modifier.size(24.dp),
contentScale = ContentScale.Crop,
alignment = Alignment.Center,
Expand Down

0 comments on commit fb2227c

Please sign in to comment.