Skip to content

Commit

Permalink
(#2) Add repository pattern.
Browse files Browse the repository at this point in the history
  • Loading branch information
myung jun Hyun committed Nov 19, 2019
1 parent 845f2a9 commit 636bf2d
Show file tree
Hide file tree
Showing 13 changed files with 184 additions and 83 deletions.
17 changes: 13 additions & 4 deletions app/src/main/java/com/mashup/MashupApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package com.mashup
import android.app.Application
import com.facebook.stetho.Stetho
import com.jakewharton.threetenabp.AndroidThreeTen
import com.mashup.app.notices.noticeModule
import com.mashup.app.notices.NoticeModule
import com.mashup.di.ApiModule
import com.mashup.di.ApplicationModule
import com.mashup.di.NetworkModule
import org.koin.android.ext.koin.androidContext
import org.koin.core.context.startKoin

class MashupApplication: Application() {
class MashupApplication : Application() {

companion object {
@JvmStatic
Expand All @@ -19,10 +22,16 @@ class MashupApplication: Application() {
instance = this
startKoin {
androidContext(this@MashupApplication)
modules(noticeModule)
modules(
listOf(
ApplicationModule,
ApiModule,
NetworkModule,
NoticeModule
))
}
initJSR310()

if (BuildConfig.DEBUG) {
Stetho.initializeWithDefaults(this)
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/mashup/MyApplication.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.mashup

import android.app.Application
import com.mashup.app.notices.noticeModule
import com.mashup.app.notices.NoticeModule
import org.koin.android.ext.koin.androidContext
import org.koin.core.context.startKoin

Expand All @@ -11,7 +11,7 @@ class MyApplication : Application() {

startKoin {
androidContext(this@MyApplication)
modules(noticeModule)
modules(NoticeModule)
}
}
}
30 changes: 24 additions & 6 deletions app/src/main/java/com/mashup/SplashActivity.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
package com.mashup

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import androidx.appcompat.app.AppCompatActivity
import com.mashup.app.notices.NoticesActivity
import com.mashup.repository.NoticesRepository
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers
import org.koin.android.ext.android.inject

class SplashActivity : AppCompatActivity() {
private val noticesRepository: NoticesRepository by inject()
private val compositeDisposable = CompositeDisposable()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash)
Handler().postDelayed({
startActivity(Intent(this, NoticesActivity::class.java))
finish()
},2000)

compositeDisposable.add(
noticesRepository
.getNoticeList()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
startActivity(Intent(this, NoticesActivity::class.java))
finish()
}, {
it.printStackTrace()
}))
}

override fun onDestroy() {
compositeDisposable.clear()
super.onDestroy()
}
}
46 changes: 0 additions & 46 deletions app/src/main/java/com/mashup/api/MashupClient.kt

This file was deleted.

7 changes: 2 additions & 5 deletions app/src/main/java/com/mashup/app/auth/LoginViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ package com.mashup.app.auth

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.google.firebase.auth.PhoneAuthProvider
import com.mashup.api.MashupClient
import com.mashup.api.user.UserService
import com.mashup.api.user.request.UserRegisterRequest
import com.mashup.model.Team
import com.mashup.model.User
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit
import javax.security.auth.callback.Callback

class LoginViewModel: ViewModel() {
Expand All @@ -20,14 +17,14 @@ class LoginViewModel: ViewModel() {

private var registerDisposable: Disposable? = null

fun register(email: String, password: String, name: String, team: Team) {
/*fun register(email: String, password: String, name: String, team: Team) {
registerDisposable?.dispose()
registerDisposable = MashupClient.getService(UserService::class.java)
.register(UserRegisterRequest(email = email, password = password, name = name, team = team))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { userData.postValue(it) }
}
}*/

fun phoneAuth(phoneNumber : String, callbacks : Callback){

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/mashup/app/notices/NoticeModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package com.mashup.app.notices
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.dsl.module

val noticeModule = module {
viewModel { NoticesViewModel() }
val NoticeModule = module {
viewModel { NoticesViewModel(get()) }
}
42 changes: 24 additions & 18 deletions app/src/main/java/com/mashup/app/notices/NoticesViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,44 @@ package com.mashup.app.notices
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.mashup.api.MashupClient
import com.mashup.api.notice.NoticeService
import com.mashup.model.Notice
import com.mashup.repository.NoticesRepository
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers

class NoticesViewModel : ViewModel() {
class NoticesViewModel(
private val noticesRepository: NoticesRepository
) : ViewModel() {
private val _items = MutableLiveData<List<Notice>>().apply { value = emptyList() }
val items: LiveData<List<Notice>> = _items
private val compositeDisposable = CompositeDisposable()

init {
getNotice()
}

private val compositeDisposable = CompositeDisposable()

private fun getNotice() {
val dummyUserId = 1
compositeDisposable.add(MashupClient.getService(NoticeService::class.java)
.getNoticeList()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
_items.value = it.map { notice ->
notice.attendanceSet.find { noticeAttendance -> noticeAttendance.user.pk == dummyUserId }?.let { attendance ->
notice.apply { notice.userAttendance = attendance.vote }
} ?: notice
}
}, {
it.printStackTrace()
})
compositeDisposable.add(
noticesRepository
.getNoticeList()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
_items.value = it.map { notice ->
notice.attendanceSet.find { noticeAttendance -> noticeAttendance.user.pk == dummyUserId }?.let { attendance ->
notice.apply { notice.userAttendance = attendance.vote }
} ?: notice
}
}, {
it.printStackTrace()
})
)
}

override fun onCleared() {
compositeDisposable.clear()
super.onCleared()
}
}
11 changes: 11 additions & 0 deletions app/src/main/java/com/mashup/di/ApiModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.mashup.di

import com.mashup.api.notice.NoticeService
import com.mashup.api.user.UserService
import org.koin.dsl.module
import retrofit2.Retrofit

val ApiModule = module {
single<NoticeService>(createdAtStart = false) { get<Retrofit>().create(NoticeService::class.java) }
single<UserService>(createdAtStart = false) { get<Retrofit>().create(UserService::class.java) }
}
11 changes: 11 additions & 0 deletions app/src/main/java/com/mashup/di/ApplicationModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.mashup.di

import com.mashup.repository.DefaultNoticesRepository
import com.mashup.repository.NoticesRepository
import com.mashup.repository.source.remote.RepositoriesRemoteDataSource
import org.koin.dsl.module

val ApplicationModule = module {
single { RepositoriesRemoteDataSource(get()) }
single { DefaultNoticesRepository(get()) as NoticesRepository }
}
44 changes: 44 additions & 0 deletions app/src/main/java/com/mashup/di/NetworkModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.mashup.di

import com.google.gson.GsonBuilder
import com.mashup.BuildConfig
import okhttp3.Cache
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import org.koin.android.ext.koin.androidApplication
import org.koin.dsl.module
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

private const val BASE_URL = BuildConfig.BASE_URL
private const val TIMEOUT_CONNECT = 15L

val NetworkModule = module {
single { Cache(androidApplication().cacheDir, 10L * 1024 * 1024) }

single {
OkHttpClient.Builder().apply {
cache(get())
connectTimeout(TIMEOUT_CONNECT, TimeUnit.SECONDS)
retryOnConnectionFailure(true)
addInterceptor(HttpLoggingInterceptor().apply {
if (BuildConfig.DEBUG) {
level = HttpLoggingInterceptor.Level.BODY
}
})
}.build()
}

single { GsonBuilder().create() }

single {
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(get()))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(get())
.build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.mashup.repository

import com.mashup.model.Notice
import com.mashup.repository.source.remote.RepositoriesRemoteDataSource
import io.reactivex.Flowable

class DefaultNoticesRepository(
private val noticesRemoteDataSource: RepositoriesRemoteDataSource
) : NoticesRepository {
private var cachedNotices = emptyList<Notice>()
private var mCacheIsDirty = false

override fun getNoticeList(): Flowable<List<Notice>> {

if (cachedNotices.isNotEmpty() && !mCacheIsDirty) {
return Flowable.fromArray(cachedNotices)
}

return noticesRemoteDataSource
.getNoticeList()
.flatMap { notices ->
Flowable.fromArray(notices).doOnNext {
cachedNotices = notices
}
}
.doOnComplete({ mCacheIsDirty = false })
}

}
9 changes: 9 additions & 0 deletions app/src/main/java/com/mashup/repository/NoticesRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.mashup.repository

import com.mashup.model.Notice
import io.reactivex.Flowable

interface NoticesRepository {

fun getNoticeList(): Flowable<List<Notice>>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.mashup.repository.source.remote

import com.mashup.api.notice.NoticeService
import com.mashup.model.Notice
import io.reactivex.Flowable
import io.reactivex.schedulers.Schedulers

class RepositoriesRemoteDataSource internal constructor(
private val noticeService: NoticeService
) {
fun getNoticeList(): Flowable<List<Notice>> =
noticeService.getNoticeList().subscribeOn(Schedulers.io())
}

0 comments on commit 636bf2d

Please sign in to comment.