Skip to content

Commit 40f05dc

Browse files
committed
More refactoring and clean up
1 parent 6b93625 commit 40f05dc

File tree

7 files changed

+48
-54
lines changed

7 files changed

+48
-54
lines changed

android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/map/Map.kt

+12-22
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import androidx.compose.animation.core.keyframes
77
import androidx.compose.animation.core.tween
88
import androidx.compose.runtime.Composable
99
import androidx.compose.runtime.LaunchedEffect
10-
import androidx.compose.runtime.derivedStateOf
1110
import androidx.compose.runtime.remember
1211
import androidx.compose.ui.Modifier
1312
import kotlinx.coroutines.launch
@@ -17,7 +16,6 @@ import net.mullvad.mullvadvpn.compose.map.data.Longitude
1716
import net.mullvad.mullvadvpn.compose.map.data.MapViewState
1817
import net.mullvad.mullvadvpn.compose.map.data.Marker
1918
import net.mullvad.mullvadvpn.compose.map.data.MarkerType
20-
import net.mullvad.mullvadvpn.compose.map.internal.COMPLETE_ANGLE
2119
import net.mullvad.mullvadvpn.compose.map.internal.MapGLShader
2220
import net.mullvad.mullvadvpn.compose.util.rememberPrevious
2321

@@ -63,18 +61,6 @@ fun animatedMapViewState(
6361

6462
val longitudeAnimation = remember { Animatable(targetCameraLocation.longitude.value) }
6563

66-
// Correct the value to be within -180 to 180
67-
val longitudeAnimationCorrected =
68-
remember(longitudeAnimation) {
69-
derivedStateOf {
70-
val value = longitudeAnimation.value.mod(COMPLETE_ANGLE)
71-
if (value > COMPLETE_ANGLE / 2) {
72-
value - COMPLETE_ANGLE
73-
} else {
74-
value
75-
}
76-
}
77-
}
7864
val latitudeAnimation = remember { Animatable(targetCameraLocation.latitude.value) }
7965
val secureZoomAnimation = remember {
8066
Animatable(if (marker?.type == MarkerType.SECURE) SECURE_ZOOM else UNSECURE_ZOOM)
@@ -84,21 +70,22 @@ fun animatedMapViewState(
8470
LaunchedEffect(targetCameraLocation) {
8571
launch { latitudeAnimation.animateTo(targetCameraLocation.latitude.value, tween(duration)) }
8672
launch {
87-
// Unwind longitudeAnimation (prevent us from winding up angle to infinity)
73+
// Unwind longitudeAnimation into a Longitude
8874
val currentLongitude = Longitude.fromFloat(longitudeAnimation.value)
89-
val previousVelocity = longitudeAnimation.velocity
90-
longitudeAnimation.snapTo(currentLongitude.value)
9175

92-
// We resolve a vector showing us the shortest path to the target longitude, e.g going
93-
// from 10 to 350 would result in -20 since we can wrap around the globe
76+
// Resolve a vector showing us the shortest path to the target longitude, e.g going
77+
// from 170 to -170 would result in 20 since we can wrap around the globe
9478
val shortestPathVector = currentLongitude.vectorTo(targetCameraLocation.longitude)
9579

80+
// Animate to the new camera location using the shortest path vector
9681
longitudeAnimation.animateTo(
9782
longitudeAnimation.value + shortestPathVector.value,
9883
tween(duration),
99-
// Restore previous velocity before we unwound the longitude
100-
initialVelocity = previousVelocity,
10184
)
85+
86+
// Current value animation value might be outside of range of a Longitude, so when the
87+
// animation is done we unwind the animation to the correct value
88+
longitudeAnimation.snapTo(targetCameraLocation.longitude.value)
10289
}
10390
launch {
10491
zoomOutMultiplier.animateTo(
@@ -125,7 +112,10 @@ fun animatedMapViewState(
125112
return MapViewState(
126113
zoom = secureZoomAnimation.value * zoomOutMultiplier.value * 0.9f,
127114
cameraLatLng =
128-
LatLng(Latitude(latitudeAnimation.value), Longitude(longitudeAnimationCorrected.value)),
115+
LatLng(
116+
Latitude(latitudeAnimation.value),
117+
Longitude.fromFloat(longitudeAnimation.value)
118+
),
129119
locationMarker = marker,
130120
percent = percent
131121
)

android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/map/data/LatLng.kt

+10-5
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import net.mullvad.mullvadvpn.compose.map.internal.COMPLETE_ANGLE
77

88
data class LatLng(val latitude: Latitude, val longitude: Longitude) {
99

10-
fun distanceTo(other: LatLng): Double =
10+
fun distanceTo(other: LatLng): Float =
1111
sqrt(
12-
(latitude.distanceTo(other.latitude).absoluteValue).pow(2f) +
13-
((longitude.distanceTo(other.longitude).absoluteValue).pow(2f)
14-
).toDouble())
12+
latitude.distanceTo(other.latitude).pow(2f) +
13+
(longitude.distanceTo(other.longitude).pow(2f))
14+
)
1515

1616
operator fun plus(other: LatLng) =
1717
LatLng(latitude + other.latitude, longitude + other.longitude)
@@ -73,6 +73,12 @@ value class Longitude(val value: Float) {
7373
private const val MAX_LONGITUDE_VALUE: Float = COMPLETE_ANGLE / 2 // 180
7474
private val LONGITUDE_RANGE = MIN_LONGITUDE_VALUE..MAX_LONGITUDE_VALUE
7575

76+
/**
77+
* Create a [Longitude] from a float value.
78+
*
79+
* This function will unwind a float to a valid longitude value. E.g 190 will be unwound to
80+
* -170 and 360 will be unwound to 0.
81+
*/
7682
fun fromFloat(value: Float): Longitude {
7783
val unwoundValue = unwind(value)
7884
return Longitude(unwoundValue)
@@ -82,7 +88,6 @@ value class Longitude(val value: Float) {
8288
val unwound = value % COMPLETE_ANGLE
8389
return when {
8490
unwound > MAX_LONGITUDE_VALUE -> unwound - COMPLETE_ANGLE
85-
unwound < MIN_LONGITUDE_VALUE -> unwound + COMPLETE_ANGLE
8691
else -> unwound
8792
}
8893
}

android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/map/internal/GLHelper.kt

+16-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package net.mullvad.mullvadvpn.compose.map.internal
33
import android.opengl.GLES20
44
import android.opengl.Matrix
55
import android.util.Log
6+
import androidx.compose.ui.graphics.Color
67
import java.nio.Buffer
78
import java.nio.ByteBuffer
89
import java.nio.FloatBuffer
@@ -17,20 +18,20 @@ fun initShaderProgram(vsSource: String, fsSource: String): Int {
1718
val program = GLES20.glCreateProgram()
1819
check(program != 0) { "Could not create program" }
1920

20-
// add the vertex shader to program
21+
// Add the vertex shader to program
2122
GLES20.glAttachShader(program, vertexShader)
2223

23-
// add the fragment shader to program
24+
// Add the fragment shader to program
2425
GLES20.glAttachShader(program, fragmentShader)
2526

26-
// creates OpenGL ES program executables
27+
// Creates OpenGL ES program executables
2728
GLES20.glLinkProgram(program)
2829

2930
val linked = IntArray(1)
3031
GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linked, 0)
3132
if (linked[0] == GLES20.GL_FALSE) {
3233
val infoLog = GLES20.glGetProgramInfoLog(program)
33-
Log.e("mullvad", "Could not link program: $infoLog")
34+
Log.e("GLHelper", "Could not link program: $infoLog")
3435
GLES20.glDeleteProgram(program)
3536
error("Could not link program with vsSource: $vsSource and fsSource: $fsSource")
3637
}
@@ -39,21 +40,21 @@ fun initShaderProgram(vsSource: String, fsSource: String): Int {
3940
}
4041

4142
private fun loadShader(type: Int, shaderCode: String): Int {
42-
// create a vertex shader type (GLES20.GL_VERTEX_SHADER)
43+
// Create a vertex shader type (GLES20.GL_VERTEX_SHADER)
4344
// or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
4445
val shader = GLES20.glCreateShader(type)
4546

4647
require(shader != 0) { "Unable to create shader" }
4748

48-
// add the source code to the shader and compile it
49+
// Add the source code to the shader and compile it
4950
GLES20.glShaderSource(shader, shaderCode)
5051
GLES20.glCompileShader(shader)
5152

5253
val compiled = IntArray(1)
5354
GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0)
5455
if (compiled[0] == GLES20.GL_FALSE) {
5556
val infoLog = GLES20.glGetShaderInfoLog(shader)
56-
Log.e("mullvad", "Could not compile shader $type:$infoLog")
57+
Log.e("GLHelper", "Could not compile shader $type:$infoLog")
5758
GLES20.glDeleteShader(shader)
5859

5960
error("Could not compile shader with shaderCode: $shaderCode")
@@ -95,3 +96,11 @@ fun initIndexBuffer(dataBuffer: Buffer): IndexBufferWithLength {
9596
}
9697

9798
fun newIdentityMatrix(): FloatArray = FloatArray(MATRIX_SIZE).apply { Matrix.setIdentityM(this, 0) }
99+
100+
fun Color.toFloatArray(): FloatArray {
101+
return floatArrayOf(red, green, blue, alpha)
102+
}
103+
104+
fun Color.toFloatArrayWithoutAlpha(): FloatArray {
105+
return floatArrayOf(red, green, blue)
106+
}

android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/map/internal/MapGLRenderer.kt

+6-16
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import javax.microedition.khronos.opengles.GL10
1010
import kotlin.math.tan
1111
import net.mullvad.mullvadvpn.compose.map.data.MapViewState
1212
import net.mullvad.mullvadvpn.compose.map.data.MarkerType
13-
import net.mullvad.mullvadvpn.compose.map.shapes.Globe
14-
import net.mullvad.mullvadvpn.compose.map.shapes.LocationMarker
13+
import net.mullvad.mullvadvpn.compose.map.internal.shapes.Globe
14+
import net.mullvad.mullvadvpn.compose.map.internal.shapes.LocationMarker
1515

1616
class MapGLRenderer(private val resources: Resources, private val mapConfig: MapConfig) :
1717
GLSurfaceView.Renderer {
@@ -23,8 +23,6 @@ class MapGLRenderer(private val resources: Resources, private val mapConfig: Map
2323
private lateinit var viewState: MapViewState
2424

2525
override fun onSurfaceCreated(unused: GL10, config: EGLConfig) {
26-
// Set the background frame color
27-
// initialize a triangle
2826
globe = Globe(resources)
2927

3028
secureLocationMarker = LocationMarker(mapConfig.secureMarkerColor)
@@ -44,7 +42,7 @@ class MapGLRenderer(private val resources: Resources, private val mapConfig: Map
4442
private val projectionMatrix = newIdentityMatrix()
4543

4644
override fun onDrawFrame(gl10: GL10) {
47-
// Clear function
45+
// Clear canvas
4846
clear()
4947

5048
val viewMatrix = newIdentityMatrix()
@@ -58,16 +56,15 @@ class MapGLRenderer(private val resources: Resources, private val mapConfig: Map
5856
Matrix.rotateM(viewMatrix, 0, viewState.cameraLatLng.latitude.value, 1f, 0f, 0f)
5957
Matrix.rotateM(viewMatrix, 0, viewState.cameraLatLng.longitude.value, 0f, -1f, 0f)
6058

61-
val vP = projectionMatrix.copyOf()
6259

63-
globe.draw(vP.copyOf(), viewMatrix.copyOf(), mapConfig.globeColors)
60+
globe.draw(projectionMatrix, viewMatrix, mapConfig.globeColors)
6461

6562
viewState.locationMarker?.let {
6663
when (it.type) {
6764
MarkerType.SECURE ->
68-
secureLocationMarker.draw(vP, viewMatrix.copyOf(), it.latLng, 0.02f)
65+
secureLocationMarker.draw(projectionMatrix, viewMatrix, it.latLng, 0.02f)
6966
MarkerType.UNSECURE ->
70-
unsecureLocationMarker.draw(vP, viewMatrix.copyOf(), it.latLng, 0.02f)
67+
unsecureLocationMarker.draw(projectionMatrix, viewMatrix, it.latLng, 0.02f)
7168
}
7269
}
7370
}
@@ -109,10 +106,3 @@ class MapGLRenderer(private val resources: Resources, private val mapConfig: Map
109106
}
110107
}
111108

112-
fun Color.toFloatArray(): FloatArray {
113-
return floatArrayOf(red, green, blue, alpha)
114-
}
115-
116-
fun Color.toFloatArrayWithoutAlpha(): FloatArray {
117-
return floatArrayOf(red, green, blue)
118-
}

android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/map/internal/MapGLSurfaceView.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import android.content.Context
55
import android.opengl.GLSurfaceView
66
import androidx.compose.ui.graphics.Color
77
import net.mullvad.mullvadvpn.compose.map.data.MapViewState
8-
import net.mullvad.mullvadvpn.compose.map.shapes.GlobeColors
8+
import net.mullvad.mullvadvpn.compose.map.internal.shapes.GlobeColors
99

1010
@SuppressLint("ViewConstructor")
1111
class MapGLSurfaceView(context: Context, mapConfig: MapConfig) : GLSurfaceView(context) {

android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/map/shapes/Globe.kt android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/map/internal/shapes/Globe.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package net.mullvad.mullvadvpn.compose.map.shapes
1+
package net.mullvad.mullvadvpn.compose.map.internal.shapes
22

33
import android.content.res.Resources
44
import android.opengl.GLES20

android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/map/shapes/LocationMarker.kt android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/map/internal/shapes/LocationMarker.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package net.mullvad.mullvadvpn.compose.map.shapes
1+
package net.mullvad.mullvadvpn.compose.map.internal.shapes
22

33
import android.opengl.GLES20
44
import android.opengl.Matrix
@@ -118,7 +118,7 @@ class LocationMarker(val color: Color) {
118118
GLES20.glUseProgram(shaderProgram)
119119

120120
Matrix.rotateM(modelViewMatrix, 0, latLng.longitude.value, 0f, 1f, 0f)
121-
Matrix.rotateM(modelViewMatrix, 0, -latLng.latitude.value, 1f, 0f, 0f)
121+
Matrix.rotateM(modelViewMatrix, 0, latLng.latitude.value, -1f, 0f, 0f)
122122

123123
Matrix.scaleM(modelViewMatrix, 0, size, size, 1f)
124124

0 commit comments

Comments
 (0)