Skip to content

Instantly share code, notes, and snippets.

@murki
Last active November 29, 2024 09:31
Show Gist options
  • Save murki/7c99e2d011c663deb25267e5462cc02d to your computer and use it in GitHub Desktop.
Save murki/7c99e2d011c663deb25267e5462cc02d to your computer and use it in GitHub Desktop.

Revisions

  1. murki revised this gist Jun 1, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion PlacesActivity.kt
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,6 @@ class PlacesActivity : AppCompatActivity() {
    super.onCreate(savedInstanceState)
    // This is how you instantiate your Presenter while the service locator resolves all of its dependencies
    // Note that the explicit type argument <PlacesPresenter> is not even necessary since Kotlin can infer the type
    placesPresenter = placesPresenter = application.getSystemService<PlacesPresenter>()
    placesPresenter = application.getSystemService<PlacesPresenter>()
    }
    }
  2. murki revised this gist Jun 1, 2018. No changes.
  3. murki revised this gist May 16, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions PlacesActivity.kt
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,7 @@ class PlacesActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    // This is how you instantiate your Presenter while the service locator resolves all of its dependencies
    // Note that the explicit type argument <PlacesPresenter> is not even necessary since Kotlin can infer the type
    placesPresenter = placesPresenter = application.getSystemService<PlacesPresenter>()
    }
    }
  4. murki revised this gist May 16, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions ServiceLocatorApplication.kt
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,7 @@ class ServiceLocatorApplication : Application() {
    override fun getSystemService(name: String?): Any {
    // All this logic can be extracted into a separate "Module" class
    return when (name) {
    // We use the full class' name as the key
    PlacesPresenter::class.java.name -> PlacesPresenter(getSystemService<ILocationRepository>(), getSystemService<IPlacesRepository>())
    ILocationRepository::class.java.name -> GmsLocationRepository(getSystemService<FusedLocationProviderClient>())
    IPlacesRepository::class.java.name -> GmsPlacesRepository(getSystemService<GeoDataClient>())
    @@ -20,5 +21,6 @@ class ServiceLocatorApplication : Application() {
    * Generic strongly-typed extension method for injection
    */
    inline fun <reified T : Any> Application.getSystemService() : T {
    // We extract the class name from the generic type
    return this.getSystemService(T::class.java.name) as T
    }
  5. murki revised this gist May 16, 2018. 2 changed files with 6 additions and 6 deletions.
    2 changes: 1 addition & 1 deletion PlacesActivity.kt
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ class PlacesActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    // This is how you locate your Presenter
    // This is how you instantiate your Presenter while the service locator resolves all of its dependencies
    placesPresenter = placesPresenter = application.getSystemService<PlacesPresenter>()
    }
    }
    10 changes: 5 additions & 5 deletions ServiceLocatorApplication.kt
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,16 @@
    class CityGuideApp : Application() {
    class ServiceLocatorApplication : Application() {

    /**
    * Dependency injection using service locator pattern
    */
    override fun getSystemService(name: String?): Any {
    // All this logic can be extracted into its own "module"
    // All this logic can be extracted into a separate "Module" class
    return when (name) {
    FusedLocationProviderClient::class.java.name -> LocationServices.getFusedLocationProviderClient(this)
    GeoDataClient::class.java.name -> Places.getGeoDataClient(this, null)
    PlacesPresenter::class.java.name -> PlacesPresenter(getSystemService<ILocationRepository>(), getSystemService<IPlacesRepository>())
    ILocationRepository::class.java.name -> GmsLocationRepository(getSystemService<FusedLocationProviderClient>())
    IPlacesRepository::class.java.name -> GmsPlacesRepository(getSystemService<GeoDataClient>())
    PlacesPresenter::class.java.name -> PlacesPresenter(getSystemService<ILocationRepository>(), getSystemService<IPlacesRepository>())
    FusedLocationProviderClient::class.java.name -> LocationServices.getFusedLocationProviderClient(this)
    GeoDataClient::class.java.name -> Places.getGeoDataClient(this, null)
    else -> super.getSystemService(name)
    }
    }
  6. murki created this gist May 16, 2018.
    9 changes: 9 additions & 0 deletions PlacesActivity.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    class PlacesActivity : AppCompatActivity() {
    private lateinit var placesPresenter: PlacesPresenter

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    // This is how you locate your Presenter
    placesPresenter = placesPresenter = application.getSystemService<PlacesPresenter>()
    }
    }
    24 changes: 24 additions & 0 deletions ServiceLocatorApplication.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    class CityGuideApp : Application() {

    /**
    * Dependency injection using service locator pattern
    */
    override fun getSystemService(name: String?): Any {
    // All this logic can be extracted into its own "module"
    return when (name) {
    FusedLocationProviderClient::class.java.name -> LocationServices.getFusedLocationProviderClient(this)
    GeoDataClient::class.java.name -> Places.getGeoDataClient(this, null)
    ILocationRepository::class.java.name -> GmsLocationRepository(getSystemService<FusedLocationProviderClient>())
    IPlacesRepository::class.java.name -> GmsPlacesRepository(getSystemService<GeoDataClient>())
    PlacesPresenter::class.java.name -> PlacesPresenter(getSystemService<ILocationRepository>(), getSystemService<IPlacesRepository>())
    else -> super.getSystemService(name)
    }
    }
    }

    /**
    * Generic strongly-typed extension method for injection
    */
    inline fun <reified T : Any> Application.getSystemService() : T {
    return this.getSystemService(T::class.java.name) as T
    }