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
import android.support.annotation.NonNull; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.View; | |
public abstract class ClickableAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> { | |
private ClickItemListener clickItemListener; | |
public void setClickItemListener(ClickItemListener clickItemListener) { | |
this.clickItemListener = clickItemListener; |
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
implementation "android.arch.paging:runtime:$paging_version" | |
implementation 'android.arch.paging:rxjava2:1.0.0-alpha1' |
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
//ViewModel, LiveData and LifeCycle | |
implementation "android.arch.lifecycle:extensions:1.1.1" | |
implementation "android.arch.lifecycle:runtime:1.1.1" | |
implementation "android.arch.lifecycle:reactivestreams:1.1.1" | |
kapt "android.arch.lifecycle:compiler:1.1.1" | |
//rx java2 | |
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2' | |
implementation 'io.reactivex.rxjava2:rxjava:2.1.14-RC1' |
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
class StackOverflowConfig { | |
companion object { | |
const val SERVER_BASE_URL = "https://api.stackexchange.com" | |
} | |
var serviceCreator: Retrofit | |
var apiService : StackOverflowAPI |
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
interface StackOverflowAPI { | |
@Headers("Content-Type: application/json", "Accept: application/json") | |
@GET("/2.2/users") | |
fun getTopUsers( | |
@Query("page") page: Int, | |
@Query("pagesize") pagesize: Int, | |
@Query("order") order: String = "desc", | |
@Query("sort") sort: String = "reputation", | |
@Query("site") site: String = "stackoverflow" |
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
class UsersDataSourceFactory @Inject constructor( | |
val api: StackOverflowConfig | |
) : DataSource.Factory<Int, User>() { | |
var observableEmitter: PublishSubject<UsersDataSource> = PublishSubject.create() | |
override fun create(): DataSource<Int, User> { | |
val usersDataSource = UsersDataSource(api) | |
observableEmitter.onNext(usersDataSource) | |
return usersDataSource | |
} | |
} |
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
class UsersDataSource @Inject constructor( | |
private val soApi: StackOverflowConfig | |
) : PageKeyedDataSource<Int, User>() { | |
var loadState: PublishSubject<NetworkState> = PublishSubject.create() | |
var disposables: CompositeDisposable = CompositeDisposable() | |
override fun loadInitial( | |
params: LoadInitialParams<Int>, | |
callback: LoadInitialCallback<Int, User> | |
) { |
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
class UsersRepository @Inject constructor( | |
var dataSourceFactory: UsersDataSourceFactory | |
) { | |
fun getUsers(): Observable<PagedList<User>> { | |
val config = PagedList.Config.Builder() | |
.setInitialLoadSizeHint(SO_PAGE_SIZE) | |
.setPageSize(SO_PAGE_SIZE) | |
.build() | |
val users: Observable<PagedList<User>> = | |
RxPagedListBuilder(dataSourceFactory, config) |
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
class TopUsersViewModel @Inject constructor( | |
val usersRepository: UsersRepository | |
) : ViewModel() { | |
fun getUserList(): LiveData<PagedList<User>> { | |
val result = usersRepository | |
.getUsers() | |
.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.toFlowable(BackpressureStrategy.BUFFER) | |
return LiveDataReactiveStreams.fromPublisher(result) |
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
class UsersAdapter : PagedListAdapter<User, UsersAdapter.UsersViewHolder>( | |
UsersDiffCallback() | |
) { | |
lateinit var clickListener: (User) -> (Unit) | |
override fun onCreateViewHolder( | |
parent: ViewGroup, | |
viewType: Int | |
): UsersViewHolder = UsersViewHolder( |
OlderNewer