Skip to content

Commit ae425bb

Browse files
authored
Merge pull request #463 from android/adaptive-codelab-end
[AdaptiveUiCodelab] merge codelab updates to end
2 parents ceb05a2 + 34bea1f commit ae425bb

File tree

14 files changed

+348
-463
lines changed

14 files changed

+348
-463
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2022 Google LLC
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+
* https://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+
plugins {
18+
alias(libs.plugins.android.application)
19+
alias(libs.plugins.kotlin.android)
20+
}
21+
22+
android {
23+
namespace = "com.example.reply"
24+
compileSdk = 34
25+
26+
defaultConfig {
27+
applicationId = "com.example.reply"
28+
minSdk = 21
29+
targetSdk = 33
30+
versionCode = 1
31+
versionName = "1.0"
32+
33+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
34+
vectorDrawables {
35+
useSupportLibrary = true
36+
}
37+
}
38+
39+
buildTypes {
40+
release {
41+
isMinifyEnabled = false
42+
proguardFiles(
43+
getDefaultProguardFile("proguard-android-optimize.txt"),
44+
"proguard-rules.pro"
45+
)
46+
}
47+
}
48+
compileOptions {
49+
sourceCompatibility = JavaVersion.VERSION_1_8
50+
targetCompatibility = JavaVersion.VERSION_1_8
51+
}
52+
kotlinOptions {
53+
jvmTarget = "1.8"
54+
}
55+
buildFeatures {
56+
compose = true
57+
}
58+
composeOptions {
59+
kotlinCompilerExtensionVersion = "1.5.13"
60+
}
61+
packaging {
62+
resources {
63+
excludes += "/META-INF/AL2.0"
64+
excludes += "/META-INF/LGPL2.1"
65+
}
66+
}
67+
}
68+
69+
dependencies {
70+
val composeBom = platform(libs.androidx.compose.bom)
71+
implementation(composeBom)
72+
androidTestImplementation(composeBom)
73+
74+
implementation(libs.androidx.material3)
75+
implementation(libs.androidx.material3.adaptive)
76+
implementation(libs.androidx.material3.adaptive.layout)
77+
implementation(libs.androidx.material3.adaptive.nav.suite)
78+
implementation(libs.androidx.material3.adaptive.navigation)
79+
implementation(libs.androidx.material.icons.extended)
80+
implementation(libs.androidx.ui.tooling.preview)
81+
androidTestImplementation(libs.androidx.ui.test.junit4)
82+
debugImplementation(libs.androidx.ui.tooling)
83+
debugImplementation(libs.androidx.ui.test.manifest)
84+
85+
implementation(libs.androidx.lifecycle.viewmodel.compose)
86+
implementation(libs.androidx.lifecycle.runtime.compose)
87+
implementation(libs.androidx.lifecycle.runtime.ktx)
88+
implementation(libs.androidx.activity.compose)
89+
implementation(libs.androidx.core.ktx)
90+
implementation(libs.androidx.window)
91+
implementation(libs.kotlinx.coroutines.android)
92+
93+
testImplementation(libs.junit)
94+
androidTestImplementation(libs.androidx.junit)
95+
androidTestImplementation(libs.androidx.espresso.core)
96+
}

AdaptiveUiCodelab/app/proguard-rules.pro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Add project specific ProGuard rules here.
22
# You can control the set of applied configuration files using the
3-
# proguardFiles setting in build.gradle.
3+
# proguardFiles setting in build.gradle.kts.
44
#
55
# For more details, see
66
# http://developer.android.com/guide/developing/tools/proguard.html
@@ -18,4 +18,4 @@
1818

1919
# If you keep the line number information, uncomment this to
2020
# hide the original source file name.
21-
#-renamesourcefileattribute SourceFile
21+
#-renamesourcefileattribute SourceFile

AdaptiveUiCodelab/app/src/main/java/com/example/reply/data/Email.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ data class Email(
4848
var isStarred: Boolean = false,
4949
var mailbox: MailboxType = MailboxType.INBOX,
5050
var createAt: String,
51-
val threads: List<Email> = emptyList()
51+
val replies: List<Email> = emptyList()
5252
) {
5353
val senderPreview: String = "${sender.fullName} - 4 hrs ago"
5454
val hasBody: Boolean = body.isNotBlank()
@@ -59,5 +59,3 @@ data class Email(
5959
val nonUserAccountRecipients = recipients
6060
.filterNot { LocalAccountsDataProvider.isUserAccount(it.uid) }
6161
}
62-
63-

AdaptiveUiCodelab/app/src/main/java/com/example/reply/data/local/LocalEmailsDataProvider.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import com.example.reply.data.MailboxType
2727

2828
object LocalEmailsDataProvider {
2929

30-
private val threads = listOf(
30+
private val replies = listOf(
3131
Email(
3232
4L,
3333
LocalAccountsDataProvider.getContactAccountByUid(11L),
@@ -116,7 +116,7 @@ object LocalEmailsDataProvider {
116116
""".trimIndent(),
117117
createAt = "20 mins ago",
118118
isStarred = true,
119-
threads = threads,
119+
replies = replies,
120120
),
121121
Email(
122122
1L,
@@ -133,7 +133,7 @@ object LocalEmailsDataProvider {
133133
Ali
134134
""".trimIndent(),
135135
createAt = "40 mins ago",
136-
threads = threads,
136+
replies = replies,
137137
),
138138
Email(
139139
2L,
@@ -149,7 +149,7 @@ object LocalEmailsDataProvider {
149149
),
150150
true,
151151
createAt = "1 hour ago",
152-
threads = threads,
152+
replies = replies,
153153
),
154154
Email(
155155
3L,
@@ -311,4 +311,3 @@ object LocalEmailsDataProvider {
311311
"Grocery coupons"
312312
)
313313
}
314-

AdaptiveUiCodelab/app/src/main/java/com/example/reply/ui/MainActivity.kt

Lines changed: 20 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -20,65 +20,27 @@ import android.os.Bundle
2020
import androidx.activity.ComponentActivity
2121
import androidx.activity.compose.setContent
2222
import androidx.activity.viewModels
23-
import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi
24-
import androidx.compose.material3.windowsizeclass.WindowWidthSizeClass
25-
import androidx.compose.material3.windowsizeclass.calculateWindowSizeClass
2623
import androidx.compose.runtime.Composable
27-
import androidx.compose.runtime.collectAsState
24+
import androidx.compose.runtime.getValue
2825
import androidx.compose.ui.tooling.preview.Preview
29-
import androidx.lifecycle.flowWithLifecycle
30-
import androidx.lifecycle.lifecycleScope
31-
import androidx.window.layout.FoldingFeature
32-
import androidx.window.layout.WindowInfoTracker
26+
import androidx.lifecycle.compose.collectAsStateWithLifecycle
3327
import com.example.reply.data.local.LocalEmailsDataProvider
3428
import com.example.reply.ui.theme.ReplyTheme
35-
import com.example.reply.ui.utils.DevicePosture
36-
import com.example.reply.ui.utils.isBookPosture
37-
import com.example.reply.ui.utils.isSeparating
38-
import kotlinx.coroutines.flow.SharingStarted
39-
import kotlinx.coroutines.flow.map
40-
import kotlinx.coroutines.flow.stateIn
4129

4230
class MainActivity : ComponentActivity() {
4331

4432
private val viewModel: ReplyHomeViewModel by viewModels()
4533

46-
@OptIn(ExperimentalMaterial3WindowSizeClassApi::class)
4734
override fun onCreate(savedInstanceState: Bundle?) {
4835
super.onCreate(savedInstanceState)
4936

50-
/**
51-
* Flow of [DevicePosture] that emits every time there's a change in the windowLayoutInfo
52-
*/
53-
val devicePostureFlow = WindowInfoTracker.getOrCreate(this).windowLayoutInfo(this)
54-
.flowWithLifecycle(this.lifecycle)
55-
.map { layoutInfo ->
56-
val foldingFeature =
57-
layoutInfo.displayFeatures
58-
.filterIsInstance<FoldingFeature>()
59-
.firstOrNull()
60-
when {
61-
isBookPosture(foldingFeature) ->
62-
DevicePosture.BookPosture(foldingFeature.bounds)
63-
64-
isSeparating(foldingFeature) ->
65-
DevicePosture.Separating(foldingFeature.bounds, foldingFeature.orientation)
66-
67-
else -> DevicePosture.NormalPosture
68-
}
69-
}
70-
.stateIn(
71-
scope = lifecycleScope,
72-
started = SharingStarted.Eagerly,
73-
initialValue = DevicePosture.NormalPosture
74-
)
75-
7637
setContent {
7738
ReplyTheme {
78-
val windowSize = calculateWindowSizeClass(this)
79-
val devicePosture = devicePostureFlow.collectAsState().value
80-
val uiState = viewModel.uiState.collectAsState().value
81-
ReplyApp(windowSize.widthSizeClass, devicePosture, uiState)
39+
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
40+
ReplyApp(
41+
replyHomeUIState = uiState,
42+
onEmailClick = viewModel::setSelectedEmail
43+
)
8244
}
8345
}
8446
}
@@ -89,9 +51,10 @@ class MainActivity : ComponentActivity() {
8951
fun ReplyAppPreview() {
9052
ReplyTheme {
9153
ReplyApp(
92-
replyHomeUIState = ReplyHomeUIState(emails = LocalEmailsDataProvider.allEmails),
93-
windowSize = WindowWidthSizeClass.Compact,
94-
foldingDevicePosture = DevicePosture.NormalPosture
54+
replyHomeUIState = ReplyHomeUIState(
55+
emails = LocalEmailsDataProvider.allEmails
56+
),
57+
onEmailClick = {}
9558
)
9659
}
9760
}
@@ -101,9 +64,10 @@ fun ReplyAppPreview() {
10164
fun ReplyAppPreviewTablet() {
10265
ReplyTheme {
10366
ReplyApp(
104-
replyHomeUIState = ReplyHomeUIState(emails = LocalEmailsDataProvider.allEmails),
105-
windowSize = WindowWidthSizeClass.Medium,
106-
foldingDevicePosture = DevicePosture.NormalPosture
67+
replyHomeUIState = ReplyHomeUIState(
68+
emails = LocalEmailsDataProvider.allEmails
69+
),
70+
onEmailClick = {}
10771
)
10872
}
10973
}
@@ -113,9 +77,10 @@ fun ReplyAppPreviewTablet() {
11377
fun ReplyAppPreviewDesktop() {
11478
ReplyTheme {
11579
ReplyApp(
116-
replyHomeUIState = ReplyHomeUIState(emails = LocalEmailsDataProvider.allEmails),
117-
windowSize = WindowWidthSizeClass.Expanded,
118-
foldingDevicePosture = DevicePosture.NormalPosture
80+
replyHomeUIState = ReplyHomeUIState(
81+
emails = LocalEmailsDataProvider.allEmails
82+
),
83+
onEmailClick = {}
11984
)
12085
}
121-
}
86+
}

0 commit comments

Comments
 (0)