Skip to content

Instantly share code, notes, and snippets.

View Rasalexman's full-sized avatar
🌍
i hope you are going to have an amazing day

Alexandr Minkin Rasalexman

🌍
i hope you are going to have an amazing day
View GitHub Profile
@Rasalexman
Rasalexman / liveData.kt
Created January 19, 2020 00:24
Convert example
liveData(context = viewModelScope.coroutineContext + Dispatcher.IO) {
  emitSource(pagesRepository.getNextPageData(eventPageLiveData).asLiveData().map {
it.convertTo<MyUIModel>()
})
}
@Rasalexman
Rasalexman / PageRepository.kt
Last active January 19, 2020 01:07
PageRepository with Flow
class PageRepository(
private val simpleApi: ISimpleApi
) : IPageRepository {
override suspend fun getNextPageData(eventPageLiveData: LiveData<ViewEvent<Int>>): Flow<Result<List<MyUIModel>>> {
return flow {
// convert liveData to Flow
eventPageLiveData.asFlow().collect { viewEvent ->
val viewPage = viewEvent.data
// emit success state
@Rasalexman
Rasalexman / ISimpleApi.kt
Last active January 19, 2020 01:08
api interface for retrofit
interface ISimpleApi {
@GET("data/list")
suspend fun getListData(
@Query("page") page: Int
): List<SomeRemoteModel>
}
@Rasalexman
Rasalexman / IPageRepository.kt
Last active January 18, 2020 23:59
Repository abstraction with Flow
interface IPageRepository {
suspend fun getNextPageData(eventPageLiveData: LiveData<ViewEvent<Int>>): Flow<Result<List<MyUIModel>>>
}
@Rasalexman
Rasalexman / IPageRepository.kt
Last active January 18, 2020 23:59
Repository interface with suspend
interface IPageRepository {
suspend fun getNextPageData(page: Int): Result<List<MyUIModel>>
}
@Rasalexman
Rasalexman / MyViewModel.kt
Last active January 19, 2020 01:03
ViewModel with Flow
class MyViewModel(
private val pagesRepository: IPagesRepository
) : ViewModel() {
val eventPageLiveData = MutableLiveData<ViewEvent<Int>>()
val resultLiveData: LiveData<Result<List<MyUIModel>>> by lazy {
// do some work on IO
liveData(context = viewModelScope.coroutineContext + Dispatcher.IO) {
emitSource(pagesRepository.getNextPageData(eventPageLiveData).asLiveData())
@Rasalexman
Rasalexman / MyViewModel.kt
Last active January 18, 2020 23:59
ViewModel with LiveData ViewEvent
class MyViewModel(
private val pagesRepository: IPagesRepository
) : ViewModel() {
val eventPageLiveData = MutableLiveData<ViewEvent<Int>>()
val resultLiveData: LiveData<Result<List<MyUIModel>>> by lazy {
eventPageLiveData.switchMap { pageEvent ->
liveData(context = viewModelScope.coroutineContext + Dispatchers.IO) {
emit(pagesRepository.getNextPageData(pageEvent.data))
class MyClass {
}
class MyClassNeedAnother(val myclass: MyClass) {
}
val kodi = kodi {
bind<MyClass>() with provider { MyClass() }
typealias UserAddressBuilderLambda = () -> UserAddress // 1
class UserAddressBuilder(
addressInit: UserAddressBuilder.() -> Unit // 2
) : UserAddressBuilderLambda {
init {
this.addressInit() // 3
}
var userCity: String = ""
var userCountry: String = ""
typealias UserAddressBuilderLambda = () -> UserAddress // 1
class UserAddressBuilder(
addressInit: UserAddressBuilder.() -> Unit // 2
) : UserAddressBuilderLambda {
init {
this.addressInit() // 3
}
var userCity: String = ""
var userCountry: String = ""