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

Introduce kotlin coroutines flow #21

Merged
merged 8 commits into from
Oct 10, 2019

Conversation

Serchinastico
Copy link
Contributor

📌 References

🎩 What is the goal?

To introduce Flow into the project. The approach is not the best one but we now know this is possible.

📝 How is it being implemented?

  • Update some other dependencies I missed in Update dependencies #20 .
  • Include kotlin coroutines lib in Android project. It was working before this PR because there was a known bug in the gradle plugin version we were using that was including it automatically for us.
  • Refactor presenter/view communication by using a single render method and passing a sealed class. This is just an idea for the future I've been having for a while, doing so can make it way easier to test presenter+view interaction with the help of snapshot testing, it's not really needed for this project though.
  • Change async actual implementations to use the current scope. This means we have structured concurrency instead of relying on global scopes. The presenter now implements CoroutineScope and once it's dismissed it will just cancel running jobs for us.
  • Transform the only use case of the project into a Flow of images. It's a forced example but it's the easiest one to test flow in a multiplatform environment. The flow works by asking the API every 5 seconds for new images, in order to implement it I had to create a timer flow that returns a Unit value with a configurable interval.
  • The use case now talks with the PhotosFlow instead of doing it with the API directly.
  • Add an implementation of the Delay interface to the iOS Dispatcher. This let us call the delay function that we are using in the timer flow. The previous implementation would just stop whenever it found a delay, making the app useless in iOS. The new code has been extracted from here.

💥 How can it be tested?

Automatically!

Copy link
Contributor

@tonilopezmr tonilopezmr left a comment

Choose a reason for hiding this comment

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

Do you show numberOfLikes in android?

view.showLoader()
getPhotosJob = launchInMain {
try {
logInfo(TAG, "Start getting photos")
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we lose the logs? I'd prefer to keep those logs to understand in common module how is going the code in both platforms (android or iOS)

Copy link
Contributor

Choose a reason for hiding this comment

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

Nevermind 👍

fun onCreate() = launchInMain {
view.render(View.State.Loading)
getAllPhotos()
.flowOnBackground()
Copy link
Contributor

Choose a reason for hiding this comment

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

👏 👏 👏


suspend fun get(): Flow<Photos> =
timer(repeatEvery = updateInterval)
.map { photosApiClient.getPhotos() }
Copy link
Contributor

Choose a reason for hiding this comment

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

This is because you emit Unit but after that, you map it to the Photos you get from .getPhotos(), right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, the timer does not emit anything of value, just happens to do it in a convenient interval :)

@tonilopezmr tonilopezmr self-requested a review October 10, 2019 11:24
Copy link
Contributor

@tonilopezmr tonilopezmr left a comment

Choose a reason for hiding this comment

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

🚀

@tonilopezmr tonilopezmr merged commit 1f185b1 into master Oct 10, 2019
@tonilopezmr tonilopezmr deleted the introduce-kotlin-coroutines-flow branch October 10, 2019 11:24
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.

2 participants