nihk / videopager

An app showing how to make an Instagram/YouTube Shorts/TikTok style video pager
MIT License
109 stars 17 forks source link

It is suggested that each line of code should be annotated for your convenience #3

Closed nikolamakin closed 2 years ago

nikolamakin commented 3 years ago

It is suggested that each line of code should be annotated for your convenience, which is friendly to rookies

nickrose-ms commented 3 years ago

Are there any parts in particular you'd like more clarification on?

nikolamakin commented 3 years ago

Are there any parts in particular you'd like more clarification on?

Hello, code God

Can you write some code logic of network request? Thank you

nihk commented 3 years ago

In this repo I've abstracted the video data source to an interface VideoDataRepository. Its implementation can be anything that returns a flow of video data. For example, if you wanted to do a one-shot network request for your video data using retrofit, an implementation could look like:

class OneShotNetworkVideoDataRepository(private val api: YourApi) : VideoDataRepository {
  override fun videoData(): Flow<List<VideoData>> = flow {
    val videos = api.fetchVideos() // be sure to map your API response type to List<VideoData>
    emit(videos)
  }
}

Note I haven't included error handling and made an assumption that api.fetchVideos() is a suspending function called on another dispatcher like DIspatchers.IO.

You could also change VideoDataRepository to return something like a Flow<State<List<VideoData>> if you want to better model states like loading/success/error.

Hope that helps. Let me know if you want more information on that.