Skip to content

Instantly share code, notes, and snippets.

View mantisbayne's full-sized avatar
💭
Coding

Meredith Bayne mantisbayne

💭
Coding
  • Austin, TX
View GitHub Profile
class UserViewModel(
private val userUseCase: ExampleUserUseCase
) : ViewModel() {
private val userReceivedLiveData = MutableLiveData<Unit>()
val userReceivedLiveData: LiveData<Unit>
get() = userReceivedLiveData
fun requestUser() {
class MainApplication : Application(), HasActivityInjector {
@Inject
lateinit var activityInjector: DispatchingAndroidInjector<Activity>
override fun onCreate() {
super.onCreate()
DaggerApplicationComponent
.builder()
class UserRepository(
private val userRemoteRepository: UserRemoteRepository,
private val userLocalRepository: UserLocalRepository
) : UserRepository {
override fun getLocalUser(): Single<User> {
return userLocalRepository.getUser()
}
override fun getRemoteUser(): Single<User> {
class UserViewModel @Inject constructor(
savedStateHandle: SavedStateHandle,
userRepository: UserRepository
) : ViewModel() {
val userId : String = savedStateHandle["uid"] ?:
throw IllegalArgumentException("missing user id")
val user : LiveData<User> = userRepository.getUser(userId)
}
interface ExampleUserRepository {
fun getUser(
id: Int,
name: String,
age: Int,
phone: String? = null
): Single<User>
fun getUser(id: Int): Single<User>
}
interface Webservice {
/**
* @GET declares GET request
* @Path("user") annotation, then userId parameter specified
* Uses Retrofit library
*/
@GET("/users/{user}")
fun getUser(@Path("user") userId: String): Single<User>
}
class ExampleUserUseCase(private val repository: ExampleUserRepository) {
sealed class Result {
object InProgess
data class OnSuccess(val users: List<ExampleUser>) : Result()
data class OnError(val error: Throwable) : Result()
}
fun execute() {
repository.getExampleUserList()
.map(mapper::map)
interface ExampleUserRepository {
fun getExampleUserList(
id: Int,
name: String,
age: Int,
phone: String? = null
): Single<List<ExampleUser>>
fun getUser(id: Int): Flowable<ExampleUser>
import android.os.Parcelable
import kotlinx.android.parcel.Parcelize
@Parcelize
data class ExampleUser(
val id: Long,
val name: String,
val age: Int,
val phone: String? = null
) : Parcelable
class ExampleFragment : BaseFragment() {
val exampleJobDao: ExampleJobDao by inject()
...
}