Skip to content

Instantly share code, notes, and snippets.

@0xZhangKe
Created December 3, 2023 02:44
Show Gist options
  • Save 0xZhangKe/09087416137ba48a92c7461548db1ab5 to your computer and use it in GitHub Desktop.
Save 0xZhangKe/09087416137ba48a92c7461548db1ab5 to your computer and use it in GitHub Desktop.
clean-adapter-sample
data class UserEntity(val name: String, val avatar: String)
interface UserService {
@GET("/user")
suspend fun getUserInfo(@query("id") id: String): UserEntity
}
data class User(val name: String, val avatar: String)
class UserEntityAdapter @Inject constructor() {
fun toUser(entity: UserEntity): User {
return User(name = entity.name, avatar = entity.avatar)
}
}
class UserRepo @Inject constructor(
private val userEntityAdapter: UserEntityAdapter,
) {
private val userService: UserService by lazy {
retrofit.create(UserService::class.java)
}
suspend fun getUser(id: String): User {
return userService.getUserInfo(id).let(userEntityAdapter::toUser)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment