Skip to content
This repository has been archived by the owner on Apr 26, 2020. It is now read-only.

Commit

Permalink
Pulled dependency to Blob out of Thumbnail
Browse files Browse the repository at this point in the history
and added workaround for invalid bitmap data
  • Loading branch information
DreierF committed Jan 12, 2018
1 parent 76fdae3 commit 43e802b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class StatisticsActivity : ChildActivityBase(), LoaderManager.LoaderCallbacks<Li
.map { bid ->
if (bid != null) {
val bow = Bow[bid] ?: return@map Tag(bid, "Deleted " + bid)
Tag(bow.id, bow.name, bow.thumbnail!!.blob.blob, true)
Tag(bow.id, bow.name, bow.thumbnail, true)
} else {
Tag(null, getString(R.string.unknown))
}
Expand All @@ -221,7 +221,7 @@ class StatisticsActivity : ChildActivityBase(), LoaderManager.LoaderCallbacks<Li
.map { aid ->
if (aid != null) {
val arrow = Arrow[aid] ?: return@map Tag(aid, "Deleted " + aid)
Tag(arrow.id, arrow.name, arrow.thumbnail!!.blob.blob, true)
Tag(arrow.id, arrow.name, arrow.thumbnail, true)
} else {
Tag(null, getString(R.string.unknown))
}
Expand Down
23 changes: 5 additions & 18 deletions app/src/main/java/de/dreier/mytargets/features/statistics/Tag.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@ package de.dreier.mytargets.features.statistics
import android.annotation.SuppressLint
import android.content.Context
import android.databinding.DataBindingUtil
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.drawable.Drawable
import android.os.Parcelable
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.RelativeLayout

import de.dreier.mytargets.R
import de.dreier.mytargets.databinding.ViewChipsBinding
import de.dreier.mytargets.shared.utils.RoundedAvatarDrawable
import de.dreier.mytargets.shared.models.Thumbnail
import kotlinx.android.parcel.IgnoredOnParcel
import kotlinx.android.parcel.Parcelize

Expand All @@ -37,22 +34,12 @@ import kotlinx.android.parcel.Parcelize
class Tag @JvmOverloads constructor(
var id: Long?,
var text: String,
var image: ByteArray? = null,
var isChecked: Boolean = true) : Parcelable {
var image: Thumbnail? = null,
var isChecked: Boolean = true
) : Parcelable {

@IgnoredOnParcel
private var thumbnail: Bitmap? = null

val drawable: Drawable?
get() {
if (image == null) {
return null
}
if (thumbnail == null) {
thumbnail = BitmapFactory.decodeByteArray(image, 0, image!!.size)
}
return RoundedAvatarDrawable(thumbnail!!)
}
val drawable: Drawable? by lazy { image?.roundDrawable }

fun getView(context: Context, parent: ViewGroup): ViewChipsBinding {
val binding = DataBindingUtil
Expand Down
24 changes: 11 additions & 13 deletions shared/src/main/java/de/dreier/mytargets/shared/models/Thumbnail.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,28 @@ import android.graphics.drawable.Drawable
import android.media.ThumbnailUtils
import android.os.Parcelable
import android.support.annotation.DrawableRes
import com.raizlabs.android.dbflow.data.Blob
import de.dreier.mytargets.shared.utils.RoundedAvatarDrawable
import de.dreier.mytargets.shared.utils.toByteArray
import kotlinx.android.parcel.Parcelize
import timber.log.Timber
import java.io.File
import java.util.*

@SuppressLint("ParcelCreator")
@Parcelize
class Thumbnail(internal var data: ByteArray) : Parcelable {
class Thumbnail(val data: ByteArray) : Parcelable {

@Suppress("PLUGIN_WARNING")
val roundDrawable: Drawable by lazy {
val bitmap = BitmapFactory.decodeByteArray(data, 0, data.size)
if (bitmap == null) {
Timber.w("Invalid bitmap data provided: %s", Arrays.asList(data).toString())
val dummyBitmap = Bitmap.createBitmap(20, 20, Bitmap.Config.ARGB_8888)
return@lazy RoundedAvatarDrawable(dummyBitmap)
}
RoundedAvatarDrawable(bitmap)
}

val blob: Blob
get() = Blob(data)


constructor() : this(ByteArray(0))

constructor(data: Blob) : this(data.blob)

companion object {
/**
* Constant used to indicate the dimension of micro thumbnail.
Expand All @@ -64,12 +62,12 @@ class Thumbnail(internal var data: ByteArray) : Parcelable {
fun from(imageFile: File): Thumbnail {
val thumbnail = ThumbnailUtils
.extractThumbnail(BitmapFactory.decodeFile(imageFile.path),
TARGET_SIZE_MICRO_THUMBNAIL, TARGET_SIZE_MICRO_THUMBNAIL) ?:
Bitmap.createBitmap(TARGET_SIZE_MICRO_THUMBNAIL, TARGET_SIZE_MICRO_THUMBNAIL, Bitmap.Config.RGB_565)
TARGET_SIZE_MICRO_THUMBNAIL, TARGET_SIZE_MICRO_THUMBNAIL)
?: Bitmap.createBitmap(TARGET_SIZE_MICRO_THUMBNAIL, TARGET_SIZE_MICRO_THUMBNAIL, Bitmap.Config.RGB_565)
return Thumbnail(thumbnail.toByteArray())
}

fun from(context: Context, @DrawableRes resId: Int) :Thumbnail {
fun from(context: Context, @DrawableRes resId: Int): Thumbnail {
return from(BitmapFactory.decodeResource(context.resources, resId))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import de.dreier.mytargets.shared.models.Thumbnail
class ThumbnailConverter : TypeConverter<Blob, Thumbnail>() {

override fun getDBValue(model: Thumbnail?): Blob? {
return model?.blob
return if(model == null) null else Blob(model.data)
}

override fun getModelValue(data: Blob?): Thumbnail? {
return if (data != null) Thumbnail(data) else null
return if (data != null) Thumbnail(data.blob) else null
}

}

0 comments on commit 43e802b

Please sign in to comment.