Skip to content

Commit

Permalink
Build140
Browse files Browse the repository at this point in the history
  - Fixed DMS formatting issues
  - MVVM architecture for Coordinates and Custom Locations
  - Typos
  • Loading branch information
Hamza417 committed Aug 29, 2021
1 parent 6bc5423 commit 6c66395
Show file tree
Hide file tree
Showing 11 changed files with 311 additions and 173 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ android {

compileSdkVersion 30

def appVersionCode = 139
def appVersionCode = 140
def appVersionName = "positional_build_${appVersionCode}_final"

defaultConfig {
Expand Down Expand Up @@ -138,7 +138,7 @@ dependencies {
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'

// Kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.30"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0'

// Google Services and Default android libs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ class LocationsAdapter : RecyclerView.Adapter<LocationsAdapter.Holder>() {
notifyDataSetChanged()
}

@SuppressLint("NotifyDataSetChanged")
fun addLocation(locations: Locations) {
this.locations.add(0, locations)
notifyItemInserted(0)
}

fun clearList() {
for (i in locations.indices) {
removeItem(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface LocationDao {
* @param location saves location details
*/
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insetLocation(location: Locations)
suspend fun insertLocation(location: Locations)

/**
* Update location
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package app.simple.positional.dialogs.gps

import android.content.*
import android.location.Location
import android.os.Bundle
import android.os.Handler
import android.os.Looper
Expand All @@ -10,8 +9,8 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import app.simple.positional.BuildConfig
import app.simple.positional.R
import app.simple.positional.decorations.ripple.DynamicRippleImageButton
Expand All @@ -21,6 +20,7 @@ import app.simple.positional.util.DMSConverter
import app.simple.positional.util.HtmlHelper.fromHtml
import app.simple.positional.util.TextViewUtils.setTextAnimation
import app.simple.positional.util.UTMConverter
import app.simple.positional.viewmodels.viewmodel.LocationViewModel
import gov.nasa.worldwind.geom.Angle
import gov.nasa.worldwind.geom.coords.MGRSCoord
import kotlinx.coroutines.Dispatchers
Expand All @@ -29,7 +29,7 @@ import kotlinx.coroutines.withContext

class CoordinatesExpansion : CustomBottomSheetDialogFragment() {

private lateinit var broadcastReceiver: BroadcastReceiver
private lateinit var locationViewModel: LocationViewModel
private val handler = Handler(Looper.getMainLooper())

private lateinit var coordinatesDataTextView: TextView
Expand Down Expand Up @@ -65,24 +65,14 @@ class CoordinatesExpansion : CustomBottomSheetDialogFragment() {

copyImageButton = view.findViewById(R.id.coordinates_copy)

locationViewModel = ViewModelProvider(requireActivity()).get(LocationViewModel::class.java)

return view
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

formatCoordinates(requireArguments().getDouble("latitude"), requireArguments().getDouble("longitude"))

broadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent) {
if (intent.action == "location") {
if (MainPreferences.isCustomCoordinate()) return
val location = intent.getParcelableExtra<Location>("location") ?: return
formatCoordinates(location.latitude, location.longitude)
}
}
}

copyImageButton.setOnClickListener {
handler.removeCallbacks(textAnimationRunnable)
val clipboard: ClipboardManager = requireContext().getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
Expand Down Expand Up @@ -123,20 +113,40 @@ class CoordinatesExpansion : CustomBottomSheetDialogFragment() {
handler.postDelayed(textAnimationRunnable, 3000)
}
}
}

override fun onResume() {
super.onResume()
if (!MainPreferences.isCustomCoordinate()) {
LocalBroadcastManager.getInstance(requireContext()).registerReceiver(broadcastReceiver, IntentFilter("location"))
}
}
if (MainPreferences.isCustomCoordinate()) {
with(MainPreferences.getCoordinates()) {
formatCoordinates(this[0].toDouble(), this[1].toDouble())
}

override fun onPause() {
super.onPause()
if (!MainPreferences.isCustomCoordinate()) {
LocalBroadcastManager.getInstance(requireContext()).unregisterReceiver(broadcastReceiver)
return
}

locationViewModel.dms.observe(viewLifecycleOwner, {
dmsLatitude.text = it.first
dmsLongitude.text = it.second
})

locationViewModel.dm.observe(viewLifecycleOwner, {
dmLatitude.text = it.first
dmLongitude.text = it.second
})

locationViewModel.dd.observe(viewLifecycleOwner, {
ddLatitude.text = it.first
ddLongitude.text = it.second
})

locationViewModel.mgrs.observe(viewLifecycleOwner, {
mgrsCoordinates.text = it
})

locationViewModel.utm.observe(viewLifecycleOwner, {
utmZone.text = fromHtml("<b>${getString(R.string.utm_zone)}</b> ${it.zone}")
utmEasting.text = fromHtml("<b>${getString(R.string.utm_easting)}</b> ${it.easting}")
utmNorthing.text = fromHtml("<b>${getString(R.string.utm_northing)}</b> ${it.northing}")
utmMeridian.text = fromHtml("<b>${getString(R.string.utm_meridian)}</b> ${it.centralMeridian}")
})
}

override fun onDestroy() {
Expand All @@ -147,7 +157,6 @@ class CoordinatesExpansion : CustomBottomSheetDialogFragment() {

private fun formatCoordinates(latitude: Double, longitude: Double) {
viewLifecycleOwner.lifecycleScope.launch {

val dmsLatitude: Spanned
val dmsLongitude: Spanned
val dmLatitude: Spanned
Expand All @@ -163,10 +172,10 @@ class CoordinatesExpansion : CustomBottomSheetDialogFragment() {
withContext(Dispatchers.Default) {
dmsLatitude = fromHtml("<b>${getString(R.string.gps_latitude)}</b> ${DMSConverter.latitudeAsDMS(latitude, requireContext())}")
dmsLongitude = fromHtml("<b>${getString(R.string.gps_longitude)}</b> ${DMSConverter.longitudeAsDMS(longitude, requireContext())}")
dmLatitude = fromHtml("<b>${getString(R.string.gps_latitude)}</b> ${DMSConverter.getLatitudeAsDM(latitude, requireContext())}")
dmLongitude = fromHtml("<b>${getString(R.string.gps_longitude)}</b> ${DMSConverter.getLongitudeAsDM(longitude, requireContext())}")
ddLatitude = fromHtml("<b>${getString(R.string.gps_latitude)}</b> ${DMSConverter.getLatitudeAsDD(latitude, requireContext())}")
ddLongitude = fromHtml("<b>${getString(R.string.gps_longitude)}</b> ${DMSConverter.getLongitudeAsDD(longitude, requireContext())}")
dmLatitude = fromHtml("<b>${getString(R.string.gps_latitude)}</b> ${DMSConverter.latitudeAsDM(latitude, requireContext())}")
dmLongitude = fromHtml("<b>${getString(R.string.gps_longitude)}</b> ${DMSConverter.longitudeAsDM(longitude, requireContext())}")
ddLatitude = fromHtml("<b>${getString(R.string.gps_latitude)}</b> ${DMSConverter.latitudeAsDD(latitude)}")
ddLongitude = fromHtml("<b>${getString(R.string.gps_longitude)}</b> ${DMSConverter.longitudeAsDD(longitude)}")
mgrsCoord = MGRSCoord.fromLatLon(Angle.fromDegreesLatitude(latitude), Angle.fromDegreesLongitude(longitude)).toString()

val utm = UTMConverter.getUTM(latitude, longitude)
Expand Down Expand Up @@ -195,10 +204,8 @@ class CoordinatesExpansion : CustomBottomSheetDialogFragment() {
}

companion object {
fun newInstance(latitude: Double, longitude: Double): CoordinatesExpansion {
fun newInstance(): CoordinatesExpansion {
val args = Bundle()
args.putDouble("latitude", latitude)
args.putDouble("longitude", longitude)
val fragment = CoordinatesExpansion()
fragment.arguments = args
return fragment
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package app.simple.positional.preferences

import android.annotation.SuppressLint
import androidx.annotation.IntRange
import androidx.annotation.NonNull
import androidx.appcompat.app.AppCompatDelegate
Expand Down Expand Up @@ -134,12 +135,14 @@ object MainPreferences {

//--------------------------------------------------------------------------------------------------//

@SuppressLint("ApplySharedPref")
fun setLatitude(@NotNull value: Float) {
getSharedPreferences().edit().putFloat(latitude, value).apply()
getSharedPreferences().edit().putFloat(latitude, value).commit()
}

@SuppressLint("ApplySharedPref")
fun setLongitude(@NotNull value: Float) {
getSharedPreferences().edit().putFloat(longitude, value).apply()
getSharedPreferences().edit().putFloat(longitude, value).commit()
}

fun getCoordinates(): FloatArray {
Expand Down
12 changes: 3 additions & 9 deletions app/src/main/java/app/simple/positional/ui/panels/GPS.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ class GPS : ScopedFragment() {
private lateinit var infoText: TextView

private lateinit var handler: Handler
private var filter: IntentFilter = IntentFilter()
private lateinit var bottomSheetInfoPanel: BottomSheetBehavior<CoordinatorLayout>
private var location: Location? = null
private var backPress: OnBackPressedDispatcher? = null
Expand Down Expand Up @@ -414,7 +413,7 @@ class GPS : ScopedFragment() {
locations.address = address.text.toString()
locations.date = System.currentTimeMillis()

db.locationDao()?.insetLocation(locations)
db.locationDao()?.insertLocation(locations)
db.close()

isLocationSaved = true
Expand Down Expand Up @@ -497,13 +496,8 @@ class GPS : ScopedFragment() {
}

coordinatesBox.setOnClickListener {
if (location.isNull() && !isCustomCoordinate) {
Toast.makeText(requireContext(), R.string.location_not_available, Toast.LENGTH_SHORT).show()
} else {
val latitude = if (isCustomCoordinate) customLatitude else location!!.latitude
val longitude = if (isCustomCoordinate) customLongitude else location!!.longitude
CoordinatesExpansion.newInstance(latitude, longitude).show(childFragmentManager, "coordinates_expansion")
}
CoordinatesExpansion.newInstance()
.show(childFragmentManager, "coordinates_expansion")
}

maps?.setOnMapsCallbackListener(object : MapsCallbacks {
Expand Down
Loading

0 comments on commit 6c66395

Please sign in to comment.