Skip to content

Commit

Permalink
Update Koin to release 2.2.2
Browse files Browse the repository at this point in the history
Signed-off-by: Rafael Chagas <[email protected]>
  • Loading branch information
rtchagas committed Jan 2, 2021
1 parent e9be060 commit c5f8592
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 53 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.72'
ext.kotlin_version = '1.4.21'

repositories {
google()
Expand Down
7 changes: 4 additions & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ android {

kotlinOptions {
jvmTarget = "1.8"
freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"
}

defaultConfig {
Expand Down Expand Up @@ -50,7 +51,7 @@ dependencies {
// Support library
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'com.google.android.material:material:1.3.0-alpha03'
implementation 'com.google.android.material:material:1.3.0-beta01'

// Android architecture components
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
Expand All @@ -63,8 +64,8 @@ dependencies {
implementation 'com.google.android.libraries.places:places:2.4.0'

// Koin for Android
implementation 'org.koin:koin-android:2.1.5'
implementation 'org.koin:koin-android-viewmodel:2.1.5'
implementation 'org.koin:koin-android:2.2.2'
implementation 'org.koin:koin-android-viewmodel:2.2.2'

// Rx
implementation 'io.reactivex.rxjava2:rxjava:2.2.10'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
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.gms.maps.model.LatLng
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 Down Expand Up @@ -64,17 +58,20 @@ class PingPlacePicker private constructor() {
* Set whether the library should return the place coordinate retrieved from GooglePlace or the actual selected location from google map
*/
fun setShouldReturnActualLatLng(shouldReturnActualLatLng: Boolean): IntentBuilder {
intent.putExtra(PlacePickerActivity.EXTRA_RETURN_ACTUAL_LATLNG, shouldReturnActualLatLng)
intent.putExtra(
PlacePickerActivity.EXTRA_RETURN_ACTUAL_LATLNG,
shouldReturnActualLatLng
)
return this
}

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

initKoin(activity.application)
PingKoinContext.init(activity.application)

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

if (ConnectionResult.SUCCESS != result) {
throw GooglePlayServicesNotAvailableException(result)
Expand All @@ -85,21 +82,6 @@ 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(listOf(
repositoryModule,
viewModelModule)
)
}
}
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
package com.rtchagas.pingplacepicker.inject

import android.content.Context
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.Koin
import org.koin.core.KoinApplication
import org.koin.core.KoinComponent
import org.koin.core.component.KoinApiExtension
import org.koin.core.component.KoinComponent
import org.koin.dsl.koinApplication

object PingKoinContext {

var koinApp: KoinApplication? = null
private lateinit var appContext: Context

val koin: Koin by lazy {
koinApplication {
androidLogger()
androidContext(appContext)
modules(listOf(repositoryModule, viewModelModule))
}.koin
}

/**
* Initializes the Dependency Injection framework by passing
* the current application context.
*/
@Synchronized
fun init(context: Context) {
appContext = context.applicationContext
}
}

@OptIn(KoinApiExtension::class)
interface PingKoinComponent : KoinComponent {

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

}
}
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.core.view.isVisible
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationServices
Expand All @@ -42,7 +41,6 @@ import com.rtchagas.pingplacepicker.PingPlacePicker
import com.rtchagas.pingplacepicker.R
import com.rtchagas.pingplacepicker.helper.PermissionsHelper
import com.rtchagas.pingplacepicker.inject.PingKoinComponent
import com.rtchagas.pingplacepicker.inject.PingKoinContext
import com.rtchagas.pingplacepicker.viewmodel.PlacePickerViewModel
import com.rtchagas.pingplacepicker.viewmodel.Resource
import io.reactivex.disposables.CompositeDisposable
Expand All @@ -51,7 +49,8 @@ import org.jetbrains.anko.toast
import org.koin.android.viewmodel.ext.android.viewModel
import kotlin.math.abs

class PlacePickerActivity : AppCompatActivity(), PingKoinComponent,
class PlacePickerActivity : AppCompatActivity(),
PingKoinComponent,
OnMapReadyCallback,
GoogleMap.OnMarkerClickListener,
PlaceConfirmDialogFragment.OnPlaceConfirmedListener {
Expand Down Expand Up @@ -101,13 +100,6 @@ class PlacePickerActivity : AppCompatActivity(), PingKoinComponent,
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_place_picker)

// Check if PING was killed for some reason.
// If so, should restart the activity and init everything again.
if (PingKoinContext.koinApp == null) {
finish()
return
}

// Configure the toolbar
setSupportActionBar(toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
Expand Down Expand Up @@ -204,10 +196,9 @@ class PlacePickerActivity : AppCompatActivity(), PingKoinComponent,
override fun onPlaceConfirmed(place: Place) {
val data = Intent()

if (intent.getBooleanExtra(EXTRA_RETURN_ACTUAL_LATLNG, false)){
if (intent.getBooleanExtra(EXTRA_RETURN_ACTUAL_LATLNG, false)) {
data.putExtra(PingPlacePicker.EXTRA_ACTUAL_LATLNG, selectedLatLng)
}
else {
} else {
data.putExtra(PingPlacePicker.EXTRA_ACTUAL_LATLNG, place.latLng)
}

Expand Down Expand Up @@ -255,9 +246,9 @@ class PlacePickerActivity : AppCompatActivity(), PingKoinComponent,
for (place in places) {
place.latLng?.let {
val marker: Marker = addMarker(
MarkerOptions()
.position(it)
.icon(getPlaceMarkerBitmap(place))
MarkerOptions()
.position(it)
.icon(getPlaceMarkerBitmap(place))
)

marker.tag = place
Expand Down Expand Up @@ -499,7 +490,7 @@ class PlacePickerActivity : AppCompatActivity(), PingKoinComponent,

private fun loadNearbyPlaces() {
viewModel.getNearbyPlaces(lastKnownLocation ?: defaultLocation)
.observe(this, Observer { handlePlacesLoaded(it) })
.observe(this, { handlePlacesLoaded(it) })
}

private fun moveCameraToSelectedPlace(place: Place) {
Expand All @@ -511,7 +502,7 @@ class PlacePickerActivity : AppCompatActivity(), PingKoinComponent,
private fun refreshNearbyPlaces() {
googleMap?.cameraPosition?.run {
viewModel.getNearbyPlaces(target)
.observe(this@PlacePickerActivity, Observer { handlePlacesLoaded(it) })
.observe(this@PlacePickerActivity, { handlePlacesLoaded(it) })
}
}

Expand Down Expand Up @@ -567,7 +558,7 @@ class PlacePickerActivity : AppCompatActivity(), PingKoinComponent,
googleMap?.cameraPosition?.run {
selectedLatLng = target
viewModel.getPlaceByLocation(target).observe(this@PlacePickerActivity,
Observer { handlePlaceByLocation(it) })
{ handlePlaceByLocation(it) })
}
}

Expand Down
2 changes: 1 addition & 1 deletion sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ dependencies {
// Support library
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'com.google.android.material:material:1.3.0-alpha03'
implementation 'com.google.android.material:material:1.3.0-beta01'

// Places library
implementation 'com.google.android.libraries.places:places:2.4.0'
Expand Down

0 comments on commit c5f8592

Please sign in to comment.