Skip to content

Commit ce99939

Browse files
committedSep 24, 2019
Addressing previous JetNews comments
Main changes: * Move composables, Icons and app status to different files * Variables renamed Change-Id: I9b9d8fc5a9db36dd83ae87013870eac9aad5a5b0
1 parent cfc9d58 commit ce99939

17 files changed

+292
-232
lines changed
 

‎JetNews/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#JetNews sample
1+
#Jetnews sample
22

33
### License
44

‎JetNews/app/build.gradle

+1-8
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
1818

1919
apply plugin: 'com.android.application'
2020
apply plugin: 'kotlin-android'
21-
apply plugin: 'kotlin-android-extensions'
2221

2322
Configuration kotlinPluginConfiguration = configurations.create("kotlinPlugin")
2423
tasks.withType(KotlinCompile).configureEach { compile ->
@@ -78,16 +77,10 @@ dependencies {
7877
implementation ('androidx.ui:ui-material:0.1.0-SNAPSHOT')
7978
implementation ('androidx.ui:ui-android-view:0.1.0-SNAPSHOT')
8079
implementation ('androidx.ui:ui-platform:0.1.0-SNAPSHOT')
81-
implementation ('androidx.lifecycle:lifecycle-viewmodel-ktx:2.0.0')
8280
implementation ('androidx.appcompat:appcompat:1.1.0-beta01')
8381
implementation ('androidx.activity:activity-ktx:1.0.0-beta01')
84-
implementation ('androidx.lifecycle:lifecycle-extensions:2.0.0')
8582

86-
implementation "androidx.core:core-ktx:1.0.2"
87-
implementation "androidx.lifecycle:lifecycle-viewmodel:2.1.0-beta01"
88-
implementation "androidx.lifecycle:lifecycle-extensions:2.1.0-beta01"
89-
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0-beta01"
90-
implementation "com.google.code.gson:gson:2.8.5"
83+
implementation "androidx.core:core-ktx:1.1.0"
9184

9285
implementation fileTree(dir: 'libs', include: ['*.aar'])
9386
implementation "androidx.ui:ui-tooling:+"

‎JetNews/app/src/main/AndroidManifest.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
android:label="@string/app_name"
2525
android:roundIcon="@mipmap/ic_launcher_round"
2626
android:supportsRtl="true"
27-
android:theme="@style/AppTheme">
27+
android:theme="@style/Theme.Jetnews">
2828
<activity android:name=".ui.MainActivity">
2929
<intent-filter>
3030
<action android:name="android.intent.action.MAIN"/>

‎JetNews/app/src/main/java/com/example/jetnews/ui/HomeScreen.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fun HomeScreen(icons: Icons, openDrawer: () -> Unit) {
5353
FlexColumn {
5454
inflexible {
5555
TopAppBar<Any>(
56-
{ Text("JetNews") },
56+
{ Text("Jetnews") },
5757
navigationIcon = navigationIcon
5858
)
5959
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 android.content.res.Resources
20+
import androidx.ui.graphics.imageFromResource
21+
import com.example.jetnews.R
22+
import kotlin.LazyThreadSafetyMode.NONE
23+
24+
/**
25+
* Defining and loading here all the images that we use from resources.
26+
*/
27+
class Icons(resources: Resources) {
28+
val appLogo by lazy(NONE) { imageFromResource(resources, R.drawable.jp_news_logo) }
29+
val home by lazy(NONE) { imageFromResource(resources, R.drawable.baseline_home_black_24dp) }
30+
val menu by lazy(NONE) { imageFromResource(resources, R.drawable.baseline_menu_white_24dp) }
31+
val more by lazy(NONE) {
32+
imageFromResource(resources, R.drawable.baseline_more_vert_black_24dp)
33+
}
34+
val back by lazy(NONE) {
35+
imageFromResource(resources, R.drawable.baseline_arrow_back_white_24dp)
36+
}
37+
val bookmarkOn by lazy(NONE) {
38+
imageFromResource(resources, R.drawable.baseline_bookmark_black_24dp)
39+
}
40+
val bookmarkOff by lazy(NONE) {
41+
imageFromResource(resources, R.drawable.baseline_bookmark_border_black_24dp)
42+
}
43+
val heartOn by lazy(NONE) {
44+
imageFromResource(resources, R.drawable.baseline_favorite_black_24dp)
45+
}
46+
val heartOff by lazy(NONE) {
47+
imageFromResource(resources, R.drawable.baseline_favorite_border_black_24dp)
48+
}
49+
val share by lazy(NONE) { imageFromResource(resources, R.drawable.baseline_share_black_24dp) }
50+
val placeholder_1_1 by lazy(NONE) { imageFromResource(resources, R.drawable.placeholder_1_1) }
51+
val placeholder_4_3 by lazy(NONE) { imageFromResource(resources, R.drawable.placeholder_4_3) }
52+
}

‎JetNews/app/src/main/java/com/example/jetnews/ui/ImageUtils.kt

+8-8
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,19 @@ fun ZoomedClippedImage(image: Image) {
4848
val drawHeight = imWidth / pAspectRatio
4949
val drawWidth = imWidth
5050
Rect(
51-
top = imHeight / 2 - drawHeight / 2,
52-
left = imWidth / 2 - drawWidth / 2,
53-
right = imWidth / 2 + drawWidth / 2,
54-
bottom = imHeight / 2 + drawHeight / 2
51+
top = (imHeight - drawHeight) / 2,
52+
left = (imWidth - drawWidth) / 2,
53+
right = (imWidth + drawWidth) / 2,
54+
bottom = (imHeight + drawHeight) / 2
5555
)
5656
} else {
5757
val drawHeight = imHeight
5858
val drawWidth = imHeight * pAspectRatio
5959
Rect(
60-
top = imHeight / 2 - drawHeight / 2,
61-
left = imWidth / 2 - drawWidth / 2,
62-
right = imWidth / 2 + drawWidth / 2,
63-
bottom = imHeight / 2 + drawHeight / 2
60+
top = (imHeight - drawHeight) / 2,
61+
left = (imWidth - drawWidth) / 2,
62+
right = (imWidth + drawWidth) / 2,
63+
bottom = (imHeight + drawHeight) / 2
6464
)
6565
}
6666

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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

Comments
 (0)