Skip to content

Commit

Permalink
Implement better row sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
OwenMcGirr committed Mar 6, 2024
1 parent c56880d commit 478d103
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,12 @@ class MenuItem(
view?.getLocationOnScreen(location)
return location[1]
}

override fun getWidth(): Int {
return view?.width ?: 0
}

override fun getHeight(): Int {
return view?.height ?: 0
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class MenuView(
baseLayout.addView(menuPages[currentPage].getMenuLayout())
// Build the scan tree after half a second
Handler(Looper.getMainLooper()).postDelayed({
scanTree.buildTree(menuPages[currentPage].getMenuItems())
scanTree.buildTree(menuPages[currentPage].getMenuItems(), 0)
}, 500)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ class Node : ScanNodeInterface {
return y
}

override fun getWidth(): Int {
return width
}

override fun getHeight(): Int {
return height
}

fun getCenterX(): Int {
return centerX
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ interface ScanNodeInterface {
*/
fun getY(): Int

/**
* This function gets the width of the node
* @return The width of the node
*/
fun getWidth(): Int

/**
* This function gets the height of the node
* @return The height of the node
*/
fun getHeight(): Int

/**
* This function highlights the node
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.enaboapps.switchify.service.scanning

import android.content.Context
import android.util.Log
import com.enaboapps.switchify.service.utils.ScreenUtils
import kotlin.math.abs

class ScanTree(
private val context: Context,
Expand Down Expand Up @@ -61,28 +63,67 @@ class ScanTree(
/**
* This function builds the scanning tree
* by examining the x and y coordinates of the nodes
* and organizing them into a tree of rows
* and organizing them into a tree of rows and columns
* @param nodes The nodes to build the tree from
* @param rowThreshold The threshold for determining if a node is in a row
*/
fun buildTree(nodes: List<ScanNodeInterface>) {
fun buildTree(nodes: List<ScanNodeInterface>, rowThreshold: Int = 100) {
reset()
clearTree()
if (nodes.isNotEmpty()) {
var currentRow = mutableListOf<ScanNodeInterface>()
var currentY = nodes[0].getY()
for (node in nodes) {
if (node.getY() != currentY) {
addRow(currentRow)
currentRow = mutableListOf()
currentY = node.getY()
// Initial sort of nodes by their Y position to process from top to bottom.
val sortedNodes = nodes.sortedBy { it.getY() }

// Initialize the list for the first row with the first node.
var currentRow = mutableListOf<ScanNodeInterface>(sortedNodes.first())
// Set the Y position baseline for the first row.
var currentYBaseline = sortedNodes.first().getY()

// Get screen dimensions.
val screenWidth = ScreenUtils.getWidth(context)
val screenHeight = ScreenUtils.getHeight(context)

// Function to add the node to the current row.
val addNodeToRow: (ScanNodeInterface) -> Unit = { node ->
// If node is under 80% of the screen dimensions
// And larger than 0, add it to the current row
val width = node.getWidth()
val height = node.getHeight()
val isCloseToFullScreen = width > 0.8 * screenWidth && height > 0.8 * screenHeight
val isBiggerThanZero = width > 0 && height > 0
if (!isCloseToFullScreen && isBiggerThanZero) {
currentRow.add(node)
}
currentRow.add(node)
}
addRow(currentRow)
}

// sort the rows by the y coordinate
tree = tree.sortedBy { it.y }.toMutableList()
// Start iterating from the second node since the first is already included.
for (node in sortedNodes.drop(1)) {
// Determine if the current node's Y position is within the threshold of the current row's baseline.
if (abs(node.getY() - currentYBaseline) <= rowThreshold) {
// Node is close enough to be considered part of the current row.
addNodeToRow(node)
} else {
// Node is too far from the current row's baseline, indicating a new row.
// Process the current row before starting a new one.
if (currentRow.isNotEmpty()) {
// Remove duplicates from the row.
currentRow = currentRow.distinct().toMutableList()
// Add the current row to the scanning tree.
addRow(currentRow)
// Clear the current row for the next iteration.
currentRow = mutableListOf()
}
// Add the current node to the new row and update the baseline Y position.
addNodeToRow(node)
currentYBaseline = node.getY()
}
}

// Ensure the last row is added after processing all nodes.
if (currentRow.isNotEmpty()) {
addRow(currentRow)
}
}

setupScanningScheduler()
}
Expand All @@ -92,8 +133,10 @@ class ScanTree(
* @param row The row to add
*/
private fun addRow(row: List<ScanNodeInterface>) {
val sortedRow = row.sortedBy { it.getX() }
tree.add(Row(sortedRow, sortedRow[0].getY()))
if (row.isNotEmpty()) {
val sortedRow = row.sortedBy { it.getX() }
tree.add(Row(sortedRow, sortedRow[0].getY()))
}
}

/**
Expand Down

0 comments on commit 478d103

Please sign in to comment.