Skip to content

Commit

Permalink
all: smoother adapters (fixes #2012) (#2015)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
Okuro3499 and dogi authored Jun 4, 2024
1 parent 9293980 commit 925f933
Show file tree
Hide file tree
Showing 21 changed files with 85 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ open class BaseInitialActivity: PermissionActivity(), NavigationView.OnNavigatio
*/


override fun sendMessage(s: String) {
override fun sendMessage(s: String?) {
// Check that we're actually connected before trying anything
if (mChatService.state != Constants.STATE_CONNECTED) {
Toast.makeText(this@BaseInitialActivity, R.string.not_connected, Toast.LENGTH_SHORT).show()
return
}

// Check that there's actually something to send
if (s.isNotEmpty()) {
if (s?.isNotEmpty() == true) {
// Get the message bytes and tell the BluetoothChatService to write
val send = s.toByteArray()
mChatService.write(send)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import io.treehouses.remote.R
import io.treehouses.remote.pojo.CommandListItem
import java.util.*

class CommandListAdapter(private val context: Context, private val expandableListTitle: List<String>,
private val expandableListDetail: HashMap<String, List<CommandListItem>>) : BaseExpandableListAdapter() {
class CommandListAdapter(private val context: Context, private val expandableListTitle: List<String>, private val expandableListDetail: HashMap<String, List<CommandListItem>>) : BaseExpandableListAdapter() {
override fun getChild(listPosition: Int, expandedListPosition: Int): Any {
return if (expandedListPosition < expandableListDetail[expandableListTitle[listPosition]]!!.size) {
expandableListDetail[expandableListTitle[listPosition]]!![expandedListPosition].getTitle()
} else "Add"
} else {
"Add"
}
}

override fun getChildId(listPosition: Int, expandedListPosition: Int): Long {
Expand All @@ -38,7 +39,9 @@ class CommandListAdapter(private val context: Context, private val expandableLis
override fun getChildrenCount(listPosition: Int): Int {
return if (listPosition == 0) {
expandableListDetail[expandableListTitle[listPosition]]!!.size + 1
} else expandableListDetail[expandableListTitle[listPosition]]!!.size
} else {
expandableListDetail[expandableListTitle[listPosition]]!!.size
}
}

override fun getGroup(listPosition: Int): Any {
Expand All @@ -53,20 +56,18 @@ class CommandListAdapter(private val context: Context, private val expandableLis
return listPosition.toLong()
}

override fun getGroupView(listPosition: Int, isExpanded: Boolean,
convertView: View?, parent: ViewGroup): View {
override fun getGroupView(listPosition: Int, isExpanded: Boolean, convertView: View?, parent: ViewGroup): View {
var newView = convertView
val listTitle = getGroup(listPosition) as String
if (newView == null) {
newView = getConvertView(R.layout.list_group)
}
val listTitleTextView = newView
.findViewById<View>(R.id.lblListHeader) as TextView
val listTitleTextView = newView.findViewById<View>(R.id.lblListHeader) as TextView
listTitleTextView.text = listTitle
return newView
}

fun getConvertView(layout_id: Int): View {
private fun getConvertView(layout_id: Int): View {
val layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
return layoutInflater.inflate(layout_id, null)
}
Expand All @@ -78,5 +79,4 @@ class CommandListAdapter(private val context: Context, private val expandableLis
override fun isChildSelectable(listPosition: Int, expandedListPosition: Int): Boolean {
return true
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import io.treehouses.remote.databinding.RowHelpBinding
import io.treehouses.remote.pojo.HelpCommand

class HelpAdapter : RecyclerView.Adapter<HelpViewHolder>() {

private val sortedList: SortedList<HelpCommand> = SortedList(HelpCommand::class.java, object : SortedList.Callback<HelpCommand>() {
override fun areItemsTheSame(item1: HelpCommand?, item2: HelpCommand?): Boolean {
return item1?.title == item2?.title && item1?.preview == item2?.preview
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ class ProfilesListAdapter(private val context: Context, private val titles: List
private fun setLabelText(label: TextView, s: String, delete: Button) {
label.text = s
label.setTextColor(ContextCompat.getColor(context, R.color.expandable_child_text))

delete.visibility = View.GONE
}

Expand All @@ -87,20 +86,18 @@ class ProfilesListAdapter(private val context: Context, private val titles: List

private fun showConfirmation(name: String, groupPosition: Int, childPosition: Int) {
val alertDialog = AlertDialog.Builder(ContextThemeWrapper(context, R.style.CustomAlertDialogStyle))
.setTitle("Delete Profile?")
.setMessage("Are you sure you want to delete the following Network Profile: $name")
.setPositiveButton("Delete") { _, _ ->
deleteProfile(context, groupPosition, childPosition)
data[titles[groupPosition]]!!.removeAt(childPosition)
notifyDataSetChanged()
}
.setNegativeButton("Cancel") { dialog, _ -> dialog.dismiss() }.create()
.setTitle("Delete Profile?")
.setMessage("Are you sure you want to delete the following Network Profile: $name")
.setPositiveButton("Delete") { _, _ ->
deleteProfile(context, groupPosition, childPosition)
data[titles[groupPosition]]!!.removeAt(childPosition)
notifyDataSetChanged()
}.setNegativeButton("Cancel") { dialog, _ -> dialog.dismiss() }.create()
alertDialog.window!!.setBackgroundDrawableResource(android.R.color.transparent)
alertDialog.show()
}

override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean {
return true
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ import io.treehouses.remote.pojo.DeviceInfo
class RPIListAdapter(private val mContext: Context, private val data: List<DeviceInfo>) : ArrayAdapter<DeviceInfo?>(mContext, 0, data) {
var deviceListener: DeviceDeleteListener? = null
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
// Get the data item for this position
var newView = convertView
val deviceText = data[position].deviceName

// Check if an existing view is being reused, otherwise inflate the view
if (newView == null) {
newView = LayoutInflater.from(mContext).inflate(R.layout.list_rpi_item, parent, false)
}
Expand All @@ -36,14 +33,10 @@ class RPIListAdapter(private val mContext: Context, private val data: List<Devic
pairedImage.setColorFilter(getTint(data[position]))
deleteDevice.setColorFilter(getTint(data[position]))
}

// Return the completed view to render on screen
return newView
}

private fun getTint(deviceInfo: DeviceInfo): Int {
return if (deviceInfo.isInRange) ContextCompat.getColor(mContext, R.color.md_green_500) else ContextCompat.getColor(mContext, R.color.md_grey_400)
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import io.treehouses.remote.R
import io.treehouses.remote.pojo.ServiceInfo
import java.util.*

class ServicesListAdapter //private Button start, install, restart, link, info;
(private val mContext: Context, private val dataIn: MutableList<ServiceInfo>, private val headerColour: Int) : ArrayAdapter<ServiceInfo>(mContext, 0, dataIn) {
class ServicesListAdapter(mContext: Context, private val dataIn: MutableList<ServiceInfo>, private val headerColour: Int) : ArrayAdapter<ServiceInfo>(mContext, 0, dataIn) {
var data: MutableList<ServiceInfo> = dataIn
private var name: TextView? = null
private var status: ImageView? = null
Expand Down Expand Up @@ -81,8 +80,7 @@ class ServicesListAdapter //private Button start, install, restart, link, info;
}

private fun initView(position: Int, parent: ViewGroup): View {
val convertView: View?
convertView = if (flag(position)) {
val convertView: View = if (flag(position)) {
LayoutInflater.from(context).inflate(R.layout.services_row_layout, parent, false)
} else {
LayoutInflater.from(context).inflate(R.layout.services_section_header, parent, false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ import io.treehouses.remote.R
import io.treehouses.remote.callback.HomeInteractListener
import io.treehouses.remote.pojo.NetworkListItem

class SystemListAdapter(val context: Context, list: List<NetworkListItem>) : BaseExpandableListAdapter() {
private val list: List<NetworkListItem>
class SystemListAdapter(val context: Context, private val list: List<NetworkListItem>) : BaseExpandableListAdapter() {
private val inflater: LayoutInflater = LayoutInflater.from(context)
private var listener: HomeInteractListener? = null
private val views: MutableList<View>
private val groupHeader: MutableList<View>
private val views: MutableList<View> = mutableListOf()
private val groupHeader: MutableList<View> = mutableListOf()
fun setListener(listener: HomeInteractListener?) {
this.listener = listener
if (listener == null) {
Expand Down Expand Up @@ -67,23 +66,20 @@ class SystemListAdapter(val context: Context, list: List<NetworkListItem>) : Bas

@RequiresApi(api = Build.VERSION_CODES.O)
override fun getChildView(i: Int, i1: Int, b: Boolean, convertView: View?, parent: ViewGroup): View {

// Needs to recycle views instead of creating new ones each time.
// if (convertView == null) creating bugs
if (views.size > i) {
return views[i]
}
val newView: View = inflater.inflate(list[i].layout, parent, false)
layout = list[i].layout
position = i
when (layout) {
R.layout.configure_shutdown_reboot -> ViewHolderShutdownReboot(newView, context, listener!!)
R.layout.open_vnc -> ViewHolderVnc(newView, context, listener!!)
R.layout.configure_tethering -> ViewHolderTether(newView, listener!!, context)
R.layout.configure_ssh_key -> ViewHolderSSHKey(newView, context, listener!!)
R.layout.configure_camera -> ViewHolderCamera(newView, context, listener!!)
R.layout.configure_blocker -> ViewHolderBlocker(newView, context, listener!!)
R.layout.configure_ssh2fa -> ViewHolderSSH2FA(newView, context, listener!!)
R.layout.configure_shutdown_reboot -> ViewHolderShutdownReboot(newView, context, listener)
R.layout.open_vnc -> ViewHolderVnc(newView, context, listener)
R.layout.configure_tethering -> ViewHolderTether(newView, listener, context)
R.layout.configure_ssh_key -> ViewHolderSSHKey(newView, context, listener)
R.layout.configure_camera -> ViewHolderCamera(newView, context, listener)
R.layout.configure_blocker -> ViewHolderBlocker(newView, context, listener)
R.layout.configure_ssh2fa -> ViewHolderSSH2FA(newView, context, listener)
}
views.add(newView)
return newView
Expand All @@ -98,10 +94,4 @@ class SystemListAdapter(val context: Context, list: List<NetworkListItem>) : Bas
private set
var position = 0
}

init {
this.list = list
views = mutableListOf()
groupHeader = mutableListOf()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,15 @@ class TerminalPagerAdapter : PagerAdapter() {
override fun instantiateItem(container: ViewGroup, position: Int): Any {
if (terminalPager.getManager() == null || terminalPager.getManager()!!.bridges.size <= position) Log.w(BaseSSHConsole.TAG, "Activity not bound when creating TerminalView.")
val bridge = terminalPager.getManager()!!.bridges[position]
bridge.promptHelper!!.setHandler(terminalPager.getHandler())
bridge.promptHelper?.setHandler(terminalPager.getHandler())

// inflate each terminal view
val view = terminalPager.getInflater().inflate(R.layout.item_terminal, container, false) as RelativeLayout

// set the terminal name overlay text
val terminalNameOverlay = view.findViewById<TextView>(R.id.terminal_name_overlay)
terminalNameOverlay.text = bridge.host!!.nickname

// and add our terminal view control, using index to place behind overlay
terminalNameOverlay.text = bridge.host?.nickname
val terminal = TerminalView(container.context, bridge, terminalPager.getPager())
terminal.id = R.id.terminal_view
view.addView(terminal, 0)

// Tag the view with its bridge so it can be retrieved later.
view.tag = bridge
container.addView(view)
terminalNameOverlay.startAnimation(terminalPager.getAnimation())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@ import android.widget.ArrayAdapter
import android.widget.TextView
import io.treehouses.remote.R


class TunnelPortAdapter(private val mContext: Context, private val data: List<String?>) : ArrayAdapter<String?>(mContext, 0, data) {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {

val convertedView: View
val deleteAllPortsButtonSelected = data.size > 1 && position == data.size-1
convertedView = if (deleteAllPortsButtonSelected) LayoutInflater.from(mContext).inflate(R.layout.select_dialog_item_delete_all, parent, false)
else LayoutInflater.from(mContext).inflate(R.layout.select_dialog_item, parent, false)
val text = convertedView as TextView
text.text = getItem(position)
// Return the completed view to render on screen
return convertedView
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import io.treehouses.remote.R
import io.treehouses.remote.callback.HomeInteractListener
import io.treehouses.remote.network.BluetoothChatService

class ViewHolderBlocker internal constructor(v: View, context: Context?, listener: HomeInteractListener) {
class ViewHolderBlocker internal constructor(v: View, context: Context?, listener: HomeInteractListener?) {
private val blockerBar: SeekBar = v.findViewById(R.id.blockerBar)
private val levelText: TextView = v.findViewById(R.id.levelText)
private val blockerDesc: TextView = v.findViewById(R.id.blockerDesc)
Expand All @@ -23,11 +23,8 @@ class ViewHolderBlocker internal constructor(v: View, context: Context?, listene
private var blockerLevel:Int = 0
private var previousLevel:Int = 0
private var readMessage = ""
private val mChatService: BluetoothChatService = listener.getChatService()
private val mChatService: BluetoothChatService? = listener?.getChatService()

/**
* The Handler that gets information back from the BluetoothChatService
*/
val mHandler: Handler = object : Handler(Looper.getMainLooper()) {
override fun handleMessage(msg: Message) {
when (msg.what) {
Expand Down Expand Up @@ -56,10 +53,9 @@ class ViewHolderBlocker internal constructor(v: View, context: Context?, listene
}
}


init {
mChatService.updateHandler(mHandler)
listener.sendMessage(context!!.resources.getString(R.string.TREEHOUSES_BLOCKER_CHECK))
mChatService?.updateHandler(mHandler)
listener?.sendMessage(context!!.resources.getString(R.string.TREEHOUSES_BLOCKER_CHECK))
blockerBar.max = 5
setBlocker.visibility = View.GONE
moveText.visibility = View.VISIBLE
Expand Down Expand Up @@ -88,7 +84,7 @@ class ViewHolderBlocker internal constructor(v: View, context: Context?, listene

fun setBlocker(level:Int) {
fun sendCommand(level:String) {
listener.sendMessage(context.resources.getString(R.string.TREEHOUSES_BLOCKER, level))
listener?.sendMessage(context?.resources?.getString(R.string.TREEHOUSES_BLOCKER, level))
}

when (level) {
Expand Down Expand Up @@ -140,6 +136,4 @@ class ViewHolderBlocker internal constructor(v: View, context: Context?, listene
fun Context?.toast(s: String): Toast {
return Toast.makeText(this, s, Toast.LENGTH_SHORT).apply { show() }
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import android.os.Looper
import android.os.Message
import android.view.View
import android.widget.ImageView
import android.widget.Switch
import android.widget.Toast
import androidx.appcompat.widget.SwitchCompat
import io.treehouses.remote.Constants
import io.treehouses.remote.network.BluetoothChatService
import io.treehouses.remote.R
import io.treehouses.remote.callback.HomeInteractListener
import io.treehouses.remote.network.BluetoothChatService

class ViewHolderCamera internal constructor(v: View, private val c: Context, listener: HomeInteractListener) {
private val mChatService: BluetoothChatService = listener.getChatService()
private val cameraSwitch: Switch
class ViewHolderCamera internal constructor(v: View, private val c: Context, listener: HomeInteractListener?) {
private val mChatService: BluetoothChatService? = listener?.getChatService()
private val cameraSwitch: SwitchCompat
private val icon: ImageView
private val mHandler: Handler = object : Handler(Looper.getMainLooper()) {
override fun handleMessage(msg: Message) {
Expand All @@ -40,14 +40,14 @@ class ViewHolderCamera internal constructor(v: View, private val c: Context, lis
}

init {
mChatService.updateHandler(mHandler)
mChatService?.updateHandler(mHandler)
cameraSwitch = v.findViewById(R.id.CameraSwitch)
icon = v.findViewById(R.id.cameraIcon)
listener.sendMessage(c.resources.getString(R.string.TREEHOUSES_CAMERA))
listener?.sendMessage(c.resources.getString(R.string.TREEHOUSES_CAMERA))
cameraSwitch.isEnabled = false
cameraSwitch.setOnClickListener {
if (cameraSwitch.isChecked) listener.sendMessage(c.resources.getString(R.string.TREEHOUSES_CAMERA_ON))
else listener.sendMessage(c.resources.getString(R.string.TREEHOUSES_CAMERA_OFF))
if (cameraSwitch.isChecked) listener?.sendMessage(c.resources.getString(R.string.TREEHOUSES_CAMERA_ON))
else listener?.sendMessage(c.resources.getString(R.string.TREEHOUSES_CAMERA_OFF))
cameraSwitch.isEnabled = false
}
}
Expand Down
Loading

0 comments on commit 925f933

Please sign in to comment.