Initial commit
This commit is contained in:
96
app/src/main/java/com/example/superheroes/MainActivity.kt
Normal file
96
app/src/main/java/com/example/superheroes/MainActivity.kt
Normal file
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright (c) 2022 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
|
||||
*
|
||||
* http://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.Box
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.material.MaterialTheme
|
||||
import androidx.compose.material.Scaffold
|
||||
import androidx.compose.material.Text
|
||||
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.tooling.preview.Preview
|
||||
import androidx.compose.ui.unit.dp
|
||||
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 {
|
||||
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
|
||||
*/
|
||||
@Composable
|
||||
fun TopAppBar(modifier: Modifier = Modifier) {
|
||||
Box(
|
||||
modifier = modifier
|
||||
.fillMaxWidth()
|
||||
.size(56.dp),
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
Text(
|
||||
text = stringResource(R.string.app_name),
|
||||
style = MaterialTheme.typography.h1,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Preview(showBackground = true)
|
||||
@Composable
|
||||
fun SuperHeroesPreview() {
|
||||
SuperheroesTheme {
|
||||
SuperheroesApp()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user