Skip to content
This repository has been archived by the owner on Jul 19, 2022. It is now read-only.

Code Explanations #1

Open
ashishkharcheiu opened this issue Mar 9, 2020 · 5 comments
Open

Code Explanations #1

ashishkharcheiu opened this issue Mar 9, 2020 · 5 comments

Comments

@ashishkharcheiu
Copy link

In addPendingUploads() method, you have added id as it which is the url of image like file://image/path

How does passing url of image path which is equal to it in id works inside AlbumImage?
I see both link and id has it and I am confused on how the code is working here.

Could you please explain the above query?

` fun addPendingUploads(albumId: String, urls: List, onAdded: (() -> Unit)?) {

    val newImages: List<AlbumImage> = urls.map {

        
        AlbumImage(
            id = it,
            datetime = (System.currentTimeMillis() / 1000).toInt(),
            link = it,
            albumId = albumId,
            queuedForUpload = true
        )

    }
    executors.diskIO().execute {
        Log.d(TAG, "addPendingUploads: inside executors.diskIO().execute")
        imageDao.insertAll(newImages)
        if (onAdded != null) {
            Log.d(TAG, "addPendingUploads: onAdded != null")
            onAdded()
        }
    }
    rateLimiter.reset(rateLimitKey(albumId))
}

`

@xiprox
Copy link
Owner

xiprox commented Mar 11, 2020

Hey.

This is how it works:

  1. File picked
  2. File is added to db with path as its primary key to identify, and its queuedForUpload field as true
  3. ImageUploadWorker will get all image records where queuedForUpload is true
    val pendingUploads = imageDao.findAllPendingUploads()
    @Query("SELECT * FROM AlbumImage WHERE queued_for_upload = 1")
  4. ImageUploadWorker will make an upload request and delegate result to UploadStatusDelegate:
  5. UploadStatusDelegate will update this temporary image record in db to update UI for various states like pending, uploading, error, and success
  6. When upload succeeds, delegate will parse response from Imgur and pass it on to updateUploadSuccess of AlbumDetailsRepository:
  7. AlbumDetailsRepository will delete the temporary record with file path as id, and insert a new record with ID from Imgur

At least, that's what I think is happening. Maybe I forgot something, it's been a while. :)

@ashishkharcheiu
Copy link
Author

@xiprox Thank you so much. I will review your explanation and compare it with the code.

I request you to keep the issue open, so if required I can ask for further explanation.

@xiprox
Copy link
Owner

xiprox commented Mar 11, 2020

You're welcome.

I can't promise prompt response, but sure.

@ashishkharcheiu
Copy link
Author

val image = Gson().fromJson<ImgurResponse<AlbumImage>>(response.bodyAsString)

In UploadStatusDelegate class you have used, this the above code to convert from Json Response received from Imgur server when an image is uploaded to convert to Kotlin Objects.

Is there a better way to convert the same with Retrofit Converter Gson library in the model itself and getting rid of the ImgurResponseConverterFactory fromJson extension altogether?

Please explain the reason why you choose to do the above way using custom ImgurResponseConverter...

@xiprox
Copy link
Owner

xiprox commented Mar 21, 2020

What ImgurResponseConverter and ImgurResponseConverterFactory do, together, is unwrap JSON objects from Imgur, which are structured like

{
  "data": { "someField": "hey" },
  "success": true,
  "status": 200
}

into just { "someField": "hey"' }.

If you look at ImgurService, you will notice that all our functions ultimately return objects of direct types (e.g. Album and not ImgurResponse<Album>). So later, when we are using this data elsewhere, we can just do something like result.someField instead of result.data.someField.

It's really not an essential thing. One could get rid of it and just do result.data.someField everywhere. I do think it makes things a little bit nicer though.

If you would prefer to get rid of it, removing the factory and the converter, then wrapping response types in ImgurResponse should work.

@xiprox xiprox changed the title Code Explaination for addPendingUploads() method. Code Explanations Mar 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants