Skip to content

Commit

Permalink
Refactor to new module
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawa committed Feb 9, 2024
1 parent 33a0610 commit 1189c36
Show file tree
Hide file tree
Showing 33 changed files with 344 additions and 142 deletions.
1 change: 1 addition & 0 deletions android/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ dependencies {
implementation(project(Dependencies.Mullvad.talpidLib))
implementation(project(Dependencies.Mullvad.themeLib))
implementation(project(Dependencies.Mullvad.paymentLib))
implementation(project(Dependencies.Mullvad.mapLib))

// Play implementation
playImplementation(project(Dependencies.Mullvad.billingLib))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,6 @@ import net.mullvad.mullvadvpn.compose.destinations.DeviceRevokedDestination
import net.mullvad.mullvadvpn.compose.destinations.OutOfTimeDestination
import net.mullvad.mullvadvpn.compose.destinations.SelectLocationDestination
import net.mullvad.mullvadvpn.compose.destinations.SettingsDestination
import net.mullvad.mullvadvpn.compose.map.Map
import net.mullvad.mullvadvpn.compose.map.data.LatLng
import net.mullvad.mullvadvpn.compose.map.data.Latitude
import net.mullvad.mullvadvpn.compose.map.data.Longitude
import net.mullvad.mullvadvpn.compose.map.data.Marker
import net.mullvad.mullvadvpn.compose.map.data.MarkerType
import net.mullvad.mullvadvpn.compose.map.data.gothenburgLatLng
import net.mullvad.mullvadvpn.compose.state.ConnectUiState
import net.mullvad.mullvadvpn.compose.test.CIRCULAR_PROGRESS_INDICATOR
import net.mullvad.mullvadvpn.compose.test.CONNECT_BUTTON_TEST_TAG
Expand All @@ -70,11 +63,15 @@ import net.mullvad.mullvadvpn.compose.test.SCROLLABLE_COLUMN_TEST_TAG
import net.mullvad.mullvadvpn.compose.test.SELECT_LOCATION_BUTTON_TEST_TAG
import net.mullvad.mullvadvpn.compose.transitions.HomeTransition
import net.mullvad.mullvadvpn.lib.common.util.openAccountPageInBrowser
import net.mullvad.mullvadvpn.lib.map.Map
import net.mullvad.mullvadvpn.lib.theme.AppTheme
import net.mullvad.mullvadvpn.lib.theme.Dimens
import net.mullvad.mullvadvpn.lib.theme.color.AlphaScrollbar
import net.mullvad.mullvadvpn.lib.theme.color.AlphaTopBar
import net.mullvad.mullvadvpn.model.GeoIpLocation
import net.mullvad.mullvadvpn.model.LatLng
import net.mullvad.mullvadvpn.model.Latitude
import net.mullvad.mullvadvpn.model.Longitude
import net.mullvad.mullvadvpn.model.TunnelState
import net.mullvad.mullvadvpn.util.appendHideNavOnPlayBuild
import net.mullvad.mullvadvpn.viewmodel.ConnectViewModel
Expand Down Expand Up @@ -326,15 +323,25 @@ fun ConnectScreen(
}
}

fun TunnelState.toMarker(location: GeoIpLocation?): Marker? {
fun TunnelState.toMarker(location: GeoIpLocation?): net.mullvad.mullvadvpn.lib.map.data.Marker? {
if (location == null) return null
return when (this) {
is TunnelState.Connected -> Marker(location.toLatLng(), MarkerType.SECURE)
is TunnelState.Connected ->
net.mullvad.mullvadvpn.lib.map.data.Marker(
location.toLatLng(),
net.mullvad.mullvadvpn.lib.map.data.MarkerType.SECURE
)
is TunnelState.Connecting -> null
is TunnelState.Disconnected -> Marker(location.toLatLng(), MarkerType.UNSECURE)
is TunnelState.Disconnected ->
net.mullvad.mullvadvpn.lib.map.data.Marker(
location.toLatLng(),
net.mullvad.mullvadvpn.lib.map.data.MarkerType.UNSECURE
)
is TunnelState.Disconnecting -> null
is TunnelState.Error -> null
}
}

fun GeoIpLocation.toLatLng() = LatLng(Latitude(latitude.toFloat()), Longitude(longitude.toFloat()))

val gothenburgLatLng = LatLng(Latitude(57.7065f), Longitude(11.967f))

This file was deleted.

1 change: 1 addition & 0 deletions android/buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ object Dependencies {
const val commonTestLib = ":lib:common-test"
const val billingLib = ":lib:billing"
const val paymentLib = ":lib:payment"
const val mapLib = ":lib:map"
}

object Plugin {
Expand Down
1 change: 1 addition & 0 deletions android/config/detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,7 @@ style:
excludeImportStatements: true
excludeCommentStatements: false
excludeRawStrings: true
ignoreAnnotated: ['Test']
MayBeConst:
active: true
ModifierOrder:
Expand Down
44 changes: 44 additions & 0 deletions android/lib/map/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
plugins {
id(Dependencies.Plugin.kotlinAndroidId)
id(Dependencies.Plugin.androidLibraryId)
}

android {
namespace = "net.mullvad.mullvadvpn.lib.map"
compileSdk = Versions.Android.compileSdkVersion

defaultConfig {
minSdk = Versions.Android.minSdkVersion
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

kotlinOptions {
jvmTarget = Versions.jvmTarget
}

buildFeatures { compose = true }

composeOptions { kotlinCompilerExtensionVersion = Versions.kotlinCompilerExtensionVersion }

lint {
lintConfig = file("${rootProject.projectDir}/config/lint.xml")
abortOnError = true
warningsAsErrors = true
}
}

dependencies {

//Model
implementation(project(Dependencies.Mullvad.modelLib))

implementation(Dependencies.Compose.ui)
implementation(Dependencies.Compose.foundation)

implementation(Dependencies.AndroidX.lifecycleRuntimeKtx)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package net.mullvad.mullvadvpn.lib.map

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("net.mullvad.mullvadvpn.lib.map.test", appContext.packageName)
}
}
4 changes: 4 additions & 0 deletions android/lib/map/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

</manifest>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.mullvad.mullvadvpn.compose.map
package net.mullvad.mullvadvpn.lib.map

import android.util.Log
import androidx.compose.animation.core.Animatable
Expand All @@ -10,14 +10,13 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import kotlinx.coroutines.launch
import net.mullvad.mullvadvpn.compose.map.data.LatLng
import net.mullvad.mullvadvpn.compose.map.data.Latitude
import net.mullvad.mullvadvpn.compose.map.data.Longitude
import net.mullvad.mullvadvpn.compose.map.data.MapViewState
import net.mullvad.mullvadvpn.compose.map.data.Marker
import net.mullvad.mullvadvpn.compose.map.data.MarkerType
import net.mullvad.mullvadvpn.compose.map.internal.MapGLShader
import net.mullvad.mullvadvpn.compose.util.rememberPrevious
import net.mullvad.mullvadvpn.lib.map.data.MapViewState
import net.mullvad.mullvadvpn.lib.map.data.Marker
import net.mullvad.mullvadvpn.lib.map.data.MarkerType
import net.mullvad.mullvadvpn.lib.map.internal.MapGLShader
import net.mullvad.mullvadvpn.model.LatLng
import net.mullvad.mullvadvpn.model.Latitude
import net.mullvad.mullvadvpn.model.Longitude

@Composable
fun Map(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package net.mullvad.mullvadvpn.lib.map

/*
* Code snippet taken from:
* https://stackoverflow.com/questions/67801939/get-previous-value-of-state-in-composable-jetpack-compose
*/

import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.remember

// TODO this file was copied for now and should be removed/broken out to a new module
@Composable
fun <T> rememberPrevious(
current: T,
shouldUpdate: (prev: T?, curr: T) -> Boolean = { a: T?, b: T -> a != b },
): T? {
val ref = rememberRef<T>()

// launched after render, so the current render will have the old value anyway
SideEffect {
if (shouldUpdate(ref.value, current)) {
ref.value = current
}
}

return ref.value
}

@Composable
private fun <T> rememberRef(): MutableState<T?> {
// for some reason it always recreated the value with vararg keys,
// leaving out the keys as a parameter for remember for now
return remember {
object : MutableState<T?> {
override var value: T? = null

override fun component1(): T? = value

override fun component2(): (T?) -> Unit = { value = it }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package net.mullvad.mullvadvpn.compose.map.data
package net.mullvad.mullvadvpn.lib.map.data

import net.mullvad.mullvadvpn.model.LatLng

class MapViewState(
val zoom: Float,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package net.mullvad.mullvadvpn.compose.map.data
package net.mullvad.mullvadvpn.lib.map.data

import net.mullvad.mullvadvpn.model.LatLng

data class Marker(val latLng: LatLng, val type: MarkerType, val size: Float = 0.02f)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package net.mullvad.mullvadvpn.compose.map.internal
package net.mullvad.mullvadvpn.lib.map.internal

const val VERTEX_COMPONENT_SIZE = 3
const val COLOR_COMPONENT_SIZE = 4

const val MATRIX_SIZE = 16

const val COMPLETE_ANGLE = 360f
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.mullvad.mullvadvpn.compose.map.internal
package net.mullvad.mullvadvpn.lib.map.internal

import android.opengl.GLES20
import android.opengl.Matrix
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package net.mullvad.mullvadvpn.compose.map.internal
package net.mullvad.mullvadvpn.lib.map.internal

data class IndexBufferWithLength(val indexBuffer: Int, val length: Int)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.mullvad.mullvadvpn.compose.map.internal
package net.mullvad.mullvadvpn.lib.map.internal

import android.content.res.Resources
import android.opengl.GLES20
Expand All @@ -8,10 +8,11 @@ import androidx.compose.ui.graphics.Color
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10
import kotlin.math.tan
import net.mullvad.mullvadvpn.compose.map.data.MapViewState
import net.mullvad.mullvadvpn.compose.map.data.MarkerType
import net.mullvad.mullvadvpn.compose.map.internal.shapes.Globe
import net.mullvad.mullvadvpn.compose.map.internal.shapes.LocationMarker
import net.mullvad.mullvadvpn.lib.map.data.MapViewState
import net.mullvad.mullvadvpn.lib.map.data.MarkerType
import net.mullvad.mullvadvpn.lib.map.internal.shapes.Globe
import net.mullvad.mullvadvpn.lib.map.internal.shapes.LocationMarker
import net.mullvad.mullvadvpn.model.COMPLETE_ANGLE

class MapGLRenderer(private val resources: Resources, private val mapConfig: MapConfig) :
GLSurfaceView.Renderer {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package net.mullvad.mullvadvpn.compose.map.internal
package net.mullvad.mullvadvpn.lib.map.internal

import android.annotation.SuppressLint
import android.content.Context
import android.opengl.GLSurfaceView
import androidx.compose.ui.graphics.Color
import net.mullvad.mullvadvpn.compose.map.data.MapViewState
import net.mullvad.mullvadvpn.compose.map.internal.shapes.GlobeColors
import net.mullvad.mullvadvpn.compose.map.internal.shapes.LocationMarkerColors
import net.mullvad.mullvadvpn.lib.map.data.MapViewState
import net.mullvad.mullvadvpn.lib.map.internal.shapes.GlobeColors
import net.mullvad.mullvadvpn.lib.map.internal.shapes.LocationMarkerColors

@SuppressLint("ViewConstructor")
class MapGLSurfaceView(context: Context, mapConfig: MapConfig) : GLSurfaceView(context) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.mullvad.mullvadvpn.compose.map.internal
package net.mullvad.mullvadvpn.lib.map.internal

import android.util.Log
import androidx.compose.runtime.Composable
Expand All @@ -9,7 +9,7 @@ import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.viewinterop.AndroidView
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import net.mullvad.mullvadvpn.compose.map.data.MapViewState
import net.mullvad.mullvadvpn.lib.map.data.MapViewState

@Composable
fun MapGLShader(modifier: Modifier = Modifier, mapViewState: MapViewState) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package net.mullvad.mullvadvpn.compose.map.internal.shapes
package net.mullvad.mullvadvpn.lib.map.internal.shapes

import android.content.res.Resources
import android.opengl.GLES20
import android.opengl.Matrix
import androidx.compose.ui.graphics.Color
import java.nio.ByteBuffer
import net.mullvad.mullvadvpn.compose.map.internal.IndexBufferWithLength
import net.mullvad.mullvadvpn.compose.map.internal.VERTEX_COMPONENT_SIZE
import net.mullvad.mullvadvpn.compose.map.internal.initArrayBuffer
import net.mullvad.mullvadvpn.compose.map.internal.initIndexBuffer
import net.mullvad.mullvadvpn.compose.map.internal.initShaderProgram
import net.mullvad.mullvadvpn.compose.map.internal.toFloatArray
import net.mullvad.mullvadvpn.R
import net.mullvad.mullvadvpn.lib.map.internal.IndexBufferWithLength
import net.mullvad.mullvadvpn.lib.map.internal.VERTEX_COMPONENT_SIZE
import net.mullvad.mullvadvpn.lib.map.internal.initArrayBuffer
import net.mullvad.mullvadvpn.lib.map.internal.initIndexBuffer
import net.mullvad.mullvadvpn.lib.map.internal.initShaderProgram
import net.mullvad.mullvadvpn.lib.map.internal.toFloatArray
import net.mullvad.mullvadvpn.lib.map.R

data class GlobeColors(
val landColor: Color,
Expand Down
Loading

0 comments on commit 1189c36

Please sign in to comment.