Mobile Apps 2023W Lecture 12

From Soma-notes
Revision as of 20:38, 17 February 2023 by Soma (talk | contribs)

Resources

Notes

To get the ScrollDB app working, you need to (work in progress):

  • Create an empty Compose app at the latest API level, 33
  • In build.gradle (Project), add
 room_version = '2.5.0'

To the "ext" block at the top.

  • In build.gradle (Module), add


Code

package carleton.comp2601.scrolldemo

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import carleton.comp2601.scrolldemo.database.AppDatabase
import carleton.comp2601.scrolldemo.ui.theme.ScrollDemoTheme

class MainActivity : ComponentActivity() {
    //val database: AppDatabase by lazy { AppDatabase.getDatabase(this) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ScrollDemoTheme {
                // A surface container using the 'background' color from the theme
                ScrollList()
                    //ScrollList(database)
            }
        }
    }
}

@Composable
fun ScrollList() {
//fun ScrollList(database: AppDatabase) {

    val listState = rememberLazyListState()

    Surface(
        modifier = Modifier.fillMaxSize(),
        color = MaterialTheme.colors.background
    ) {
        Scaffold(
            topBar = {
                TopAppBar(
                    backgroundColor = MaterialTheme.colors.primary,
                    title = { Text("Scroll Demo") }
                )
            },
            content = {
                LazyColumn(
                    state = listState,
                    modifier = Modifier.fillMaxWidth(),
                    contentPadding = PaddingValues(16.dp),
                    verticalArrangement = Arrangement.spacedBy(16.dp),
                ) {
                    items(20000) { index ->
                        Text(text = "Item: $index")
                    }
                }
            }
        )
    }
}

//@Preview(showBackground = true)
//@Composable
//fun DefaultPreview() {
//    ScrollDemoTheme {
//        ScrollList()
//    }
//}