Skip to content

Commit

Permalink
Graphical overhaul
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanSmudja committed Nov 28, 2023
1 parent f8602f4 commit ed8c698
Show file tree
Hide file tree
Showing 13 changed files with 383 additions and 114 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ dependencies {
// Google maps for Compose
implementation("com.google.maps.android:maps-compose:4.3.0")

// Viewmodel
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.6.2")

//Gson
implementation("com.google.code.gson:gson:2.10.1")

Expand Down
42 changes: 6 additions & 36 deletions app/src/main/java/com/lilstiffy/mockgps/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,32 @@ import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.location.Location
import android.location.LocationListener
import android.os.Bundle
import android.os.IBinder
import android.util.Log
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.sharp.Close
import androidx.compose.material.icons.sharp.PlayArrow
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButtonDefaults
import androidx.compose.material3.IconToggleButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex
import com.google.android.gms.maps.model.LatLng
import com.google.maps.android.compose.GoogleMap
import com.google.maps.android.compose.MapUiSettings
import com.google.maps.android.compose.Marker
import com.google.maps.android.compose.MarkerState
import com.lilstiffy.mockgps.service.LocationHelper
import com.lilstiffy.mockgps.service.MockLocationService
import com.lilstiffy.mockgps.service.VibratorService
import com.lilstiffy.mockgps.storage.StorageManager
import com.lilstiffy.mockgps.ui.screens.MapScreen
import com.lilstiffy.mockgps.ui.theme.ButtonGreen
import com.lilstiffy.mockgps.ui.theme.ButtonRed
import com.lilstiffy.mockgps.ui.theme.MockGpsTheme

class MainActivity : ComponentActivity() {
var mockLocationService: MockLocationService? = null
private var mockLocationService: MockLocationService? = null
private set(value) {
field = value
MockLocationService.instance = value
}

private var isBound = false

private val locationListener =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.lilstiffy.mockgps.extensions

import android.location.Address

fun Address.displayString(): String {
val addressLines = mutableListOf<String>()

// Concatenate lines of the address
for (i in 0..maxAddressLineIndex) {
addressLines.add(getAddressLine(i) ?: "")
}

// Concatenate other details
val otherDetails = mutableListOf<String>().apply {
locality?.let { add(it) }
adminArea?.let { add(it) }
countryName?.let { add(it) }
postalCode?.let { add(it) }
}

// Combine address lines and other details
val result = (addressLines + otherDetails).joinToString(", ")

return result.ifEmpty { "No address available" }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.lilstiffy.mockgps.extensions

import com.google.android.gms.maps.model.LatLng

fun LatLng.prettyPrint(): String {
return "Latitude: ${this.latitude}\nLongitude: ${this.longitude}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.lilstiffy.mockgps.extensions

import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

fun Modifier.roundedShadow(radius: Dp): Modifier {
return this
.shadow(
elevation = 4.dp,
shape = RoundedCornerShape(radius),
clip = true
)
}
14 changes: 12 additions & 2 deletions app/src/main/java/com/lilstiffy/mockgps/service/LocationHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.lilstiffy.mockgps.service

import android.Manifest.permission.*
import android.content.pm.PackageManager
import android.location.Address
import android.location.Geocoder
import android.os.Build
import androidx.activity.ComponentActivity
Expand All @@ -28,10 +29,19 @@ object LocationHelper {
}

// Geocoding
fun reverseGeocoding(location: LatLng): String? {
fun reverseGeocoding(latLng: LatLng, result: (Address?) -> Unit) {
val geocoder: Geocoder = Geocoder(MockGpsApp.shared.applicationContext)

return null
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1) { response ->
val address = response.firstOrNull()
result(address)
}
} else {
val response = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1)
val address = response?.firstOrNull()
result(address)
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import kotlinx.coroutines.launch

class MockLocationService : Service() {

private companion object {
companion object {
const val TAG = "MockLocationService"
var instance: MockLocationService? = null
}

var isMocking = false
Expand Down Expand Up @@ -147,7 +148,7 @@ class MockLocationService : Service() {
longitude = latLng.longitude
altitude = 12.5
time = System.currentTimeMillis()
accuracy = 10f
accuracy = 2f
elapsedRealtimeNanos = SystemClock.elapsedRealtimeNanos()
}

Expand Down
38 changes: 36 additions & 2 deletions app/src/main/java/com/lilstiffy/mockgps/storage/StorageManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.lilstiffy.mockgps.service.LocationHelper
object StorageManager {
private const val PREF = "gpsprefs"
private const val KEY_HISTORY = "history"
private const val KEY_FAVORITES = "favorites"

private lateinit var pref: SharedPreferences

Expand All @@ -19,7 +20,7 @@ object StorageManager {
}

fun getLatestLocation(): LatLng {
return locationHistory.firstOrNull() ?: LocationHelper.DEFAULT_LOCATION
return locationHistory.lastOrNull() ?: LocationHelper.DEFAULT_LOCATION
}

fun addLocationToHistory(latLng: LatLng) {
Expand All @@ -30,10 +31,29 @@ object StorageManager {
}
}

fun toggleFavoriteForPosition(latLng: LatLng) {
if (favorites.contains(latLng))
removeFavorite(latLng)
else
addLocationToFavorites(latLng)
}

private fun addLocationToFavorites(latLng: LatLng) {
val tempFavorites = favorites
tempFavorites.add(latLng)
favorites = tempFavorites
}

private fun removeFavorite(latLng: LatLng) {
val tempFavorites = favorites
tempFavorites.remove(latLng)
favorites = tempFavorites
}

private var locationHistory: MutableList<LatLng>
get() {
val json = pref.getString(KEY_HISTORY, "[]")
val typeToken = object : TypeToken<MutableList<LatLng>>() {}
val typeToken = object : TypeToken<MutableList<LatLng>>() {}.type
return Gson().fromJson(json, typeToken)
}
private set(value) {
Expand All @@ -44,4 +64,18 @@ object StorageManager {
}
}

var favorites: MutableList<LatLng>
get() {
val json = pref.getString(KEY_FAVORITES, "[]")
val typeToken = object : TypeToken<MutableList<LatLng>>() {}.type
return Gson().fromJson(json, typeToken)
}
private set(value) {
val json = Gson().toJson(value)
pref.edit {
putString(KEY_FAVORITES, json)
commit()
}
}

}
Loading

0 comments on commit ed8c698

Please sign in to comment.