diff --git a/build.gradle b/build.gradle index 5b42df2..00a6635 100644 --- a/build.gradle +++ b/build.gradle @@ -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() diff --git a/library/build.gradle b/library/build.gradle index eed799d..018329c 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -14,6 +14,7 @@ android { kotlinOptions { jvmTarget = "1.8" + freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn" } defaultConfig { @@ -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' @@ -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' diff --git a/library/src/main/java/com/rtchagas/pingplacepicker/PingPlacePicker.kt b/library/src/main/java/com/rtchagas/pingplacepicker/PingPlacePicker.kt index f344b44..fa9e5bb 100644 --- a/library/src/main/java/com/rtchagas/pingplacepicker/PingPlacePicker.kt +++ b/library/src/main/java/com/rtchagas/pingplacepicker/PingPlacePicker.kt @@ -1,7 +1,6 @@ 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 @@ -9,12 +8,7 @@ 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() { @@ -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) @@ -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 { diff --git a/library/src/main/java/com/rtchagas/pingplacepicker/inject/PingKoinComponent.kt b/library/src/main/java/com/rtchagas/pingplacepicker/inject/PingKoinComponent.kt index c9545c0..667ff2d 100644 --- a/library/src/main/java/com/rtchagas/pingplacepicker/inject/PingKoinComponent.kt +++ b/library/src/main/java/com/rtchagas/pingplacepicker/inject/PingKoinComponent.kt @@ -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 -} \ No newline at end of file +} diff --git a/library/src/main/java/com/rtchagas/pingplacepicker/ui/PlacePickerActivity.kt b/library/src/main/java/com/rtchagas/pingplacepicker/ui/PlacePickerActivity.kt index 5006743..d9c58be 100644 --- a/library/src/main/java/com/rtchagas/pingplacepicker/ui/PlacePickerActivity.kt +++ b/library/src/main/java/com/rtchagas/pingplacepicker/ui/PlacePickerActivity.kt @@ -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 @@ -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 @@ -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 { @@ -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) @@ -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) } @@ -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 @@ -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) { @@ -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) }) } } @@ -567,7 +558,7 @@ class PlacePickerActivity : AppCompatActivity(), PingKoinComponent, googleMap?.cameraPosition?.run { selectedLatLng = target viewModel.getPlaceByLocation(target).observe(this@PlacePickerActivity, - Observer { handlePlaceByLocation(it) }) + { handlePlaceByLocation(it) }) } } diff --git a/sample/build.gradle b/sample/build.gradle index 62dcece..cd8e66a 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -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'