Skip to content

Instantly share code, notes, and snippets.

@lukasdylan
Last active September 5, 2018 13:12
Show Gist options
  • Save lukasdylan/59ee7f10692103d9de08fe92a787a32d to your computer and use it in GitHub Desktop.
Save lukasdylan/59ee7f10692103d9de08fe92a787a32d to your computer and use it in GitHub Desktop.
class DatabaseHelper(context: Context) : ManagedSQLiteOpenHelper(context,
DatabaseConstant.DATABASE_NAME, null, DatabaseConstant.DATABASE_VERSION) {
override fun onCreate(database: SQLiteDatabase?) {
database?.createTable(DetailEventResponse.TABLE_NAME, true,
DetailEventResponse.EVENT_ID to TEXT + PRIMARY_KEY,
DetailEventResponse.HOME_TEAM_ID to TEXT,
DetailEventResponse.AWAY_TEAM_ID to TEXT,
DetailEventResponse.HOME_TEAM_NAME to TEXT,
DetailEventResponse.AWAY_TEAM_NAME to TEXT,
DetailEventResponse.HOME_TEAM_SCORE to TEXT,
DetailEventResponse.AWAY_TEAM_SCORE to TEXT,
DetailEventResponse.HOME_GOAL_DETAILS to TEXT,
DetailEventResponse.AWAY_GOAL_DETAILS to TEXT,
DetailEventResponse.HOME_RED_CARDS to TEXT,
DetailEventResponse.AWAY_RED_CARDS to TEXT,
DetailEventResponse.HOME_YELLOW_CARDS to TEXT,
DetailEventResponse.AWAY_YELLOW_CARDS to TEXT,
DetailEventResponse.HOME_GOALKEEPER to TEXT,
DetailEventResponse.AWAY_GOALKEEPER to TEXT,
DetailEventResponse.HOME_DEFENDERS to TEXT,
DetailEventResponse.AWAY_DEFENDERS to TEXT,
DetailEventResponse.HOME_MIDFIELDERS to TEXT,
DetailEventResponse.AWAY_MIDFIELDERS to TEXT,
DetailEventResponse.HOME_FORWARDERS to TEXT,
DetailEventResponse.AWAY_FORWARDERS to TEXT,
DetailEventResponse.HOME_SUBSTITUTES to TEXT,
DetailEventResponse.AWAY_SUBSTITUTES to TEXT,
DetailEventResponse.DATE_EVENT to TEXT,
DetailEventResponse.DATE to TEXT,
DetailEventResponse.TIME to TEXT)
}
override fun onUpgrade(database: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
database?.dropTable(DetailEventResponse.TABLE_NAME, true)
}
}
fun DatabaseHelper.loadDetailEventResponse2(): List<DetailEventResponse> {
val result = use { select(DetailEventResponse.TABLE_NAME) }
return result.parseList(classParser())
}
@Module
class DatabaseModule {
@Provides
@Singleton
fun provideDatabase(application: CoreApplication): DatabaseHelper {
return DatabaseHelper(application.applicationContext)
}
}
@Module
class MatchListModule {//module untuk fragment
@Provides
fun provideUseCase(apiServices: ApiServices, databaseHelper: DatabaseHelper): MatchListUseCase {
return MatchListUseCaseImpl(apiServices, databaseHelper)
}
@Provides
fun provideFactory(matchListUseCase: MatchListUseCase): MatchListViewModelFactory {
return MatchListViewModelFactory(matchListUseCase)
}
@Provides
fun provideViewModel(fragment: MatchListFragment,
factory: MatchListViewModelFactory): MatchListViewModel {
return ViewModelProviders.of(fragment, factory).get(MatchListViewModel::class.java)
}
}
interface MatchListUseCase {
fun loadMatchesByType(matchType: Int, leagueId: Int = ApiConstant.PREMIERE_LEAGUE_ID): Single<List<DetailEventResponse>>
fun loadFavoriteMatch2(): List<DetailEventResponse>
}
class MatchListUseCaseImpl(private val apiServices: ApiServices) : MatchListUseCase {
override fun loadFavoriteMatch2(): List<DetailEventResponse> {
return databaseHelper.loadDetailEventResponse2()
}
override fun loadMatchesByType(matchType: Int, leagueId: Int): Single<List<DetailEventResponse>> {
return if (matchType == MatchListFragment.LAST_MATCH_TYPE) {
apiServices.loadPastLeagueEvent(leagueId.toString())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.map {
if (it.listEvent.isNotEmpty()) {
return@map it.listEvent
} else {
return@map mutableListOf<DetailEventResponse>()
}
}
} else {
apiServices.loadNextLeagueEvent(leagueId.toString())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.map {
if (it.listEvent.isNotEmpty()) {
return@map it.listEvent
} else {
return@map mutableListOf<DetailEventResponse>()
}
}
}
}
private fun <T> returnAsObservable(func: Callable<T>): Observable<T> {
return Observable.create {
try {
it.onNext(func.call())
} catch (ex: Exception) {
Log.e(MatchListUseCaseImpl::class.java.simpleName, "Error reading from the database", ex)
}
}
}
}
class MatchListViewModel(private val matchListUseCase: MatchListUseCase) : AndroidViewModel(application) {
private val compositeDisposable = CompositeDisposable()
val errorSnackBar = SingleLiveEvent<String>()
val matchResultList = MutableLiveData<List<DetailEventResponse>>()
override fun onCleared() {
compositeDisposable.dispose()
super.onCleared()
}
fun loadData(screenType: Int) {
when (screenType) {
MatchListFragment.LAST_MATCH_TYPE, MatchListFragment.NEXT_MATCH_TYPE -> {
loadMatchFromServer(screenType)
}
else -> {
loadFavoriteMatchFromDb()
}
}
}
private fun loadFavoriteMatchFromDb() {
// compositeDisposable.add(
// matchListUseCase.loadFavoriteMatch()
// .subscribeBy(onNext = {
// matchResultList.value = it
// }, onError = {
// errorSnackBar.value = it.localizedMessage
// }))
matchResultList.value = matchListUseCase.loadFavoriteMatch()
}
private fun loadMatchFromServer(screenType: Int) {
compositeDisposable.add(
matchListUseCase.loadMatchesByType(screenType)
.subscribeBy(onSuccess = {
matchResultList.value = it
}, onError = {
errorSnackBar.value = it.localizedMessage
})
)
}
}
class MatchListViewModelFactory(private val matchListUseCase: MatchListUseCase,
private val application: CoreApplication) : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(MatchListViewModel::class.java)) {
return MatchListViewModel(matchListUseCase, application) as T
}
throw IllegalArgumentException("Unknown ViewModel class")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment