Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1039: Introduce custom type oppia image [WIP] #2012

Closed

Conversation

prayutsu
Copy link
Contributor

Explanation

Fixes #1039:
Custom Type OppiaImage is introduced

Checklist

  • The PR title starts with "Fix #bugnum: ", followed by a short, clear summary of the changes. (If this PR fixes part of an issue, prefix the title with "Fix part of #bugnum: ...".)
  • The PR explanation includes the words "Fixes #bugnum: ..." (or "Fixes part of #bugnum" if the PR only partially fixes an issue).
  • The PR follows the style guide.
  • The PR does not contain any unnecessary auto-generated code from Android Studio.
  • The PR is made from a branch that's not called "develop".
  • The PR is made from a branch that is up-to-date with "develop".
  • The PR's branch is based on "develop" and not on any other branch.
  • The PR is assigned to an appropriate reviewer in both the Assignees and the Reviewers sections.

urlDrawable, { it }
)
private fun findImageType(urlDrawable: UrlDrawable, oppiaImage: OppiaImage): CustomImageTarget {
return when(oppiaImage) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am stuck here, what should be the type Argument here for CustomImageTarget

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BenHenning Can you please help me here?

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you want the target to be an OppiaImage but you need to update the Glide pipeline to support OppiaImage as a valid image type so that it knows how to bind it to a target (e.g. by extracting its bitmap or picture).

The idea behind OppiaImage is that it's self-service: you just load an Oppia image and bind it to a CustomImageTarget and Oppia's Glide integration takes care of the rest.

You can take a look at how we set up the custom support for Picture today--Glide doesn't support SVGs out of the box.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BenHenning I need a bit more help here. How could I update Glide pipeline to support our OppiaImage?

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@prayutsu take a look at RepositoryGlideModule. It registers a couple of custom type translators: SVG -> PictureDrawable, and InputStream -> SVG. We select this custom pipeline in GlideImageLoader by asking Glide to load a 'PictureDrawable' and it knows how to map from PictureDrawable to SVG to InputStream (which it will retrieve & provide for us). My suggestion is investigating whether we could set up a similar pipeline from Bitmaps/Pictures to the custom Oppia image type so that we can load that from Glide directly.

Note that we would probably still specialize between SVG & Bitmap, e.g.:

    Glide.with(context)
      .`as`(OppiaImage.BitmapImage::class.java)
      .load(model)
      .intoTarget(target)

    Glide.with(context)
      .`as`(OppiaImage.SvgImage::class.java)
      .fitCenter()
      .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.NONE))
      .load(model)
      .intoTarget(target)

Does that help clarify?

@prayutsu
Copy link
Contributor Author

@BenHenning Is the PR progressing in the right direction?

Copy link
Sponsor Member

@BenHenning BenHenning left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @prayutsu! I think this is on the right track, and I left some design comments.

Also, I suggest thinking through how you're going to test this. I don't think the current image loading pipeline has any tests, and it would be very helpful to get some added. Let me know if you run into any problems with that.

urlDrawable, { it }
)
private fun findImageType(urlDrawable: UrlDrawable, oppiaImage: OppiaImage): CustomImageTarget {
return when(oppiaImage) {
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you want the target to be an OppiaImage but you need to update the Glide pipeline to support OppiaImage as a valid image type so that it knows how to bind it to a target (e.g. by extracting its bitmap or picture).

The idea behind OppiaImage is that it's self-service: you just load an Oppia image and bind it to a CustomImageTarget and Oppia's Glide integration takes care of the rest.

You can take a look at how we set up the custom support for Picture today--Glide doesn't support SVGs out of the box.

Comment on lines 4 to 5
class BitmapImage(val urlDrawable: UrlImageParser.UrlDrawable) : OppiaImage()
class SvgImage(val urlDrawable: UrlImageParser.UrlDrawable) : OppiaImage()
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't be referencing UrlImageParser things here--that's a dependency inversion. OppiaImage is part of image loading, and UrlImageParser depends on image loading.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

* internet.
*/
fun loadSvg(imageUrl: String, target: ImageTarget<PictureDrawable>)
fun loadOppiaImage(imageUrl: String, target: ImageTarget<OppiaImage>)
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's okay to have both loadBitmap and loadSvg--we probably shouldn't assume a URL ends with svg. This is a not-so-great assumption by UrlImageParser, but better to do it at the HTML level than in the actual image loader.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, reverted these changes

@BenHenning BenHenning assigned prayutsu and unassigned BenHenning Oct 22, 2020
@prayutsu
Copy link
Contributor Author

Thanks @prayutsu! I think this is on the right track, and I left some design comments.

Also, I suggest thinking through how you're going to test this. I don't think the current image loading pipeline has any tests, and it would be very helpful to get some added. Let me know if you run into any problems with that.

Actually, right now I have no idea how will we test this but as the PR gets closer to correct implementation, I'll try it.
Is it fine to do so?

@prayutsu prayutsu assigned BenHenning and unassigned prayutsu Oct 24, 2020
@BenHenning
Copy link
Sponsor Member

@prayutsu it's fine to approach tests after getting a working implementation, though we should keep the PR in draft until your tests are also finalized & ready for review.

@BenHenning BenHenning assigned prayutsu and unassigned BenHenning Oct 27, 2020
@prayutsu
Copy link
Contributor Author

Closing the PR as unassigning myself from the issue

@prayutsu prayutsu closed this Dec 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Introduce custom type - OppiaImage that renders both Bitmaps and Svgs
3 participants