Created
December 3, 2023 02:44
-
-
Save 0xZhangKe/09087416137ba48a92c7461548db1ab5 to your computer and use it in GitHub Desktop.
clean-adapter-sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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