-
-
Save rt4914/ea26d973a9a62b682bc93e61c06e71ef to your computer and use it in GitHub Desktop.
This `LessonThumbnailImageView` is similar to `ImageRegionSelectionInteractionView`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.oppia.app.customview | |
import android.content.Context | |
import android.util.AttributeSet | |
import android.widget.ImageView | |
import androidx.appcompat.widget.AppCompatImageView | |
import androidx.fragment.app.FragmentManager | |
import org.oppia.app.fragment.InjectableFragment | |
import org.oppia.app.model.LessonThumbnail | |
import org.oppia.util.gcsresource.DefaultResourceBucketName | |
import org.oppia.util.parser.DefaultGcsPrefix | |
import org.oppia.util.parser.ImageLoader | |
import org.oppia.util.parser.ImageViewTarget | |
import org.oppia.util.parser.RevisionCardHtmlParserEntityType | |
import org.oppia.util.parser.ThumbnailDownloadUrlTemplate | |
import javax.inject.Inject | |
/** A custom [AppCompatImageView] used to show lesson thumbnails. */ | |
class LessonThumbnailImageView @JvmOverloads constructor( | |
context: Context, | |
attrs: AttributeSet? = null, | |
defStyleAttr: Int = 0 | |
) : AppCompatImageView(context, attrs, defStyleAttr) { | |
val imageView: ImageView = this | |
private lateinit var lessonThumbnail: LessonThumbnail | |
@Inject | |
lateinit var imageLoader: ImageLoader | |
@Inject | |
@field:RevisionCardHtmlParserEntityType | |
lateinit var entityType: String | |
@Inject | |
@field:DefaultResourceBucketName | |
lateinit var resourceBucketName: String | |
@Inject | |
@field:ThumbnailDownloadUrlTemplate | |
lateinit var thumbnailDownloadUrlTemplate: String | |
@Inject | |
@field:DefaultGcsPrefix | |
lateinit var gcsPrefix: String | |
private lateinit var entityId: String | |
/** | |
* Sets the URL for the image & initiates loading it. This is intended to be called via data-binding. | |
*/ | |
fun setLessonThumbnail(lessonThumbnail: LessonThumbnail) { | |
this.lessonThumbnail = lessonThumbnail | |
if (lessonThumbnail.thumbnailFilename.isNotEmpty()) { | |
loadImage(lessonThumbnail.thumbnailFilename) | |
} | |
} | |
/** Loads an image using Glide from [filename]. */ | |
private fun loadImage(filename: String) { | |
val imageName = String.format( | |
thumbnailDownloadUrlTemplate, | |
entityType, | |
entityId, | |
filename | |
) | |
val imageUrl = "$gcsPrefix/$resourceBucketName/$imageName" | |
if (imageUrl.endsWith("svg", ignoreCase = true)) { | |
imageLoader.loadSvg(imageUrl, ImageViewTarget(this)) | |
} else { | |
imageLoader.loadBitmap(imageUrl, ImageViewTarget(this)) | |
} | |
} | |
fun setEntityId(entityId: String) { | |
this.entityId = entityId | |
} | |
override fun onAttachedToWindow() { | |
super.onAttachedToWindow() | |
FragmentManager.findFragment<InjectableFragment>(this) | |
.createViewComponent(this) | |
.inject(this) | |
} | |
} |
Author
rt4914
commented
Aug 5, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment