|
| 1 | +/* |
| 2 | + * Copyright 2019 Google, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.jetnews.ui |
| 18 | + |
| 19 | +import androidx.compose.Composable |
| 20 | +import androidx.compose.composer |
| 21 | +import androidx.compose.frames.open |
| 22 | +import androidx.compose.state |
| 23 | +import androidx.compose.unaryPlus |
| 24 | +import androidx.ui.core.Text |
| 25 | +import androidx.ui.core.dp |
| 26 | +import androidx.ui.layout.Column |
| 27 | +import androidx.ui.layout.CrossAxisAlignment |
| 28 | +import androidx.ui.layout.HeightSpacer |
| 29 | +import androidx.ui.layout.LayoutSize |
| 30 | +import androidx.ui.layout.MainAxisAlignment |
| 31 | +import androidx.ui.layout.Padding |
| 32 | +import androidx.ui.layout.Row |
| 33 | +import androidx.ui.material.Button |
| 34 | +import androidx.ui.material.DrawerState |
| 35 | +import androidx.ui.material.MaterialTheme |
| 36 | +import androidx.ui.material.ModalDrawerLayout |
| 37 | +import androidx.ui.material.TextButtonStyle |
| 38 | + |
| 39 | +@Composable |
| 40 | +fun JetnewsApp(icons: Icons) { |
| 41 | + |
| 42 | + val (drawerState, onDrawerStateChange) = +state { DrawerState.Closed } |
| 43 | + |
| 44 | + MaterialTheme( |
| 45 | + colors = lightThemeColors, |
| 46 | + typography = typography |
| 47 | + ) { |
| 48 | + ModalDrawerLayout( |
| 49 | + drawerState = drawerState, |
| 50 | + onStateChange = onDrawerStateChange, |
| 51 | + drawerContent = { AppDrawer(closeDrawer = { onDrawerStateChange(DrawerState.Closed) }) }, |
| 52 | + bodyContent = { |
| 53 | + AppContent( |
| 54 | + icons = icons, |
| 55 | + openDrawer = { onDrawerStateChange(DrawerState.Opened) }) |
| 56 | + } |
| 57 | + ) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +@Composable |
| 62 | +private fun DrawerButton(drawerButtonText: String, action: () -> Unit) { |
| 63 | + Button(onClick = { action() }, style = TextButtonStyle()) { |
| 64 | + Row( |
| 65 | + mainAxisAlignment = MainAxisAlignment.Start, |
| 66 | + mainAxisSize = LayoutSize.Expand |
| 67 | + ) { |
| 68 | + Text(drawerButtonText) |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +@Composable |
| 74 | +private fun AppContent(icons: Icons, openDrawer: () -> Unit) { |
| 75 | + val screen = JetnewsStatus.currentScreen |
| 76 | + when (screen) { |
| 77 | + is Screen.Home -> HomeScreen(icons = icons, openDrawer = { openDrawer() }) |
| 78 | + is Screen.Interests -> TopicsScreen(icons = icons, openDrawer = { openDrawer() }) |
| 79 | + is Screen.Article -> ArticleScreen(icons = icons, postId = screen.postId) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +@Composable |
| 84 | +private fun AppDrawer(closeDrawer: () -> Unit) { |
| 85 | + Column( |
| 86 | + crossAxisAlignment = CrossAxisAlignment.Start, |
| 87 | + crossAxisSize = LayoutSize.Expand, |
| 88 | + mainAxisAlignment = MainAxisAlignment.Start, |
| 89 | + mainAxisSize = LayoutSize.Expand |
| 90 | + ) { |
| 91 | + HeightSpacer(40.dp) |
| 92 | + Padding(8.dp) { |
| 93 | + DrawerButton("Home") { |
| 94 | + navigateTo(Screen.Home) |
| 95 | + closeDrawer() |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + Padding(8.dp) { |
| 100 | + DrawerButton("Interests") { |
| 101 | + navigateTo(Screen.Interests) |
| 102 | + closeDrawer() |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments