1
0
Fork 0
mirror of https://github.com/anyproto/anytype-kotlin.git synced 2025-06-08 05:47:05 +09:00

Render bookmark block (#291)

This commit is contained in:
Evgenii Kozlov 2020-03-16 09:06:53 +01:00 committed by GitHub
parent ca4961e229
commit 40207cdfa6
Signed by: github
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 139 additions and 8 deletions

View file

@ -317,16 +317,16 @@ sealed class BlockView : ViewType {
* @property title website's title
* @property title website's content description
* @property url website's url
* @property logoUrl website's logo url
* @property logoUrl content's main image url
* @property faviconUrl website's favicon url
* @property imageUrl content's main image url
*/
data class Bookmark(
override val id: String,
val title: String,
val description: String,
val url: String,
val logoUrl: String,
val imageUrl: String
val title: String?,
val description: String?,
val faviconUrl: String?,
val imageUrl: String?
) : BlockView() {
override fun getViewType() = HOLDER_BOOKMARK
}

View file

@ -659,12 +659,49 @@ sealed class BlockViewHolder(view: View) : RecyclerView.ViewHolder(view) {
private val url = itemView.bookmarkUrl
private val image = itemView.bookmarkImage
private val logo = itemView.bookmarkLogo
private val error = itemView.loadBookmarkPictureError
private val listener: RequestListener<Drawable> = object : RequestListener<Drawable> {
override fun onLoadFailed(
e: GlideException?,
model: Any?,
target: Target<Drawable>?,
isFirstResource: Boolean
): Boolean {
error.visible()
return false
}
override fun onResourceReady(
resource: Drawable?,
model: Any?,
target: Target<Drawable>?,
dataSource: DataSource?,
isFirstResource: Boolean
): Boolean {
error.invisible()
return false
}
}
fun bind(item: BlockView.Bookmark) {
title.text = item.title
description.text = item.description
url.text = item.url
// TODO set logo icon and website's image
item.imageUrl?.let { url ->
Glide.with(image)
.load(url)
.listener(listener)
.into(image)
}
item.faviconUrl?.let { url ->
Glide.with(logo)
.load(url)
.listener(listener)
.into(logo)
}
}
}

View file

@ -64,5 +64,17 @@
app:layout_constraintTop_toTopOf="@id/bookmarkUrl"
tools:background="@color/orange" />
<TextView
android:id="@+id/loadBookmarkPictureError"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:text="@string/error_while_loading_picture"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="@+id/bookmarkImage"
app:layout_constraintEnd_toEndOf="@+id/bookmarkImage"
app:layout_constraintStart_toStartOf="@+id/bookmarkImage"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

View file

@ -83,6 +83,7 @@ fun BlockEntity.Content.toDomain(): Block.Content = when (this) {
is BlockEntity.Content.Divider -> toDomain()
is BlockEntity.Content.File -> toDomain()
is BlockEntity.Content.Icon -> toDomain()
is BlockEntity.Content.Bookmark -> toDomain()
}
fun BlockEntity.Content.File.toDomain(): Block.Content.File {
@ -97,6 +98,16 @@ fun BlockEntity.Content.File.toDomain(): Block.Content.File {
)
}
fun BlockEntity.Content.Bookmark.toDomain(): Block.Content.Bookmark {
return Block.Content.Bookmark(
url = url,
title = title,
description = description,
image = image,
favicon = favicon
)
}
fun BlockEntity.Content.Icon.toDomain(): Block.Content.Icon = Block.Content.Icon(
name = name
)
@ -203,6 +214,7 @@ fun Block.Content.toEntity(): BlockEntity.Content = when (this) {
is Block.Content.Divider -> toEntity()
is Block.Content.File -> toEntity()
is Block.Content.Icon -> toEntity()
is Block.Content.Bookmark -> toEntity()
}
fun Block.Content.File.toEntity(): BlockEntity.Content.File {
@ -217,6 +229,16 @@ fun Block.Content.File.toEntity(): BlockEntity.Content.File {
)
}
fun Block.Content.Bookmark.toEntity(): BlockEntity.Content.Bookmark {
return BlockEntity.Content.Bookmark(
url = url,
title = title,
description = description,
image = image,
favicon = favicon
)
}
fun Block.Content.Icon.toEntity(): BlockEntity.Content.Icon = BlockEntity.Content.Icon(
name = name
)

View file

@ -85,6 +85,14 @@ data class BlockEntity(
enum class State { EMPTY, UPLOADING, DONE, ERROR }
}
data class Bookmark(
val url: String,
val title: String?,
val description: String?,
val image: String?,
val favicon: String?
) : Content()
object Divider : Content()
}

View file

@ -2,7 +2,9 @@ package com.agileburo.anytype.domain.block.model
import com.agileburo.anytype.domain.block.model.Block.Content.Text.Mark
import com.agileburo.anytype.domain.block.model.Block.Content.Text.Style
import com.agileburo.anytype.domain.common.Hash
import com.agileburo.anytype.domain.common.Id
import com.agileburo.anytype.domain.common.Url
/**
* Represents block as basic data structure.
@ -169,6 +171,21 @@ data class Block(
enum class State { EMPTY, UPLOADING, DONE, ERROR }
}
/**
* @property url url associated with this bookmark
* @property title optional bookmark title
* @property description optional bookmark's content description
* @property image optional hash of bookmark's image
* @property favicon optional hash of bookmark's favicon
*/
data class Bookmark(
val url: Url,
val title: String?,
val description: String?,
val image: Hash?,
val favicon: Hash?
) : Content()
object Divider : Content()
}

View file

@ -1,4 +1,5 @@
package com.agileburo.anytype.domain.common
typealias Id = String
typealias Url = String
typealias Url = String
typealias Hash = String

View file

@ -28,6 +28,7 @@ fun Map<String, List<Block>>.asRender(anchor: String): List<Block> {
is Content.Image,
is Content.Link,
is Content.Divider,
is Content.Bookmark,
is Content.File -> {
result.add(child)
result.addAll(asRender(child.id))

View file

@ -28,6 +28,15 @@ android {
includeAndroidResources = true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies {

View file

@ -242,6 +242,14 @@ fun Block.file(): BlockEntity.Content.File = BlockEntity.Content.File(
}
)
fun Block.bookmark(): BlockEntity.Content.Bookmark = BlockEntity.Content.Bookmark(
url = bookmark.url,
description = bookmark.description.ifEmpty { null },
title = bookmark.title.ifEmpty { null },
image = bookmark.imageHash.ifEmpty { null },
favicon = bookmark.faviconHash.ifEmpty { null }
)
fun List<Block>.blocks(): List<BlockEntity> = mapNotNull { block ->
when (block.contentCase) {
Block.ContentCase.DASHBOARD -> {
@ -308,6 +316,14 @@ fun List<Block>.blocks(): List<BlockEntity> = mapNotNull { block ->
content = block.icon()
)
}
Block.ContentCase.BOOKMARK -> {
BlockEntity(
id = block.id,
children = block.childrenIdsList,
fields = block.fields(),
content = block.bookmark()
)
}
else -> {
null
}

View file

@ -301,6 +301,14 @@ class PageViewModel(
is Content.Divider -> block.toView(
urlBuilder = urlBuilder
)
is Content.Bookmark -> BlockView.Bookmark(
id = block.id,
url = content.url,
title = content.title,
description = content.description,
imageUrl = content.image?.let { urlBuilder.image(it) },
faviconUrl = content.favicon?.let { urlBuilder.image(it) }
)
else -> null
}
}