Skip to content

Instantly share code, notes, and snippets.

@yoloroy
Last active July 9, 2022 19:21
Show Gist options
  • Select an option

  • Save yoloroy/fa900165ae9982850f5e6d5bf1ac739b to your computer and use it in GitHub Desktop.

Select an option

Save yoloroy/fa900165ae9982850f5e6d5bf1ac739b to your computer and use it in GitHub Desktop.
object Presenter { // presentation layer
fun loadDataForList() {
val dataToShow: List<UiDataShort> = DataStore.retrieveDataList()
}
fun loadDataForDetailedView(clickedData: UiDataShort) {
val dataToShow: UIDataDetailed = DataStore.getDetails(clickedData)
}
}
interface UiDataShort { // some list items
val title: String
val authorName: String
val authorIconUrl: String
}
interface UIDataDetailed { // data for some full page
val title: String
val headerImage: String
val authorName: String
val authorIconUrl: String
val content: String
}
object DataStore { // data payer
private val someData = List(10) { // data layer has access to full data
DataEntity(-1, "title", "header img url", "author", "author ic url", "content", false)
}
fun retrieveDataList(): List<UiDataShort> {
return someData.map { it.ProxyShort() }
}
fun getDetails(short: UiDataShort): UIDataDetailed {
require(short is DataEntity.Inner) // otherwise, this data was not created in the data layer
return short.entity.ProxyDetailed()
}
}
class DataEntity(
val id: Int,
val title: String,
val headerImage: String,
val authorName: String,
val authorIconUrl: String,
val content: String,
val isLast: Boolean
) {
abstract class Inner(val entity: DataEntity)
inner class ProxyShort : Inner(this@DataEntity), UiDataShort {
override val title: String get() = this@DataEntity.title
override val authorName: String get() = this@DataEntity.authorName
override val authorIconUrl: String get() = this@DataEntity.authorIconUrl
}
inner class ProxyDetailed : Inner(this@DataEntity), UIDataDetailed {
override val title: String get() = this@DataEntity.title
override val headerImage: String get() = this@DataEntity.headerImage
override val authorName: String get() = this@DataEntity.authorName
override val authorIconUrl: String get() = this@DataEntity.authorIconUrl
override val content: String get() = this@DataEntity.content
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment