102 lines
3.3 KiB
Kotlin
102 lines
3.3 KiB
Kotlin
/*
|
|
* Copyright (C) 2023 The Android Open Source Project
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
package com.example.superheroes
|
|
|
|
import android.os.Bundle
|
|
import androidx.activity.ComponentActivity
|
|
import androidx.activity.compose.setContent
|
|
import androidx.compose.foundation.layout.fillMaxSize
|
|
import androidx.compose.foundation.layout.padding
|
|
import androidx.compose.material3.CenterAlignedTopAppBar
|
|
import androidx.compose.material3.ExperimentalMaterial3Api
|
|
import androidx.compose.material3.MaterialTheme
|
|
import androidx.compose.material3.Scaffold
|
|
import androidx.compose.material3.Surface
|
|
import androidx.compose.material3.Text
|
|
import androidx.compose.runtime.Composable
|
|
import androidx.compose.ui.Modifier
|
|
import androidx.compose.ui.res.stringResource
|
|
import androidx.compose.ui.tooling.preview.Preview
|
|
import com.example.superheroes.model.HeroesRepository
|
|
import com.example.superheroes.ui.theme.SuperheroesTheme
|
|
|
|
class MainActivity : ComponentActivity() {
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
setContent {
|
|
SuperheroesTheme {
|
|
// A surface container using the 'background' color from the theme
|
|
Surface(
|
|
modifier = Modifier.fillMaxSize(),
|
|
color = MaterialTheme.colorScheme.background
|
|
) {
|
|
SuperheroesApp()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Composable that displays an app bar and a list of heroes.
|
|
*/
|
|
@Composable
|
|
fun SuperheroesApp() {
|
|
Scaffold(
|
|
modifier = Modifier.fillMaxSize(),
|
|
topBar = {
|
|
TopAppBar()
|
|
}
|
|
) {
|
|
/* Important: It is not a good practice to access data source directly from the UI.
|
|
In later units you will learn how to use ViewModel in such scenarios that takes the
|
|
data source as a dependency and exposes heroes.
|
|
*/
|
|
val heroes = HeroesRepository.heroes
|
|
HeroesList(heroes = heroes, Modifier.padding(it))
|
|
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Composable that displays a Top Bar with an icon and text.
|
|
*
|
|
* @param modifier modifiers to set to this composable
|
|
*/
|
|
@OptIn(ExperimentalMaterial3Api::class)
|
|
@Composable
|
|
fun TopAppBar(modifier: Modifier = Modifier) {
|
|
CenterAlignedTopAppBar(
|
|
title = {
|
|
Text(
|
|
text = stringResource(R.string.app_name),
|
|
style = MaterialTheme.typography.displayLarge,
|
|
)
|
|
},
|
|
modifier = modifier
|
|
)
|
|
}
|
|
|
|
@Preview(showBackground = true)
|
|
@Composable
|
|
fun SuperHeroesPreview() {
|
|
SuperheroesTheme {
|
|
SuperheroesApp()
|
|
}
|
|
}
|
|
}
|
|
|