Skip to content

Commit

Permalink
add gestures example
Browse files Browse the repository at this point in the history
  • Loading branch information
Sameri11 committed Aug 2, 2023
1 parent ff70215 commit be55457
Show file tree
Hide file tree
Showing 8 changed files with 571 additions and 11 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ android {
compileSdk 34

defaultConfig {
applicationId "ru.dgis.sdk.demo"
applicationId "ru.mobile.sdk.app"
minSdkVersion 21
targetSdkVersion 34
versionCode 1
Expand Down Expand Up @@ -166,4 +166,5 @@ dependencies {
classifier = "sources"
}
}
implementation libs.kotlin.reflect
}
22 changes: 13 additions & 9 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Expand All @@ -15,11 +15,13 @@
android:roundIcon="@mipmap/icon_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".GesturesActivity"
android:exported="false" />
<activity
android:name=".SplashActivity"
android:exported="true"
android:theme="@style/SplashTheme"
>
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand All @@ -38,15 +40,17 @@
tools:ignore="LockedOrientationActivity" />
<activity
android:name=".MapStyleActivity"
android:theme="@style/MapStyleActivityTheme"
/>
android:theme="@style/MapStyleActivityTheme" />
<activity
android:name=".NavigationActivity"
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"
android:theme="@style/GenericActivityTheme" />
<activity android:name=".MapFpsActivity" android:theme="@style/GenericActivityTheme"/>
<activity android:name=".ParkingActivity"
android:theme="@style/AppTheme"/>
<activity
android:name=".MapFpsActivity"
android:theme="@style/GenericActivityTheme" />
<activity
android:name=".ParkingActivity"
android:theme="@style/AppTheme" />
</application>

</manifest>
161 changes: 161 additions & 0 deletions app/src/main/java/ru/dgis/sdk/demo/GesturesActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package ru.dgis.sdk.demo

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.widget.addTextChangedListener
import ru.dgis.sdk.demo.common.addSettingsLayout
import ru.dgis.sdk.demo.databinding.ActivityGesturesBinding
import ru.dgis.sdk.demo.databinding.ActivityGesturesSettingsBinding
import ru.dgis.sdk.map.Gesture
import kotlin.reflect.KProperty1
import kotlin.reflect.full.functions
import kotlin.reflect.full.instanceParameter

class GesturesActivity : AppCompatActivity() {
private val binding: ActivityGesturesBinding by lazy { ActivityGesturesBinding.inflate(layoutInflater) }
private val mapView by lazy { binding.mapView }
private val gestureManager by lazy { mapView.gestureManager }
private val settingsBinding by lazy { prepareSettingsBinding() }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
binding.addSettingsLayout().apply {
settingsDrawerInnerLayout.addView(settingsBinding.root)
}
mapView.getMapAsync {
initSwitches()
initRotationSettings()
initTiltSettings()
initScailingSettings()
initMultiTouchShiftSettings()
}
}

private fun prepareSettingsBinding(): ActivityGesturesSettingsBinding {
return ActivityGesturesSettingsBinding.inflate(layoutInflater)
}

private fun initSwitches() {
val gestureManager = this.gestureManager!!

mapOf(
settingsBinding.scailingSwitch to Gesture.SCALING,
settingsBinding.shiftSwitch to Gesture.SHIFT,
settingsBinding.multiTouchShiftSwitch to Gesture.MULTI_TOUCH_SHIFT,
settingsBinding.tiltSwitch to Gesture.TILT,
settingsBinding.rotationSwitch to Gesture.ROTATION
).forEach { (switchMaterial, gesture) ->
switchMaterial.isChecked = gestureManager.gestureEnabled(gesture)
switchMaterial.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
gestureManager.enableGesture(gesture)
} else {
gestureManager.disableGesture(gesture)
}
}
}
}

private fun initRotationSettings() {
val gestureManager = this.gestureManager!!
val rotationSettings = gestureManager.rotationSettings

mapOf(
settingsBinding.rotationAnglediffinscalingdeg to "angleDiffInScalingDeg",
settingsBinding.rotationDistancediffmm to "distanceDiffMm",
settingsBinding.rotationAnglediffdegEditText to "angleDiffDeg",
settingsBinding.rotationDistancediffinscalingmm to "distanceDiffInScalingMm"
).forEach { (editText, setting) ->
val propertyValue = readDataClassPropertyByName<Float>(rotationSettings, setting)
editText.apply {
setText(propertyValue.toString())
addTextChangedListener {
gestureManager.rotationSettings = copySettingsByName(
gestureManager.rotationSettings,
mapOf(setting to it.toString().toFloat())
)
}
}
}
}

private fun initTiltSettings() {
val gestureManager = this.gestureManager!!
val tiltSettings = gestureManager.tiltSettings

mapOf(
settingsBinding.tiltHorizontalswervedeg to "horizontalSwerveDeg",
settingsBinding.tiltLenondegreemm to "lenOnDegreeMm",
settingsBinding.tiltThresholdmm to "thresholdMm",
settingsBinding.tiltVerticalswervedeg to "verticalSwerveDeg"
).forEach { (editText, setting) ->
val propertyValue = readDataClassPropertyByName<Float>(tiltSettings, setting)
editText.apply {
setText(propertyValue.toString())
addTextChangedListener {
gestureManager.tiltSettings = copySettingsByName(
gestureManager.tiltSettings,
mapOf(setting to it.toString().toFloat())
)
}
}
}
}

private fun initScailingSettings() {
val gestureManager = this.gestureManager!!
val scalingSettings = gestureManager.scalingSettings

mapOf(
settingsBinding.scailingScaleratiothreshold to "scaleRatioThreshold",
settingsBinding.scailingScaleratiothresholdinrotation to "scaleRatioThresholdInRotation"
).forEach { (editText, setting) ->
val propertyValue = readDataClassPropertyByName<Float>(scalingSettings, setting)
editText.apply {
setText(propertyValue.toString())
addTextChangedListener {
gestureManager.scalingSettings = copySettingsByName(
gestureManager.scalingSettings,
mapOf(setting to it.toString().toFloat())
)
}
}
}
}
private fun initMultiTouchShiftSettings() {
val gestureManager = this.gestureManager!!
val multiTouchShiftSettings = gestureManager.multitouchShiftSettings

mapOf(
settingsBinding.multiTouchShiftThresholdmm to "thresholdMm"
).forEach { (editText, setting) ->
val propertyValue = readDataClassPropertyByName<Float>(multiTouchShiftSettings, setting)
editText.apply {
setText(propertyValue.toString())
addTextChangedListener {
gestureManager.multitouchShiftSettings = copySettingsByName(
gestureManager.multitouchShiftSettings,
mapOf(setting to it.toString().toFloat())
)
}
}
}
}

@Suppress("UNCHECKED_CAST")
private fun <T> readDataClassPropertyByName(instance: Any, propertyName: String): T {
val property = instance::class.members.first { it.name == propertyName } as KProperty1<Any, *>
return property.get(instance) as T
}

@Suppress("UNCHECKED_CAST")
private fun <T> copySettingsByName(instance: Any, newValues: Map<String, Any>): T {
val clazz = instance::class
val copyFunction = clazz.functions.first { it.name == "copy" }
val args = copyFunction.parameters
.filter { param -> newValues.keys.contains(param.name) }
.map { param -> param to newValues[param.name] }

return copyFunction.callBy(mapOf(copyFunction.instanceParameter!! to instance) + args) as? T ?: error("error")
}
}
4 changes: 4 additions & 0 deletions app/src/main/java/ru/dgis/sdk/demo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class MainActivity : AppCompatActivity() {
Page("Parkings on map") {
val intent = Intent(this@MainActivity, ParkingActivity::class.java)
startActivity(intent)
},
Page("Gestures") {
val intent = Intent(this@MainActivity, GesturesActivity::class.java)
startActivity(intent)
}
)

Expand Down
18 changes: 18 additions & 0 deletions app/src/main/res/layout/activity_gestures.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".GesturesActivity">

<ru.dgis.sdk.map.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:dgis_cameraTargetLat="25.09608"
app:dgis_cameraTargetLng="55.132429"
app:dgis_cameraTilt="25.0"
app:dgis_cameraZoom="13.5" />

</FrameLayout>
Loading

0 comments on commit be55457

Please sign in to comment.