From 07cc8ce7015a765a961181f6ee351a65220590eb Mon Sep 17 00:00:00 2001 From: PWRxPSYCHO Date: Thu, 20 Oct 2022 08:50:21 -0400 Subject: [PATCH] Updated UI for Map Layers & Style. Still need on Click listener --- .../geeksville/mesh/model/map/ChildData.kt | 3 + .../geeksville/mesh/model/map/Constants.kt | 6 + .../mesh/model/map/MapParentData.kt | 8 ++ .../com/geeksville/mesh/ui/MapFragment.kt | 65 +++++------ .../mesh/ui/MapMenuRecyclerAdapter.kt | 109 ++++++++++++++++++ .../com/geeksville/mesh/ui/MapStyleAdapter.kt | 43 ------- .../res/layout/adapter_map_menu_selection.xml | 24 ---- app/src/main/res/layout/child_row.xml | 22 ++++ app/src/main/res/layout/map_menu.xml | 40 ------- app/src/main/res/layout/map_menu_layout.xml | 12 ++ app/src/main/res/layout/parent_row.xml | 34 ++++++ 11 files changed, 223 insertions(+), 143 deletions(-) create mode 100644 app/src/main/java/com/geeksville/mesh/model/map/ChildData.kt create mode 100644 app/src/main/java/com/geeksville/mesh/model/map/Constants.kt create mode 100644 app/src/main/java/com/geeksville/mesh/model/map/MapParentData.kt create mode 100644 app/src/main/java/com/geeksville/mesh/ui/MapMenuRecyclerAdapter.kt delete mode 100644 app/src/main/java/com/geeksville/mesh/ui/MapStyleAdapter.kt delete mode 100644 app/src/main/res/layout/adapter_map_menu_selection.xml create mode 100644 app/src/main/res/layout/child_row.xml delete mode 100644 app/src/main/res/layout/map_menu.xml create mode 100644 app/src/main/res/layout/map_menu_layout.xml create mode 100644 app/src/main/res/layout/parent_row.xml diff --git a/app/src/main/java/com/geeksville/mesh/model/map/ChildData.kt b/app/src/main/java/com/geeksville/mesh/model/map/ChildData.kt new file mode 100644 index 00000000..136c5e8e --- /dev/null +++ b/app/src/main/java/com/geeksville/mesh/model/map/ChildData.kt @@ -0,0 +1,3 @@ +package com.geeksville.mesh.model.map + +data class ChildData(val childTitle: String) diff --git a/app/src/main/java/com/geeksville/mesh/model/map/Constants.kt b/app/src/main/java/com/geeksville/mesh/model/map/Constants.kt new file mode 100644 index 00000000..4ce04c5c --- /dev/null +++ b/app/src/main/java/com/geeksville/mesh/model/map/Constants.kt @@ -0,0 +1,6 @@ +package com.geeksville.mesh.model.map + +object Constants { + const val PARENT = 0 + const val CHILD = 1 +} \ No newline at end of file diff --git a/app/src/main/java/com/geeksville/mesh/model/map/MapParentData.kt b/app/src/main/java/com/geeksville/mesh/model/map/MapParentData.kt new file mode 100644 index 00000000..e99a28af --- /dev/null +++ b/app/src/main/java/com/geeksville/mesh/model/map/MapParentData.kt @@ -0,0 +1,8 @@ +package com.geeksville.mesh.model.map + +data class MapParentData( + val title: String? = null, + var type:Int = Constants.PARENT, + var subList: MutableList = ArrayList(), + var isExpanded: Boolean = false +) 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 5090054f..9da27e27 100644 --- a/app/src/main/java/com/geeksville/mesh/ui/MapFragment.kt +++ b/app/src/main/java/com/geeksville/mesh/ui/MapFragment.kt @@ -15,19 +15,20 @@ import android.widget.* import androidx.core.content.ContextCompat import androidx.fragment.app.activityViewModels import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView import com.geeksville.mesh.BuildConfig import com.geeksville.mesh.NodeInfo import com.geeksville.mesh.R import com.geeksville.mesh.android.Logging import com.geeksville.mesh.database.entity.Packet -import com.geeksville.mesh.databinding.AdapterMapMenuSelectionBinding -import com.geeksville.mesh.databinding.MapMenuBinding +import com.geeksville.mesh.databinding.MapMenuLayoutBinding import com.geeksville.mesh.databinding.MapViewBinding import com.geeksville.mesh.model.UIViewModel +import com.geeksville.mesh.model.map.ChildData import com.geeksville.mesh.model.map.CustomOverlayManager import com.geeksville.mesh.model.map.CustomTileSource +import com.geeksville.mesh.model.map.MapParentData import com.geeksville.mesh.util.SqlTileWriterExt -import com.geeksville.mesh.util.SqlTileWriterExt.SourceCount import com.geeksville.mesh.util.formatAgo import com.google.android.material.dialog.MaterialAlertDialogBuilder import dagger.hilt.android.AndroidEntryPoint @@ -93,7 +94,6 @@ class MapFragment : ScreenFragment("Map"), Logging, View.OnClickListener { private lateinit var cacheManager: CacheManager private lateinit var downloadRegionBoundingBox: BoundingBox - override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { @@ -185,18 +185,6 @@ class MapFragment : ScreenFragment("Map"), Logging, View.OnClickListener { } - /** - * Clears active tile source cache - */ - private fun clearCache() { - val b: Boolean = SqlTileWriter().purgeCache() - SqlTileWriter().onDetach() - val title = if (b) "SQL Cache purged" else "SQL Cache purge failed, see logcat for details" - val length = if (b) Toast.LENGTH_SHORT else Toast.LENGTH_LONG - Toast.makeText(activity, title, length).show() - alertDialog!!.dismiss() - } - private fun purgeTileSource() { cache = SqlTileWriterExt() val builder = AlertDialog.Builder(context) @@ -408,26 +396,31 @@ class MapFragment : ScreenFragment("Map"), Logging, View.OnClickListener { } private fun chooseMapStyle() { - val mapMenu = MapMenuBinding.inflate(layoutInflater, view as ViewGroup, true) - val layoutManager = LinearLayoutManager(requireContext()) - val adapterMenuSelection = AdapterMapMenuSelectionBinding.inflate(layoutInflater) - val adapter = MapStyleAdapter(adapterMenuSelection) - mapMenu.mapLayerRecyclerView.adapter = - adapter.mapStyleAdapater - mapMenu.mapTypeRecyclerView.layoutManager = layoutManager - mapMenu.mapTypeRecyclerView.adapter = adapter.mapLayerAdapater + /// Prepare dialog and its items + val mapStyles by lazy { resources.getStringArray(R.array.map_styles) } + val builder = MaterialAlertDialogBuilder(context!!) + val listData: MutableList = ArrayList() + val parentData: Array = arrayOf("Map Source", "Map Layer") + val mapStyleData: MutableList = mutableListOf() + mapStyles.forEach { style -> + mapStyleData.add(ChildData(style)) + } + val mapLayerData: MutableList = mutableListOf(ChildData("test")) -// /// 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 editor: SharedPreferences.Editor = mPrefs.edit() -// val mapStyleInt = mPrefs.getInt(mapStyleId, 1) -// debug("mapStyleId from prefs: $mapStyleInt") -// + val mapStyleObj = MapParentData(title = parentData[0], subList = mapStyleData) + val mapLayerObj = MapParentData(title = parentData[1], subList = mapLayerData) + listData.add(mapStyleObj) + listData.add(mapLayerObj) + val exRecycleView = MapMenuLayoutBinding.inflate(layoutInflater) + exRecycleView.exRecycle.layoutManager = LinearLayoutManager(context) + exRecycleView.exRecycle.adapter = MapMenuRecyclerAdapter(context!!, listData) + + builder.setView(exRecycleView.root) + /// Load preferences and its value + val editor: SharedPreferences.Editor = mPrefs.edit() + val mapStyleInt = mPrefs.getInt(mapStyleId, 1) + debug("mapStyleId from prefs: $mapStyleInt") // builder.setSingleChoiceItems(mapStyles, mapStyleInt) { dialog, which -> // debug("Set mapStyleId pref to $which") // editor.putInt(mapStyleId, which) @@ -437,8 +430,8 @@ class MapFragment : ScreenFragment("Map"), Logging, View.OnClickListener { // renderDownloadButton() // drawOverlays() // } -// val dialog = builder.create() -// dialog.show() + val dialog = builder.create() + dialog.show() } private fun renderDownloadButton() { diff --git a/app/src/main/java/com/geeksville/mesh/ui/MapMenuRecyclerAdapter.kt b/app/src/main/java/com/geeksville/mesh/ui/MapMenuRecyclerAdapter.kt new file mode 100644 index 00000000..7290b467 --- /dev/null +++ b/app/src/main/java/com/geeksville/mesh/ui/MapMenuRecyclerAdapter.kt @@ -0,0 +1,109 @@ +package com.geeksville.mesh.ui + +import android.content.Context +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.ImageView +import android.widget.TextView +import androidx.recyclerview.widget.RecyclerView +import com.geeksville.mesh.R +import com.geeksville.mesh.model.map.ChildData +import com.geeksville.mesh.model.map.Constants +import com.geeksville.mesh.model.map.MapParentData + +class MapMenuRecyclerAdapter(var mContext: Context, val list: MutableList) : + RecyclerView.Adapter() { + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { + + return if (viewType == Constants.PARENT) { + val rowView: View = + LayoutInflater.from(parent.context).inflate(R.layout.parent_row, parent, false) + GroupViewHolder(rowView) + } else { + val rowView: View = + LayoutInflater.from(parent.context).inflate(R.layout.child_row, parent, false) + ChildViewHolder(rowView) + } + } + + override fun getItemCount(): Int = list.size + + override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { + + val dataList = list[position] + if (dataList.type == Constants.PARENT) { + holder as GroupViewHolder + holder.apply { + parentTV?.text = dataList.title + downIV?.setOnClickListener { + expandOrCollapseParentItem(dataList, position) + } + } + } else { + holder as ChildViewHolder + + holder.apply { + val singleService = dataList.subList.first() + childTV?.text = singleService.childTitle + } + } + } + + private fun expandOrCollapseParentItem(singleBoarding: MapParentData, position: Int) { + + if (singleBoarding.isExpanded) { + collapseParentRow(position) + } else { + expandParentRow(position) + } + } + + private fun expandParentRow(position: Int) { + val currentBoardingRow = list[position] + val services = currentBoardingRow.subList + currentBoardingRow.isExpanded = true + var nextPosition = position + if (currentBoardingRow.type == Constants.PARENT) { + + services.forEach { service -> + val parentModel = MapParentData() + parentModel.type = Constants.CHILD + val subList: ArrayList = ArrayList() + subList.add(service) + parentModel.subList = subList + list.add(++nextPosition, parentModel) + } + notifyDataSetChanged() + } + } + + private fun collapseParentRow(position: Int) { + val currentBoardingRow = list[position] + val services = currentBoardingRow.subList + list[position].isExpanded = false + if (list[position].type == Constants.PARENT) { + services.forEach { _ -> + list.removeAt(position + 1) + } + notifyDataSetChanged() + } + } + + override fun getItemViewType(position: Int): Int = list[position].type + + override fun getItemId(position: Int): Long { + return position.toLong() + } + + class GroupViewHolder(row: View) : RecyclerView.ViewHolder(row) { + val parentTV = row.findViewById(R.id.parent_Title) as TextView? + val downIV = row.findViewById(R.id.down_iv) as ImageView? + } + + class ChildViewHolder(row: View) : RecyclerView.ViewHolder(row) { + val childTV = row.findViewById(R.id.child_Title) as TextView? + + } +} diff --git a/app/src/main/java/com/geeksville/mesh/ui/MapStyleAdapter.kt b/app/src/main/java/com/geeksville/mesh/ui/MapStyleAdapter.kt deleted file mode 100644 index 9137bfd3..00000000 --- a/app/src/main/java/com/geeksville/mesh/ui/MapStyleAdapter.kt +++ /dev/null @@ -1,43 +0,0 @@ -package com.geeksville.mesh.ui - -import android.view.LayoutInflater -import android.view.ViewGroup -import androidx.recyclerview.widget.RecyclerView -import com.geeksville.mesh.databinding.AdapterMapMenuSelectionBinding - -class MapStyleAdapter(itemView: AdapterMapMenuSelectionBinding) : - RecyclerView.ViewHolder(itemView.root) { - val card = itemView.cardview - val text = itemView.textview1 - - val mapStyleAdapater = object : RecyclerView.Adapter() { - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MapStyleAdapter { - val inflater = LayoutInflater.from(parent.context) - val mapMenuBinding = AdapterMapMenuSelectionBinding.inflate(inflater, parent, false) - return MapStyleAdapter(mapMenuBinding) - } - - override fun onBindViewHolder(holder: MapStyleAdapter, position: Int) { - TODO("Not yet implemented") - } - - val list = listOf() - override fun getItemCount() = list.size - - } - - val mapLayerAdapater = object : RecyclerView.Adapter() { - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MapStyleAdapter { - val inflater = LayoutInflater.from(parent.context) - val mapMenuBinding = AdapterMapMenuSelectionBinding.inflate(inflater, parent, false) - return MapStyleAdapter(mapMenuBinding) - } - - override fun onBindViewHolder(holder: MapStyleAdapter, position: Int) { - TODO("Not yet implemented") - } - - val list = listOf() - override fun getItemCount() = list.size - } -} \ No newline at end of file diff --git a/app/src/main/res/layout/adapter_map_menu_selection.xml b/app/src/main/res/layout/adapter_map_menu_selection.xml deleted file mode 100644 index 3fe53189..00000000 --- a/app/src/main/res/layout/adapter_map_menu_selection.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - \ No newline at end of file diff --git a/app/src/main/res/layout/child_row.xml b/app/src/main/res/layout/child_row.xml new file mode 100644 index 00000000..3026a287 --- /dev/null +++ b/app/src/main/res/layout/child_row.xml @@ -0,0 +1,22 @@ + + + + + + + diff --git a/app/src/main/res/layout/map_menu.xml b/app/src/main/res/layout/map_menu.xml deleted file mode 100644 index 90f2a662..00000000 --- a/app/src/main/res/layout/map_menu.xml +++ /dev/null @@ -1,40 +0,0 @@ - - - - - - - - - - - - - diff --git a/app/src/main/res/layout/map_menu_layout.xml b/app/src/main/res/layout/map_menu_layout.xml new file mode 100644 index 00000000..7897978b --- /dev/null +++ b/app/src/main/res/layout/map_menu_layout.xml @@ -0,0 +1,12 @@ + + + + + + + diff --git a/app/src/main/res/layout/parent_row.xml b/app/src/main/res/layout/parent_row.xml new file mode 100644 index 00000000..fb7f5eb1 --- /dev/null +++ b/app/src/main/res/layout/parent_row.xml @@ -0,0 +1,34 @@ + + + + + + + +