-
Notifications
You must be signed in to change notification settings - Fork 29
Перевод приложения с Fragment/View на Compose #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
malinaalina
wants to merge
1
commit into
Otus-Android:master
Choose a base branch
from
malinaalina:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,15 @@ | ||
| package ru.otus.marketsample | ||
|
|
||
| import android.os.Bundle | ||
| import androidx.activity.enableEdgeToEdge | ||
| import androidx.activity.compose.setContent | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import androidx.core.view.ViewCompat | ||
| import androidx.core.view.WindowInsetsCompat | ||
| import ru.otus.marketsample.databinding.ActivityMainBinding | ||
|
|
||
| class MainActivity : AppCompatActivity() { | ||
|
|
||
| private lateinit var binding: ActivityMainBinding | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| enableEdgeToEdge() | ||
| super.onCreate(savedInstanceState) | ||
| binding = ActivityMainBinding.inflate(layoutInflater) | ||
| setContentView(binding.root) | ||
|
|
||
| ViewCompat.setOnApplyWindowInsetsListener(binding.container) { view, insets -> | ||
| val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()) | ||
| view.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom) | ||
| insets | ||
| val appComponent = (applicationContext as MarketSampleApp).appComponent | ||
| setContent { | ||
| MainContent(appComponent) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| package ru.otus.marketsample | ||
|
|
||
| import androidx.compose.foundation.layout.padding | ||
| import androidx.compose.foundation.layout.size | ||
| import androidx.compose.material3.Icon | ||
| import androidx.compose.material3.NavigationBar | ||
| import androidx.compose.material3.NavigationBarItem | ||
| import androidx.compose.material3.NavigationBarItemDefaults | ||
| import androidx.compose.material3.Scaffold | ||
| import androidx.compose.material3.Text | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.getValue | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.graphics.Color | ||
| import androidx.compose.ui.graphics.vector.ImageVector | ||
| import androidx.compose.ui.res.colorResource | ||
| import androidx.compose.ui.res.stringResource | ||
| import androidx.compose.ui.res.vectorResource | ||
| import androidx.compose.ui.unit.dp | ||
| import androidx.navigation.NavController | ||
| import androidx.navigation.NavDestination.Companion.hierarchy | ||
| import androidx.navigation.NavGraph.Companion.findStartDestination | ||
| import androidx.navigation.compose.NavHost | ||
| import androidx.navigation.compose.composable | ||
| import androidx.navigation.compose.currentBackStackEntryAsState | ||
| import androidx.navigation.compose.rememberNavController | ||
| import ru.otus.marketsample.common.Screen | ||
| import ru.otus.marketsample.details.feature.DetailsScreen | ||
| import ru.otus.marketsample.di.AppComponent | ||
| import ru.otus.marketsample.products.feature.ProductsScreen | ||
| import ru.otus.marketsample.promo.feature.PromoScreen | ||
|
|
||
| @Composable | ||
| fun MainContent(appComponent: AppComponent) { | ||
| val navController = rememberNavController() | ||
| val navBackStackEntry by navController.currentBackStackEntryAsState() | ||
| val currentRoute = navBackStackEntry?.destination?.route | ||
| val items = listOf(Screen.Products, Screen.Promo) | ||
| Scaffold( | ||
| bottomBar = { | ||
| if (currentRoute != Screen.Details.route) { | ||
| BottomBar( | ||
| navController = navController, | ||
| items = items, | ||
| ) | ||
| } | ||
| } | ||
| ) { innerPadding -> | ||
| NavHost( | ||
| navController = navController, | ||
| startDestination = Screen.Products.route, | ||
| modifier = Modifier.padding(innerPadding) | ||
| ) { | ||
| composable(Screen.Products.route) { | ||
| ProductsScreen( | ||
| appComponent = appComponent, | ||
| navController = navController, | ||
| ) | ||
| } | ||
| composable(Screen.Promo.route) { | ||
| PromoScreen(appComponent) | ||
| } | ||
| composable(Screen.Details.route) { backStackEntry -> | ||
| val productId = backStackEntry.arguments?.getString(Screen.Details.PRODUCT_ID) ?: "" | ||
| DetailsScreen( | ||
| appComponent = appComponent, | ||
| productId = productId | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| @Composable | ||
| fun BottomBar( | ||
| items: List<Screen>, | ||
| navController: NavController, | ||
| ) { | ||
| NavigationBar { | ||
| val navBackStackEntry by navController.currentBackStackEntryAsState() | ||
| val currentDestination = navBackStackEntry?.destination | ||
|
|
||
| items.forEach { screen -> | ||
| NavigationBarItem( | ||
| icon = { | ||
| Icon( | ||
| ImageVector.vectorResource( | ||
| screen.iconRes | ||
| ), | ||
| modifier = Modifier.size(24.dp), | ||
| contentDescription = null | ||
| ) | ||
| }, | ||
| label = { Text(stringResource(screen.titleRes)) }, | ||
| selected = currentDestination?.hierarchy?.any { it.route == screen.route } == true, | ||
| colors = NavigationBarItemDefaults.colors( | ||
| selectedIconColor = colorResource(id = ru.otus.common.ui.R.color.purple_500), | ||
| selectedTextColor = colorResource(id = ru.otus.common.ui.R.color.purple_500), | ||
| indicatorColor = Color.Transparent, | ||
| unselectedIconColor = Color.Black, | ||
| unselectedTextColor = Color.Black, | ||
| ), | ||
| onClick = { | ||
| navController.navigate(screen.route) { | ||
| popUpTo(navController.graph.findStartDestination().id) { saveState = true } | ||
| launchSingleTop = true | ||
| restoreState = true | ||
| } | ||
| } | ||
| ) | ||
| } | ||
| } | ||
| } |
This file was deleted.
Oops, something went wrong.
73 changes: 73 additions & 0 deletions
73
app/src/main/java/ru/otus/marketsample/common/DiscountBadge.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package ru.otus.marketsample.common | ||
|
|
||
| import androidx.compose.foundation.layout.Box | ||
| import androidx.compose.foundation.layout.padding | ||
| import androidx.compose.material3.Text | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.ui.Alignment | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.draw.drawWithCache | ||
| import androidx.compose.ui.geometry.CornerRadius | ||
| import androidx.compose.ui.geometry.Offset | ||
| import androidx.compose.ui.geometry.Rect | ||
| import androidx.compose.ui.geometry.RoundRect | ||
| import androidx.compose.ui.graphics.Brush | ||
| import androidx.compose.ui.graphics.Color | ||
| import androidx.compose.ui.graphics.Path | ||
| import androidx.compose.ui.graphics.drawscope.Stroke | ||
| import androidx.compose.ui.res.colorResource | ||
| import androidx.compose.ui.text.font.FontWeight | ||
| import androidx.compose.ui.unit.dp | ||
| import androidx.compose.ui.unit.sp | ||
|
|
||
| @Composable | ||
| fun DiscountBadge( | ||
| modifier: Modifier = Modifier, | ||
| discountText: String, | ||
| ) { | ||
| val purple200 = colorResource(id = ru.otus.common.ui.R.color.purple_200) | ||
| val purple500 = colorResource(id = ru.otus.common.ui.R.color.purple_500) | ||
| Box( | ||
| modifier = modifier | ||
| .padding(8.dp) | ||
| .drawWithCache { | ||
| val cornerRadius = 40.dp.toPx() | ||
| val topRightRadius = 10.dp.toPx() | ||
| val outlinePath = Path().apply { | ||
| addRoundRect( | ||
| RoundRect( | ||
| rect = Rect(Offset.Zero, size), | ||
| topLeft = CornerRadius(cornerRadius), | ||
| topRight = CornerRadius(topRightRadius), | ||
| bottomRight = CornerRadius(cornerRadius), | ||
| bottomLeft = CornerRadius(cornerRadius) | ||
| ) | ||
| ) | ||
| } | ||
| val gradientBrush = Brush.linearGradient( | ||
| colors = listOf(purple200, purple500), | ||
| start = Offset(0f, size.height), | ||
| end = Offset(size.width, 0f) | ||
| ) | ||
|
|
||
| onDrawWithContent { | ||
| drawPath(path = outlinePath, brush = gradientBrush) | ||
| drawPath( | ||
| path = outlinePath, | ||
| color = Color.White, | ||
| style = Stroke(width = 2.dp.toPx()) | ||
| ) | ||
| drawContent() | ||
| } | ||
| }, | ||
| contentAlignment = Alignment.Center | ||
| ) { | ||
| Text( | ||
| modifier = Modifier.padding(horizontal = 10.dp, vertical = 4.dp), | ||
| text = discountText, | ||
| color = Color.White, | ||
| fontSize = 14.sp, | ||
| fontWeight = FontWeight.Bold, | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package ru.otus.marketsample.common | ||
|
|
||
| import androidx.compose.foundation.layout.Box | ||
| import androidx.compose.foundation.layout.fillMaxSize | ||
| import androidx.compose.material3.CircularProgressIndicator | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.ui.Alignment | ||
| import androidx.compose.ui.Modifier | ||
|
|
||
| @Composable | ||
| fun Loader( | ||
| modifier: Modifier = Modifier, | ||
| ) { | ||
| Box( | ||
| modifier = modifier.fillMaxSize(), | ||
| contentAlignment = Alignment.Center | ||
| ) { | ||
| CircularProgressIndicator() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package ru.otus.marketsample.common | ||
|
|
||
| import ru.otus.marketsample.R | ||
|
|
||
| sealed class Screen(val route: String, val titleRes: Int, val iconRes: Int) { | ||
| object Products : Screen("products_fragment", R.string.title_products, ru.otus.common.ui.R.drawable.ic_list) | ||
| object Promo : Screen("promo_fragment", R.string.title_promo, ru.otus.common.ui.R.drawable.ic_discount) | ||
| object Details : Screen("details/{productId}", 0, 0) { | ||
| const val PRODUCT_ID = "productId" | ||
| fun createRoute(productId: String) = "details/$productId" | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
по конвенции, аргументы без дефолтного значения должны идти первыми