diff --git a/app/src/main/java/de/dreier/mytargets/features/statistics/StatisticsActivity.kt b/app/src/main/java/de/dreier/mytargets/features/statistics/StatisticsActivity.kt index 005966526..b4d4bae4c 100644 --- a/app/src/main/java/de/dreier/mytargets/features/statistics/StatisticsActivity.kt +++ b/app/src/main/java/de/dreier/mytargets/features/statistics/StatisticsActivity.kt @@ -207,7 +207,7 @@ class StatisticsActivity : ChildActivityBase(), LoaderManager.LoaderCallbacks
  • 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)) } @@ -221,7 +221,7 @@ class StatisticsActivity : ChildActivityBase(), LoaderManager.LoaderCallbacks
  • 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)) } diff --git a/app/src/main/java/de/dreier/mytargets/features/statistics/Tag.kt b/app/src/main/java/de/dreier/mytargets/features/statistics/Tag.kt index c45c3b529..17ae8bbd4 100644 --- a/app/src/main/java/de/dreier/mytargets/features/statistics/Tag.kt +++ b/app/src/main/java/de/dreier/mytargets/features/statistics/Tag.kt @@ -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 @@ -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 diff --git a/shared/src/main/java/de/dreier/mytargets/shared/models/Thumbnail.kt b/shared/src/main/java/de/dreier/mytargets/shared/models/Thumbnail.kt index 0bccfced6..d95df0edf 100644 --- a/shared/src/main/java/de/dreier/mytargets/shared/models/Thumbnail.kt +++ b/shared/src/main/java/de/dreier/mytargets/shared/models/Thumbnail.kt @@ -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. @@ -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)) } } diff --git a/shared/src/main/java/de/dreier/mytargets/shared/utils/typeconverters/ThumbnailConverter.kt b/shared/src/main/java/de/dreier/mytargets/shared/utils/typeconverters/ThumbnailConverter.kt index 2129ab30e..1a25b1208 100644 --- a/shared/src/main/java/de/dreier/mytargets/shared/utils/typeconverters/ThumbnailConverter.kt +++ b/shared/src/main/java/de/dreier/mytargets/shared/utils/typeconverters/ThumbnailConverter.kt @@ -23,11 +23,11 @@ import de.dreier.mytargets.shared.models.Thumbnail class ThumbnailConverter : TypeConverter() { 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 } }