Permalink
Browse files

Replaced injector framework with constructors and factories!

  • Loading branch information...
lalunamel committed Jan 19, 2018
1 parent 9590d90 commit 3f19d5160bc5c416ec36e2f96c3c57d221e9b251
View
@@ -32,10 +32,6 @@ dependencies {
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
compile 'com.github.salomonbrys.kodein:kodein:4.1.0' // Dependency injection framework
compile 'com.github.salomonbrys.kodein:kodein-android:4.1.0'
compile 'com.github.salomonbrys.kodein:kodein-conf:4.1.0'
compile 'io.reactivex.rxjava2:rxkotlin:2.2.0' // Rx for kotlin
compile 'io.reactivex:rxandroid:0.23.0' // Rx for Android

This file was deleted.

Oops, something went wrong.
@@ -1,15 +1,6 @@
package net.codysehl.www.reader
import android.app.Application
import com.github.salomonbrys.kodein.*
import com.github.salomonbrys.kodein.conf.global
import net.codysehl.www.reader.ReduxLike.ApplicationState
import net.codysehl.www.reader.ReduxLike.Reducer
import net.codysehl.www.reader.ReduxLike.Store
class ReaderApplication: Application() {
override fun onCreate() {
super.onCreate()
Kodein.global.addImport(KodeinModule())
}
}
@@ -1,22 +1,14 @@
package net.codysehl.www.reader.ReduxLike
import android.util.Log
import com.github.salomonbrys.kodein.Kodein
import com.github.salomonbrys.kodein.KodeinAware
import com.github.salomonbrys.kodein.conf.ConfigurableKodein
import com.github.salomonbrys.kodein.conf.KodeinGlobalAware
import com.github.salomonbrys.kodein.conf.global
import com.github.salomonbrys.kodein.instance
import com.github.salomonbrys.kodein.with
import io.reactivex.rxkotlin.subscribeBy
import net.codysehl.www.reader.Model.Book
import net.codysehl.www.reader.Repository.BookSearchRepository
import java.util.logging.Logger
class ActionCreator(override val kodein: ConfigurableKodein) : KodeinAware {
private val store: Store<ApplicationState> = instance()
private val bookSearchRepo: BookSearchRepository = with(kodein).instance()
import net.codysehl.www.reader.Repository.SomeKindOfBookSearchRepository
class ActionCreator(
private val store: Store<ApplicationState>,
private val bookSearchRepo: BookSearchRepository
) {
fun searchTermChanged(text: String) = store.dispatch(Action.SearchTermChanged(text))
fun searchSubmitted() {
val term = store.state.searchText
@@ -30,4 +22,11 @@ class ActionCreator(override val kodein: ConfigurableKodein) : KodeinAware {
store.dispatch(Action.SearchCompletedWithSuccess(books))
})
}
companion object {
fun create(): ActionCreator = ActionCreator(
Store.singleton,
SomeKindOfBookSearchRepository.create()
)
}
}
@@ -2,7 +2,6 @@ package net.codysehl.www.reader.ReduxLike
import io.reactivex.Observable
import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject
class Store<T>(val reducer: (T, Action) -> T, var state: T) {
val observable: Observable<T>
@@ -13,4 +12,10 @@ class Store<T>(val reducer: (T, Action) -> T, var state: T) {
state = reducer(state, action)
publishableObservable.onNext(state)
}
companion object {
val singleton: Store<ApplicationState> by lazy {
Store(Reducer::reduce, ApplicationState())
}
}
}
@@ -2,7 +2,6 @@ package net.codysehl.www.reader.Repository
import io.reactivex.Observable
import net.codysehl.www.reader.Model.Book
import net.codysehl.www.reader.Promise.Deferred
interface BookSearchRepository {
fun search(term: String): Observable<List<Book>>
@@ -1,20 +1,16 @@
package net.codysehl.www.reader.Repository
import android.util.Log
import com.github.salomonbrys.kodein.KodeinAware
import com.github.salomonbrys.kodein.conf.ConfigurableKodein
import com.github.salomonbrys.kodein.instance
import com.github.salomonbrys.kodein.with
import io.reactivex.Observable
import io.reactivex.schedulers.Schedulers
import net.codysehl.www.reader.Model.Book
import net.codysehl.www.reader.Promise.Deferred
import java.util.*
import kotlin.concurrent.schedule
class SomeKindOfBookSearchRepository(override val kodein: ConfigurableKodein) : BookSearchRepository, KodeinAware {
val googleBookSearchService: GoogleBooksService = instance()
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
class SomeKindOfBookSearchRepository(private val googleBookSearchService: GoogleBooksService) : BookSearchRepository {
override fun search(term: String): Observable<List<Book>> {
Log.e("Lifecycle", "Searching for term: $term")
@@ -26,4 +22,22 @@ class SomeKindOfBookSearchRepository(override val kodein: ConfigurableKodein) :
}
.subscribeOn(Schedulers.io())
}
companion object {
fun create(): BookSearchRepository {
val logging = HttpLoggingInterceptor()
logging.level = HttpLoggingInterceptor.Level.BODY
val httpClientBuilder = OkHttpClient.Builder()
httpClientBuilder.interceptors().add(logging)
val retrofit: Retrofit = Retrofit.Builder()
.baseUrl("https://www.googleapis.com/books/v1/")
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(httpClientBuilder.build())
.build()
val googleBooksService: GoogleBooksService = retrofit.create(GoogleBooksService::class.java)
return SomeKindOfBookSearchRepository(googleBooksService)
}
}
}
@@ -0,0 +1,8 @@
package net.codysehl.www.reader
import io.reactivex.Scheduler
import io.reactivex.android.schedulers.AndroidSchedulers
object SchedulerFactory {
fun createMainThreadScheduler(): Scheduler = AndroidSchedulers.mainThread()
}
@@ -3,8 +3,6 @@ package net.codysehl.www.reader.Search
import android.app.Activity
import android.os.Bundle
import android.util.Log
import com.github.salomonbrys.kodein.*
import com.github.salomonbrys.kodein.conf.*
import com.jakewharton.rxbinding2.view.RxView
import com.jakewharton.rxbinding2.widget.RxTextView
import io.reactivex.Observable
@@ -13,14 +11,13 @@ import net.codysehl.www.reader.Search.View.SearchView
class SearchActivity : Activity(), SearchPresenter.View {
override lateinit var searchTermChanged: Observable<String>
override lateinit var searchTermSubmitted: Observable<Any>
private val injector = KodeinInjector()
private lateinit var view: SearchView
private val presenter: SearchPresenter by injector.with(Kodein.global).instance()
private lateinit var presenter: SearchPresenter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
injector.inject(Kodein.global)
presenter = SearchPresenter.create()
view = SearchView(this)
setContentView(view)
@@ -1,29 +1,25 @@
package net.codysehl.www.reader.Search
import com.github.salomonbrys.kodein.KodeinAware
import com.github.salomonbrys.kodein.conf.ConfigurableKodein
import com.github.salomonbrys.kodein.instance
import com.github.salomonbrys.kodein.with
import io.reactivex.Observable
import io.reactivex.Scheduler
import io.reactivex.disposables.Disposable
import net.codysehl.www.reader.ReduxLike.ApplicationState
import net.codysehl.www.reader.ReduxLike.ActionCreator
import net.codysehl.www.reader.KodeinTag
import net.codysehl.www.reader.Model.Book
import net.codysehl.www.reader.Search.SearchPresenter.Props.Book.Companion.fromModel
import net.codysehl.www.reader.ReduxLike.ApplicationState
import net.codysehl.www.reader.ReduxLike.Store
import net.codysehl.www.reader.SchedulerFactory
class SearchPresenter(override val kodein: ConfigurableKodein) : KodeinAware {
class SearchPresenter(
private val applicationState: Observable<ApplicationState>,
private val actionCreator: ActionCreator,
private val scheduler: Scheduler
) {
private val applicationState: Observable<ApplicationState> = instance()
private val actionCreator: ActionCreator = with(kodein).instance()
private val mainThreadScheduler: Scheduler = instance(KodeinTag.MAIN_THREAD)
private var disposables: List<Disposable> = listOf()
fun onViewReady(view: View) {
disposables = listOf(
applicationState
.observeOn(mainThreadScheduler)
.observeOn(scheduler)
.map { Props.fromState(it) }
.doOnNext { view.render(it) }
.subscribe(),
@@ -82,4 +78,14 @@ class SearchPresenter(override val kodein: ConfigurableKodein) : KodeinAware {
}
}
}
companion object {
fun create(): SearchPresenter {
return SearchPresenter(
Store.singleton.observable,
ActionCreator.create(),
SchedulerFactory.createMainThreadScheduler()
)
}
}
}
@@ -1,9 +1,6 @@
package net.codysehl.www.reader.Search.View
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
@@ -1,17 +1,9 @@
package net.codysehl.www.reader.ReduxLike
import com.github.salomonbrys.kodein.Kodein
import com.github.salomonbrys.kodein.bind
import com.github.salomonbrys.kodein.conf.ConfigurableKodein
import com.github.salomonbrys.kodein.conf.global
import com.github.salomonbrys.kodein.factory
import com.github.salomonbrys.kodein.singleton
import com.nhaarman.mockito_kotlin.*
import io.reactivex.subjects.PublishSubject
import junit.framework.Assert.assertEquals
import junit.framework.Assert.assertTrue
import net.codysehl.www.reader.Model.Book
import net.codysehl.www.reader.Promise.Deferred
import net.codysehl.www.reader.Repository.BookSearchRepository
import org.junit.Before
import org.junit.Test
@@ -36,13 +28,7 @@ class ActionCreatorTest {
on { search(any()) } doReturn bookSearchObservable
}
val kodein = ConfigurableKodein()
kodein.addConfig {
bind<Store<ApplicationState>>() with singleton { store }
bind<BookSearchRepository>() with factory<ConfigurableKodein, BookSearchRepository> { _ -> bookSearchRepo }
}
subject = ActionCreator(kodein)
subject = ActionCreator(store, bookSearchRepo)
}
@Test

This file was deleted.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 3f19d51

Please sign in to comment.