Mobile Apps 2023W Lecture 18
Revision as of 16:52, 17 March 2023 by Soma (talk | contribs) (Created page with "==Code== ===MainActivity.kt=== <syntaxhighlight lang="kotlin" line> package carleton.comp2601.historyviewer import android.net.Uri import android.os.Bundle import android.util.Log import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.material.MaterialTheme import androidx.compose.material.Surface import andro...")
Code
MainActivity.kt
package carleton.comp2601.historyviewer
import android.net.Uri
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import carleton.comp2601.historyviewer.ui.theme.HistoryViewerTheme
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.text.ClickableText
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.ui.text.AnnotatedString
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
var hnTreeRoot = HNTree()
hnTreeRoot.add("www.carleton.ca")
hnTreeRoot.add("www.google.com")
hnTreeRoot.add("www.cbc.ca.")
hnTreeRoot.add("www.mit.edu")
hnTreeRoot.add("www.google.com")
setContent {
HistoryViewerTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
Start(hnTreeRoot)
}
}
}
}
}
@Composable
fun Start(hnTreeRoot: HNTree) {
val allNodes = remember {
mutableStateListOf<HNTree>()
}
hnTreeRoot.addMyNodes(allNodes)
LazyColumn {
item {
Text(text = "HOSTS:")
}
items(allNodes) { child ->
val spacing = " ".repeat(child.treeLevel)
val nodeName = child.hostSuffix
ClickableText(text = AnnotatedString("$spacing$nodeName"), onClick = {
child.toggleExpanded()
allNodes.clear()
hnTreeRoot.addMyNodes(allNodes)
Log.d("HistoryViewer","toggled node on $nodeName")
})
}
}
}
HNTree.kt
package carleton.comp2601.historyviewer
import android.util.Log
class HNTree {
var hostComponent: String = ""
var hostSuffix: String = ""
var parent: HNTree = this
var children: MutableList<HNTree> = mutableListOf()
var treeLevel: Int = 0
var expanded = true
fun add(hostPrefix: String) {
var allComponents: List<String>
var component: String
var child: HNTree? = null
/* still have to deal with getting to the end */
allComponents = hostPrefix.split(".")
if (allComponents.last() == "") {
allComponents = allComponents.dropLast(1)
}
component = allComponents.last()
for (c in this.children) {
if (c.hostComponent == component) {
child = c
break
}
}
if (child == null) {
child = HNTree()
child.parent = this
child.hostComponent = component
child.hostSuffix = "$component.$hostSuffix"
child.treeLevel = this.treeLevel + 1
children.add(child)
}
val hostRemainder = allComponents.dropLast(1).joinToString(".")
Log.d("HNTree", "$hostSuffix adding child $hostRemainder")
if (component != hostPrefix) {
child.add(hostRemainder)
}
}
fun addMyNodes(nodes: MutableList<HNTree>) {
nodes.add(this)
if (expanded) {
//Log.d("HNTree", "$hostSuffix is expanded " )
for (n in children) {
n.addMyNodes(nodes)
}
}
//Log.d("HNTree", "$hostSuffix added " + nodes.count() + " nodes." )
}
fun toggleExpanded() {
expanded = !expanded
}
}