-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #230 from 2gis/feature-SDK-#-locale-example
[SDK] Add locale example
- Loading branch information
Showing
7 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
app/src/main/java/ru/dgis/sdk/demo/LocaleSwitchActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package ru.dgis.sdk.demo | ||
|
||
import android.os.Bundle | ||
import android.view.View | ||
import android.widget.AdapterView | ||
import android.widget.ArrayAdapter | ||
import android.widget.LinearLayout | ||
import android.widget.Spinner | ||
import androidx.appcompat.app.AppCompatActivity | ||
import ru.dgis.sdk.Context | ||
import ru.dgis.sdk.coordinates.GeoPoint | ||
import ru.dgis.sdk.map.BearingSource | ||
import ru.dgis.sdk.map.CameraPosition | ||
import ru.dgis.sdk.map.DgisSource | ||
import ru.dgis.sdk.map.Map | ||
import ru.dgis.sdk.map.MapOptions | ||
import ru.dgis.sdk.map.MapView | ||
import ru.dgis.sdk.map.MyLocationControllerSettings | ||
import ru.dgis.sdk.map.MyLocationMapObjectSource | ||
import ru.dgis.sdk.map.Zoom | ||
import ru.dgis.sdk.platform.Locale | ||
import ru.dgis.sdk.platform.getLocaleManager | ||
|
||
/** | ||
* Activity that demonstrates how to dynamically switch map locales in a DGis-based application. | ||
* The locale changes are applied through a dropdown menu, and the map is recreated to reflect the | ||
* changes. | ||
*/ | ||
class LocaleSwitchActivity : AppCompatActivity() { | ||
private val sdkContext: Context by lazy { application.sdkContext } | ||
private lateinit var mapView: MapView | ||
private lateinit var mapContainer: LinearLayout | ||
private var map: Map? = null | ||
private lateinit var mapSource: MyLocationMapObjectSource | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_locale_switch) | ||
|
||
mapContainer = findViewById(R.id.map_container) | ||
initSpinner(findViewById(R.id.spinnerLocale), R.array.locales_list, this::onLocaleItemSelected) | ||
recreateMap() | ||
} | ||
|
||
/** | ||
* Handles selection changes in the locale dropdown. Updates the locale settings in the | ||
* DGis SDK and recreates the map to apply the changes. | ||
* | ||
* @param item The selected locale as a string. | ||
*/ | ||
private fun onLocaleItemSelected(item: String) { | ||
if (item == "System") { | ||
getLocaleManager(sdkContext).overrideLocales(emptyList()) | ||
} else { | ||
Locale.makeLocale(item)?.let { | ||
getLocaleManager(sdkContext).overrideLocales(listOf(it)) | ||
} | ||
} | ||
recreateMap() | ||
} | ||
|
||
/** | ||
* Recreates the map to reflect any changes in settings (e.g., locale). The current map view | ||
* is removed, and a new one is initialized with the updated options. | ||
*/ | ||
private fun recreateMap() { | ||
map?.let { lifecycle.removeObserver(mapView) } | ||
mapContainer.removeAllViews() | ||
|
||
val mapOptions = MapOptions().apply { | ||
position = CameraPosition( | ||
GeoPoint(40.37741938, 49.87862621), | ||
Zoom(9.0f) | ||
) | ||
sources = listOf(DgisSource.createDgisSource(sdkContext)) | ||
} | ||
|
||
mapView = MapView(this, mapOptions).also { | ||
it.getMapAsync { map -> | ||
this.map = map | ||
mapSource = MyLocationMapObjectSource( | ||
sdkContext, | ||
MyLocationControllerSettings(BearingSource.MAGNETIC) | ||
) | ||
map.addSource(mapSource) | ||
} | ||
mapContainer.addView(it) | ||
lifecycle.addObserver(it) | ||
} | ||
} | ||
|
||
/** | ||
* Initializes a dropdown spinner with a list of items and a callback for selection changes. | ||
* | ||
* @param onItemSelected Callback invoked when an item is selected. | ||
*/ | ||
private fun initSpinner(spinner: Spinner, itemsResource: Int, onItemSelected: (String) -> Unit) { | ||
ArrayAdapter.createFromResource(this, itemsResource, R.layout.spinner_item).also { adapter -> | ||
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) | ||
spinner.adapter = adapter | ||
} | ||
|
||
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { | ||
override fun onNothingSelected(parent: AdapterView<*>?) {} | ||
override fun onItemSelected(parent: AdapterView<*>?, view: View?, pos: Int, id: Long) { | ||
onItemSelected(parent?.getItemAtPosition(pos)?.toString() ?: "") | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<shape xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<solid android:color="@color/colorAccent"/> | ||
<corners android:radius="8dp"/> | ||
<padding android:left="4dp" android:right="4dp"/> | ||
</shape> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<RelativeLayout | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
<LinearLayout | ||
android:id="@+id/map_container" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:orientation="vertical" | ||
> | ||
</LinearLayout> | ||
|
||
<androidx.constraintlayout.widget.ConstraintLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:background="@android:color/transparent" | ||
android:fitsSystemWindows="true" | ||
tools:context=".LocaleSwitchActivity"> | ||
|
||
<androidx.constraintlayout.widget.Guideline | ||
android:id="@+id/guidelineFollowModeCenter" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:orientation="vertical" | ||
app:layout_constraintGuide_percent="0.45" /> | ||
|
||
<androidx.constraintlayout.widget.Guideline | ||
android:id="@+id/guidelineFollowModeSpinners" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:orientation="vertical" | ||
app:layout_constraintGuide_percent="0.66" /> | ||
|
||
<TextView | ||
android:id="@+id/textViewLocale" | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:layout_marginBottom="16dp" | ||
android:text="@string/locale" | ||
android:textColor="@color/text_foreground_day" | ||
app:layout_constraintBaseline_toBaselineOf="@id/spinnerLocale" | ||
app:layout_constraintStart_toEndOf="@id/guidelineFollowModeCenter" | ||
app:layout_constraintEnd_toStartOf="@id/guidelineFollowModeSpinners"/> | ||
|
||
<Spinner | ||
android:id="@+id/spinnerLocale" | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:textAlignment="center" | ||
android:layout_marginBottom="26dp" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="@id/guidelineFollowModeSpinners" | ||
/> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> | ||
|
||
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<TextView xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:textColor="@color/text_foreground_night" | ||
android:background="@drawable/spinner_item_background"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters