From 0648ccad4be9a3001a26b0047898c65024ce4d0a Mon Sep 17 00:00:00 2001 From: PWRxPSYCHO Date: Wed, 24 Aug 2022 12:16:57 -0400 Subject: [PATCH] Moved Map Source menu to MapFragment --- .../java/com/geeksville/mesh/MainActivity.kt | 45 ++++++------------- .../com/geeksville/mesh/ui/MapFragment.kt | 33 +++++++++++++- app/src/main/res/layout/map_view.xml | 18 ++++++-- app/src/main/res/menu/menu_main.xml | 4 -- 4 files changed, 59 insertions(+), 41 deletions(-) diff --git a/app/src/main/java/com/geeksville/mesh/MainActivity.kt b/app/src/main/java/com/geeksville/mesh/MainActivity.kt index e38b7d5d7..d02041216 100644 --- a/app/src/main/java/com/geeksville/mesh/MainActivity.kt +++ b/app/src/main/java/com/geeksville/mesh/MainActivity.kt @@ -126,6 +126,7 @@ class MainActivity : BaseActivity(), Logging, // const val REQUEST_ENABLE_BT = 10 const val DID_REQUEST_PERM = 11 const val RC_SIGN_IN = 12 // google signin completed + // const val SELECT_DEVICE_REQUEST_CODE = 13 const val CREATE_CSV_FILE = 14 } @@ -331,7 +332,7 @@ class MainActivity : BaseActivity(), Logging, DID_REQUEST_PERM -> { // If request is cancelled, the result arrays are empty. if ((grantResults.isNotEmpty() && - grantResults[0] == PackageManager.PERMISSION_GRANTED) + grantResults[0] == PackageManager.PERMISSION_GRANTED) ) { // Permission is granted. Continue the action or workflow // in your app. @@ -665,7 +666,13 @@ class MainActivity : BaseActivity(), Logging, // model.setLocalConfig(LocalOnlyProtos.LocalConfig.parseFrom(service.deviceConfig)) - model.setChannels(ChannelSet(AppOnlyProtos.ChannelSet.parseFrom(service.channels))) + model.setChannels( + ChannelSet( + AppOnlyProtos.ChannelSet.parseFrom( + service.channels + ) + ) + ) model.updateNodesFromDevice() @@ -1073,14 +1080,10 @@ class MainActivity : BaseActivity(), Logging, chooseLangDialog() return true } - R.id.preferences_map_style -> { - chooseMapStyle() - return true - } R.id.show_intro -> { startActivity(Intent(this, AppIntroduction::class.java)) return true - } + } R.id.preferences_quick_chat -> { val fragmentManager: FragmentManager = supportFragmentManager val fragmentTransaction: FragmentTransaction = fragmentManager.beginTransaction() @@ -1193,7 +1196,10 @@ class MainActivity : BaseActivity(), Logging, val lang = prefs.getString("lang", "zz") debug("Lang from prefs: $lang") - builder.setSingleChoiceItems(languageLabels, languageValues.indexOf(lang)) { dialog, which -> + builder.setSingleChoiceItems( + languageLabels, + languageValues.indexOf(lang) + ) { dialog, which -> val selectedLang = languageValues[which] debug("Set lang pref to $selectedLang") editor.putString("lang", selectedLang) @@ -1203,27 +1209,4 @@ class MainActivity : BaseActivity(), Logging, val dialog = builder.create() dialog.show() } - - private fun chooseMapStyle() { - /// Prepare dialog and its items - val builder = MaterialAlertDialogBuilder(this) - builder.setTitle(getString(R.string.preferences_map_style)) - val mapStyles by lazy { resources.getStringArray(R.array.map_styles) } - - /// Load preferences and its value - val prefs = UIViewModel.getPreferences(this) - val editor: SharedPreferences.Editor = prefs.edit() - val mapStyleId = prefs.getInt("map_style_id", 1) - debug("mapStyleId from prefs: $mapStyleId") - - builder.setSingleChoiceItems(mapStyles, mapStyleId) { dialog, which -> - debug("Set mapStyleId pref to $which") - editor.putInt("map_style_id", which) - editor.apply() - dialog.dismiss() - } - val dialog = builder.create() - dialog.show() - } - } diff --git a/app/src/main/java/com/geeksville/mesh/ui/MapFragment.kt b/app/src/main/java/com/geeksville/mesh/ui/MapFragment.kt index bcda93187..566fce4a0 100644 --- a/app/src/main/java/com/geeksville/mesh/ui/MapFragment.kt +++ b/app/src/main/java/com/geeksville/mesh/ui/MapFragment.kt @@ -13,8 +13,10 @@ import com.geeksville.android.Logging import com.geeksville.mesh.BuildConfig import com.geeksville.mesh.NodeInfo import com.geeksville.mesh.R +import com.geeksville.mesh.databinding.MapViewBinding import com.geeksville.mesh.model.UIViewModel import com.geeksville.util.formatAgo +import com.google.android.material.dialog.MaterialAlertDialogBuilder import dagger.hilt.android.AndroidEntryPoint import org.osmdroid.api.IMapController import org.osmdroid.config.Configuration @@ -31,6 +33,7 @@ import org.osmdroid.views.overlay.Marker @AndroidEntryPoint class MapFragment : ScreenFragment("Map"), Logging { + private lateinit var binding: MapViewBinding private lateinit var map: MapView private lateinit var mapController: IMapController private lateinit var mPrefs: SharedPreferences @@ -54,10 +57,11 @@ class MapFragment : ScreenFragment("Map"), Logging { inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { - map = MapView(inflater.context) + binding = MapViewBinding.inflate(inflater, container, false) + map = binding.map map.setDestroyMode(false) map.tag = mapTag - return map + return binding.root } override fun onViewCreated(viewIn: View, savedInstanceState: Bundle?) { @@ -74,6 +78,9 @@ class MapFragment : ScreenFragment("Map"), Logging { val point = GeoPoint(defaultLat, defaultLong) //White House Coordinates, Washington DC mapController.animateTo(point, defaultZoomLevel, defaultZoomSpeed) if (view != null) { + binding.fabStyleToggle.setOnClickListener { + chooseMapStyle() + } model.nodeDB.nodes.value?.let { nodes -> onNodesChanged(nodes.values) } @@ -87,6 +94,28 @@ class MapFragment : ScreenFragment("Map"), Logging { } } + private fun chooseMapStyle() { + /// Prepare dialog and its items + val builder = MaterialAlertDialogBuilder(context!!) + builder.setTitle(getString(R.string.preferences_map_style)) + val mapStyles by lazy { resources.getStringArray(R.array.map_styles) } + + /// Load preferences and its value + val prefs = UIViewModel.getPreferences(context!!) + val editor: SharedPreferences.Editor = prefs.edit() + val mapStyleId = prefs.getInt("map_style_id", 1) + debug("mapStyleId from prefs: $mapStyleId") + + builder.setSingleChoiceItems(mapStyles, mapStyleId) { dialog, which -> + debug("Set mapStyleId pref to $which") + editor.putInt("map_style_id", which) + editor.apply() + dialog.dismiss() + } + val dialog = builder.create() + dialog.show() + } + private fun onNodesChanged(nodes: Collection) { val nodesWithPosition = nodes.filter { it.validPosition != null } diff --git a/app/src/main/res/layout/map_view.xml b/app/src/main/res/layout/map_view.xml index a1a6e1265..7ed1b0c44 100644 --- a/app/src/main/res/layout/map_view.xml +++ b/app/src/main/res/layout/map_view.xml @@ -8,9 +8,19 @@ - + android:layout_height="match_parent" /> + \ No newline at end of file diff --git a/app/src/main/res/menu/menu_main.xml b/app/src/main/res/menu/menu_main.xml index 12ba21d62..d7817540f 100644 --- a/app/src/main/res/menu/menu_main.xml +++ b/app/src/main/res/menu/menu_main.xml @@ -32,10 +32,6 @@ android:id="@+id/preferences_language" android:title="@string/preferences_language" app:showAsAction="withText" /> -