mirror of
https://github.com/rt-bishop/Look4Sat.git
synced 2026-10-02 03:15:37 +00:00
Minor fixes to MapFragment.kt and MapViewModel.kt
This commit is contained in:
1 parent
fe69e90749
commit
48758b4f3d
2 files changed
+31
-52
No files matched your search
@@ -27,40 +27,33 @@ import androidx.fragment.app.Fragment
|
|||||||
import androidx.fragment.app.viewModels
|
import androidx.fragment.app.viewModels
|
||||||
import com.rtbishop.look4sat.R
|
import com.rtbishop.look4sat.R
|
||||||
import com.rtbishop.look4sat.databinding.FragmentMapBinding
|
import com.rtbishop.look4sat.databinding.FragmentMapBinding
|
||||||
import com.rtbishop.look4sat.framework.model.SelectedSat
|
|
||||||
import com.rtbishop.look4sat.domain.predict4kotlin.Position
|
import com.rtbishop.look4sat.domain.predict4kotlin.Position
|
||||||
import com.rtbishop.look4sat.domain.predict4kotlin.Satellite
|
import com.rtbishop.look4sat.domain.predict4kotlin.Satellite
|
||||||
import com.rtbishop.look4sat.utility.PrefsManager
|
import com.rtbishop.look4sat.framework.model.SelectedSat
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import org.osmdroid.config.Configuration
|
import org.osmdroid.config.Configuration
|
||||||
import org.osmdroid.tileprovider.tilesource.ITileSource
|
import org.osmdroid.tileprovider.tilesource.TileSourceFactory
|
||||||
import org.osmdroid.tileprovider.tilesource.TileSourcePolicy
|
|
||||||
import org.osmdroid.tileprovider.tilesource.XYTileSource
|
|
||||||
import org.osmdroid.util.GeoPoint
|
import org.osmdroid.util.GeoPoint
|
||||||
import org.osmdroid.views.CustomZoomButtonsController
|
import org.osmdroid.views.CustomZoomButtonsController
|
||||||
import org.osmdroid.views.MapView
|
import org.osmdroid.views.MapView
|
||||||
import org.osmdroid.views.overlay.*
|
import org.osmdroid.views.overlay.*
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import javax.inject.Inject
|
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class MapFragment : Fragment(R.layout.fragment_map) {
|
class MapFragment : Fragment(R.layout.fragment_map) {
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var prefsManager: PrefsManager
|
|
||||||
private lateinit var binding: FragmentMapBinding
|
|
||||||
private val mapViewModel: MapViewModel by viewModels()
|
private val mapViewModel: MapViewModel by viewModels()
|
||||||
private val minLat = MapView.getTileSystem().minLatitude
|
private val minLat = MapView.getTileSystem().minLatitude
|
||||||
private val maxLat = MapView.getTileSystem().maxLatitude
|
private val maxLat = MapView.getTileSystem().maxLatitude
|
||||||
|
|
||||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
super.onViewCreated(view, savedInstanceState)
|
super.onViewCreated(view, savedInstanceState)
|
||||||
Configuration.getInstance().load(requireContext(), prefsManager.preferences)
|
Configuration.getInstance().load(requireContext(), mapViewModel.getPreferences())
|
||||||
binding = FragmentMapBinding.bind(view).apply {
|
val binding = FragmentMapBinding.bind(view).apply {
|
||||||
mapView.apply {
|
mapView.apply {
|
||||||
setMultiTouchControls(true)
|
setMultiTouchControls(true)
|
||||||
setTileSource(getTileSource())
|
setTileSource(TileSourceFactory.WIKIMEDIA)
|
||||||
val minZoom = getMinZoom(resources.displayMetrics.heightPixels)
|
val minZoom = getMinZoom(resources.displayMetrics.heightPixels)
|
||||||
minZoomLevel = minZoom
|
minZoomLevel = minZoom
|
||||||
maxZoomLevel = 6.0
|
maxZoomLevel = 6.0
|
||||||
@@ -76,16 +69,16 @@ class MapFragment : Fragment(R.layout.fragment_map) {
|
|||||||
}
|
}
|
||||||
binding.fabPrev.setOnClickListener { mapViewModel.scrollSelection(true) }
|
binding.fabPrev.setOnClickListener { mapViewModel.scrollSelection(true) }
|
||||||
binding.fabNext.setOnClickListener { mapViewModel.scrollSelection(false) }
|
binding.fabNext.setOnClickListener { mapViewModel.scrollSelection(false) }
|
||||||
setupObservers()
|
setupObservers(binding)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setupObservers() {
|
private fun setupObservers(binding: FragmentMapBinding) {
|
||||||
mapViewModel.stationPosition.observe(viewLifecycleOwner, { setupPosOverlay(it) })
|
mapViewModel.stationPosition.observe(viewLifecycleOwner, { setupPosOverlay(it, binding) })
|
||||||
mapViewModel.getSelectedSat().observe(viewLifecycleOwner, { setSelectedSatDetails(it) })
|
mapViewModel.getSelectedSat().observe(viewLifecycleOwner, { setSatDetails(it, binding) })
|
||||||
mapViewModel.getSatMarkers().observe(viewLifecycleOwner, { setMarkers(it) })
|
mapViewModel.getSatMarkers().observe(viewLifecycleOwner, { setMarkers(it, binding) })
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setupPosOverlay(osmPos: Position) {
|
private fun setupPosOverlay(osmPos: Position, binding: FragmentMapBinding) {
|
||||||
binding.apply {
|
binding.apply {
|
||||||
Marker(mapView).apply {
|
Marker(mapView).apply {
|
||||||
setInfoWindow(null)
|
setInfoWindow(null)
|
||||||
@@ -98,7 +91,7 @@ class MapFragment : Fragment(R.layout.fragment_map) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setSelectedSatDetails(sat: SelectedSat) {
|
private fun setSatDetails(sat: SelectedSat, binding: FragmentMapBinding) {
|
||||||
binding.apply {
|
binding.apply {
|
||||||
idName.text = String.format(getString(R.string.pat_osm_idName), sat.catNum, sat.name)
|
idName.text = String.format(getString(R.string.pat_osm_idName), sat.catNum, sat.name)
|
||||||
qthLocator.text = String.format(getString(R.string.map_qth), sat.qthLoc)
|
qthLocator.text = String.format(getString(R.string.map_qth), sat.qthLoc)
|
||||||
@@ -113,11 +106,11 @@ class MapFragment : Fragment(R.layout.fragment_map) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setMarkers(map: Map<Satellite, Position>) {
|
private fun setMarkers(map: Map<Satellite, Position>, binding: FragmentMapBinding) {
|
||||||
binding.apply {
|
binding.apply {
|
||||||
val markers = FolderOverlay()
|
val markers = FolderOverlay()
|
||||||
map.entries.forEach {
|
map.entries.forEach {
|
||||||
if (prefsManager.shouldUseTextLabels()) {
|
if (mapViewModel.shouldUseTextLabels()) {
|
||||||
Marker(mapView).apply {
|
Marker(mapView).apply {
|
||||||
setInfoWindow(null)
|
setInfoWindow(null)
|
||||||
textLabelFontSize = 24
|
textLabelFontSize = 24
|
||||||
@@ -161,10 +154,7 @@ class MapFragment : Fragment(R.layout.fragment_map) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun getColorFilter(): ColorMatrixColorFilter {
|
private fun getColorFilter(): ColorMatrixColorFilter {
|
||||||
val targetColor = Color.RED
|
val grayScaleMatrix = ColorMatrix().apply { setSaturation(0f) }
|
||||||
val newR = Color.red(targetColor) / 255f
|
|
||||||
val newG = Color.green(targetColor) / 255f
|
|
||||||
val newB = Color.blue(targetColor) / 255f
|
|
||||||
val negativeMatrix = ColorMatrix(
|
val negativeMatrix = ColorMatrix(
|
||||||
floatArrayOf(
|
floatArrayOf(
|
||||||
-1f, 0f, 0f, 0f, 255f,
|
-1f, 0f, 0f, 0f, 255f,
|
||||||
@@ -173,31 +163,11 @@ class MapFragment : Fragment(R.layout.fragment_map) {
|
|||||||
0f, 0f, 0f, 1f, 0f
|
0f, 0f, 0f, 1f, 0f
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
val tintedMatrix = ColorMatrix(
|
negativeMatrix.preConcat(grayScaleMatrix)
|
||||||
floatArrayOf(
|
return ColorMatrixColorFilter(negativeMatrix)
|
||||||
newR, newG, newB, 0f, 0f,
|
|
||||||
newR, newG, newB, 0f, 0f,
|
|
||||||
newR, newG, newB, 0f, 0f,
|
|
||||||
0f, 0f, 0f, 0f, 255f
|
|
||||||
)
|
|
||||||
)
|
|
||||||
tintedMatrix.preConcat(negativeMatrix)
|
|
||||||
return ColorMatrixColorFilter(tintedMatrix)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getMinZoom(screenHeight: Int): Double {
|
private fun getMinZoom(screenHeight: Int): Double {
|
||||||
val minZoom = MapView.getTileSystem().getLatitudeZoom(maxLat, minLat, screenHeight)
|
return MapView.getTileSystem().getLatitudeZoom(maxLat, minLat, screenHeight)
|
||||||
Timber.d("Min zoom level for this screen: $minZoom")
|
|
||||||
return minZoom
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getTileSource(): ITileSource {
|
|
||||||
val copyright = resources.getString(R.string.map_copyright)
|
|
||||||
val sources = arrayOf("https://maps.wikimedia.org/osm-intl/")
|
|
||||||
val policy = TileSourcePolicy(
|
|
||||||
1, TileSourcePolicy.FLAG_NO_BULK and TileSourcePolicy.FLAG_NO_PREVENTIVE and
|
|
||||||
TileSourcePolicy.FLAG_USER_AGENT_MEANINGFUL and TileSourcePolicy.FLAG_USER_AGENT_NORMALIZED
|
|
||||||
)
|
|
||||||
return XYTileSource("wikimedia", 0, 6, 256, ".png", sources, copyright, policy)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -17,15 +17,16 @@
|
|||||||
*/
|
*/
|
||||||
package com.rtbishop.look4sat.presentation.mapScreen
|
package com.rtbishop.look4sat.presentation.mapScreen
|
||||||
|
|
||||||
|
import android.content.SharedPreferences
|
||||||
import android.graphics.Color
|
import android.graphics.Color
|
||||||
import android.graphics.Paint
|
import android.graphics.Paint
|
||||||
import androidx.lifecycle.*
|
import androidx.lifecycle.*
|
||||||
import com.rtbishop.look4sat.framework.model.SelectedSat
|
|
||||||
import com.rtbishop.look4sat.interactors.GetSelectedSatellites
|
|
||||||
import com.rtbishop.look4sat.domain.predict4kotlin.Position
|
import com.rtbishop.look4sat.domain.predict4kotlin.Position
|
||||||
import com.rtbishop.look4sat.domain.predict4kotlin.QthConverter
|
import com.rtbishop.look4sat.domain.predict4kotlin.QthConverter
|
||||||
import com.rtbishop.look4sat.domain.predict4kotlin.SatPos
|
import com.rtbishop.look4sat.domain.predict4kotlin.SatPos
|
||||||
import com.rtbishop.look4sat.domain.predict4kotlin.Satellite
|
import com.rtbishop.look4sat.domain.predict4kotlin.Satellite
|
||||||
|
import com.rtbishop.look4sat.framework.model.SelectedSat
|
||||||
|
import com.rtbishop.look4sat.interactors.GetSelectedSatellites
|
||||||
import com.rtbishop.look4sat.utility.*
|
import com.rtbishop.look4sat.utility.*
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
@@ -41,8 +42,8 @@ import javax.inject.Inject
|
|||||||
@HiltViewModel
|
@HiltViewModel
|
||||||
class MapViewModel @Inject constructor(
|
class MapViewModel @Inject constructor(
|
||||||
private val getSelectedSatellites: GetSelectedSatellites,
|
private val getSelectedSatellites: GetSelectedSatellites,
|
||||||
private val qthConverter: QthConverter,
|
private val prefsManager: PrefsManager,
|
||||||
prefsManager: PrefsManager
|
private val qthConverter: QthConverter
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
|
|
||||||
private val dateNow = Date()
|
private val dateNow = Date()
|
||||||
@@ -92,6 +93,14 @@ class MapViewModel @Inject constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getPreferences(): SharedPreferences {
|
||||||
|
return prefsManager.preferences
|
||||||
|
}
|
||||||
|
|
||||||
|
fun shouldUseTextLabels(): Boolean {
|
||||||
|
return prefsManager.shouldUseTextLabels()
|
||||||
|
}
|
||||||
|
|
||||||
fun scrollSelection(decrement: Boolean) {
|
fun scrollSelection(decrement: Boolean) {
|
||||||
if (filteredSats.isNotEmpty()) {
|
if (filteredSats.isNotEmpty()) {
|
||||||
val index = filteredSats.indexOf(selectedSat)
|
val index = filteredSats.indexOf(selectedSat)
|
||||||
|
|||||||
Reference in new issue
Block a user