Skip to content

Commit

Permalink
Replaced Dagger by Koin
Browse files Browse the repository at this point in the history
Signed-off-by: Rafael Chagas <[email protected]>
  • Loading branch information
rtchagas committed Apr 27, 2019
1 parent 24a7fd0 commit 1bb5b78
Show file tree
Hide file tree
Showing 16 changed files with 87 additions and 192 deletions.
7 changes: 3 additions & 4 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,9 @@ dependencies {
implementation 'com.google.android.gms:play-services-maps:16.1.0'
implementation 'com.google.android.libraries.places:places:1.1.0'

// Dagger2
def dagger2Version = '2.21'
implementation "com.google.dagger:dagger:${dagger2Version}"
kapt "com.google.dagger:dagger-compiler:${dagger2Version}"
// Koin for Android
implementation 'org.koin:koin-android:2.0.0-rc-2'
implementation 'org.koin:koin-android-viewmodel:2.0.0-rc-2'

// Rx
implementation 'io.reactivex.rxjava2:rxjava:2.2.4'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package com.rtchagas.pingplacepicker

import android.app.Activity
import android.app.Application
import android.content.Intent
import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.GoogleApiAvailability
import com.google.android.gms.common.GooglePlayServicesNotAvailableException
import com.google.android.libraries.places.api.model.Place
import com.rtchagas.pingplacepicker.inject.PingKoinContext
import com.rtchagas.pingplacepicker.inject.repositoryModule
import com.rtchagas.pingplacepicker.inject.viewModelModule
import com.rtchagas.pingplacepicker.ui.PlacePickerActivity
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.dsl.koinApplication

class PingPlacePicker private constructor() {

Expand All @@ -18,21 +25,23 @@ class PingPlacePicker private constructor() {
* This key will be used to all nearby requests to Google Places API.
*/
fun setAndroidApiKey(androidKey: String): IntentBuilder {
PingPlacePicker.androidApiKey = androidKey
androidApiKey = androidKey
return this
}

/**
* This key will be used to all reverse geolocation request to Google Maps API.
*/
fun setGeolocationApiKey(geoKey: String): IntentBuilder {
PingPlacePicker.geoLocationApiKey = geoKey
geoLocationApiKey = geoKey
return this
}

@Throws(GooglePlayServicesNotAvailableException::class)
fun build(activity: Activity): Intent {

initKoin(activity.application)

val result: Int =
GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(activity)

Expand All @@ -43,6 +52,21 @@ class PingPlacePicker private constructor() {
intent.setClass(activity, PlacePickerActivity::class.java)
return intent
}

/**
* Initializes the Dependency Injection framework by passing
* the current application context.
*/
private fun initKoin(application: Application) {
PingKoinContext.koinApp = koinApplication {
androidLogger()
androidContext(application)
modules(
repositoryModule,
viewModelModule
)
}
}
}

companion object {
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.rtchagas.pingplacepicker.inject

import org.koin.core.Koin
import org.koin.core.KoinApplication
import org.koin.core.KoinComponent

object PingKoinContext {

var koinApp: KoinApplication? = null

}

interface PingKoinComponent : KoinComponent {

override fun getKoin(): Koin = PingKoinContext.koinApp?.koin!!

}
Original file line number Diff line number Diff line change
@@ -1,42 +1,28 @@
package com.rtchagas.pingplacepicker.repository.inject
package com.rtchagas.pingplacepicker.inject

import android.content.Context
import com.google.android.libraries.places.api.Places
import com.google.android.libraries.places.api.net.PlacesClient
import com.rtchagas.pingplacepicker.PingPlacePicker
import com.rtchagas.pingplacepicker.repository.PlaceRepository
import com.rtchagas.pingplacepicker.repository.googlemaps.GoogleMapsAPI
import com.rtchagas.pingplacepicker.repository.googlemaps.GoogleMapsRepository
import dagger.Module
import dagger.Provides
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import org.koin.android.ext.koin.androidContext
import org.koin.dsl.module
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.moshi.MoshiConverterFactory
import javax.inject.Singleton

val repositoryModule = module {

@Module
class RepositoryModule {

@Singleton
@Provides
fun providePlaceRepository(placesClient: PlacesClient, googleMapsAPI: GoogleMapsAPI)
: PlaceRepository {
return GoogleMapsRepository(placesClient, googleMapsAPI)
}

@Singleton
@Provides
fun providesGooglePlacesClient(context: Context): PlacesClient {
Places.initialize(context, PingPlacePicker.androidApiKey)
return Places.createClient(context)
// PlacesClient
single {
Places.initialize(androidContext(), PingPlacePicker.androidApiKey)
return@single Places.createClient(androidContext())
}

@Singleton
@Provides
fun provideGoogleMapsAPI(): GoogleMapsAPI {
// GoogleMapsAPI
single(createdAtStart = true) {

val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.NONE
Expand All @@ -50,6 +36,9 @@ class RepositoryModule {
.client(client)
.build()

return retrofit.create(GoogleMapsAPI::class.java)
return@single retrofit.create(GoogleMapsAPI::class.java)
}

// GoogleMapsRepository
single { GoogleMapsRepository(googleClient = get(), googleMapsAPI = get()) as PlaceRepository }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.rtchagas.pingplacepicker.inject

import com.rtchagas.pingplacepicker.viewmodel.PlaceConfirmDialogViewModel
import com.rtchagas.pingplacepicker.viewmodel.PlacePickerViewModel
import org.koin.android.viewmodel.dsl.viewModel
import org.koin.dsl.module

val viewModelModule = module {

viewModel { PlacePickerViewModel(get()) }

viewModel { PlaceConfirmDialogViewModel(get()) }

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ import com.rtchagas.pingplacepicker.PingPlacePicker
import com.rtchagas.pingplacepicker.model.GeocodeResult
import com.rtchagas.pingplacepicker.repository.PlaceRepository
import io.reactivex.Single
import javax.inject.Inject


class GoogleMapsRepository @Inject constructor(
class GoogleMapsRepository constructor(
private val googleClient: PlacesClient,
private val googleMapsAPI: GoogleMapsAPI)
: PlaceRepository {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,20 @@ import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatDialogFragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.transition.TransitionManager
import com.google.android.libraries.places.api.model.Place
import com.rtchagas.pingplacepicker.Config
import com.rtchagas.pingplacepicker.PingPlacePicker
import com.rtchagas.pingplacepicker.R
import com.rtchagas.pingplacepicker.inject.DaggerInjector
import com.rtchagas.pingplacepicker.inject.PingKoinComponent
import com.rtchagas.pingplacepicker.viewmodel.PlaceConfirmDialogViewModel
import com.rtchagas.pingplacepicker.viewmodel.Resource
import com.rtchagas.pingplacepicker.viewmodel.inject.PingViewModelFactory
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.fragment_dialog_place_confirm.view.*
import javax.inject.Inject
import org.koin.android.viewmodel.ext.android.viewModel


class PlaceConfirmDialogFragment : AppCompatDialogFragment() {
class PlaceConfirmDialogFragment : AppCompatDialogFragment(), PingKoinComponent {

companion object {

Expand All @@ -47,23 +45,13 @@ class PlaceConfirmDialogFragment : AppCompatDialogFragment() {

var confirmListener: OnPlaceConfirmedListener? = null

@Inject
lateinit var viewModelFactory: PingViewModelFactory

private lateinit var viewModel: PlaceConfirmDialogViewModel
private val viewModel: PlaceConfirmDialogViewModel by viewModel()

private lateinit var place: Place

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

// Inject dependencies
DaggerInjector.getInjector(activity!!.application).inject(this)

// Initialize the ViewModel for this fragment
viewModel = ViewModelProviders.of(this, viewModelFactory)
.get<PlaceConfirmDialogViewModel>(PlaceConfirmDialogViewModel::class.java)

// Check mandatory parameters for this fragment
if ((arguments == null) || (arguments?.getParcelable<Place>(ARG_PLACE) == null)) {
throw IllegalArgumentException("You must pass a Place as argument to this fragment")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import androidx.core.content.res.ResourcesCompat
import androidx.core.graphics.drawable.DrawableCompat
import androidx.core.view.doOnLayout
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationServices
Expand All @@ -37,16 +36,16 @@ import com.karumi.dexter.listener.single.BasePermissionListener
import com.rtchagas.pingplacepicker.PingPlacePicker
import com.rtchagas.pingplacepicker.R
import com.rtchagas.pingplacepicker.helper.PermissionsHelper
import com.rtchagas.pingplacepicker.inject.DaggerInjector
import com.rtchagas.pingplacepicker.inject.PingKoinComponent
import com.rtchagas.pingplacepicker.viewmodel.PlacePickerViewModel
import com.rtchagas.pingplacepicker.viewmodel.Resource
import com.rtchagas.pingplacepicker.viewmodel.inject.PingViewModelFactory
import kotlinx.android.synthetic.main.activity_place_picker.*
import org.jetbrains.anko.toast
import javax.inject.Inject
import org.koin.android.viewmodel.ext.android.viewModel


class PlacePickerActivity : AppCompatActivity(), OnMapReadyCallback,
class PlacePickerActivity : AppCompatActivity(), PingKoinComponent,
OnMapReadyCallback,
GoogleMap.OnMarkerClickListener,
PlaceConfirmDialogFragment.OnPlaceConfirmedListener {

Expand Down Expand Up @@ -77,28 +76,18 @@ class PlacePickerActivity : AppCompatActivity(), OnMapReadyCallback,

private var placeAdapter: PlacePickerAdapter? = null

private lateinit var fusedLocationProviderClient: FusedLocationProviderClient

private lateinit var viewModel: PlacePickerViewModel
private val viewModel: PlacePickerViewModel by viewModel()

@Inject
lateinit var viewModelFactory: PingViewModelFactory
private lateinit var fusedLocationProviderClient: FusedLocationProviderClient

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_place_picker)

// Inject dependencies
DaggerInjector.getInjector(application).inject(this)

// Configure the toolbar
setSupportActionBar(toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)

// Initialize the ViewModel for this activity
viewModel = ViewModelProviders.of(this, viewModelFactory)
.get<PlacePickerViewModel>(PlacePickerViewModel::class.java)

// Retrieve location and camera position from saved instance state.
lastKnownLocation = savedInstanceState
?.getParcelable(STATE_LOCATION) ?: lastKnownLocation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import com.rtchagas.pingplacepicker.repository.PlaceRepository
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import javax.inject.Inject

class PlaceConfirmDialogViewModel @Inject constructor(private var repository: PlaceRepository)
class PlaceConfirmDialogViewModel constructor(private var repository: PlaceRepository)
: BaseViewModel() {

private val placePhotoLiveData: MutableLiveData<Resource<Bitmap>> = MutableLiveData()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import com.rtchagas.pingplacepicker.repository.PlaceRepository
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import javax.inject.Inject

class PlacePickerViewModel @Inject constructor(private var repository: PlaceRepository)
class PlacePickerViewModel constructor(private var repository: PlaceRepository)
: BaseViewModel() {

// Keep the place list in this view model state
Expand Down
Loading

0 comments on commit 1bb5b78

Please sign in to comment.