Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e70ba38

Browse files
authoredSep 8, 2020
Merge pull request android#151 from android/fm/using_composeview
[Jetsurvey] Using ComposeView.setContent and addressing some of the l…
2 parents db27abe + 5e1364d commit e70ba38

File tree

12 files changed

+28
-46
lines changed

12 files changed

+28
-46
lines changed
 

‎Jetsurvey/app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ plugins {
2121
}
2222

2323
android {
24-
compileSdkVersion 29
24+
compileSdkVersion 30
2525

2626
defaultConfig {
2727
applicationId "com.example.compose.jetsurvey"
2828
minSdkVersion 21
29-
targetSdkVersion 29
29+
targetSdkVersion 30
3030
versionCode 1
3131
versionName "1.0"
3232

‎Jetsurvey/app/src/main/java/com/example/compose/jetsurvey/signinsignup/PasswordState.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ConfirmPasswordState(private val passwordState: PasswordState) : TextField
2525

2626
override fun getError(): String? {
2727
return if (showErrors()) {
28-
passwordConfirmationError(passwordState.text, text)
28+
passwordConfirmationError()
2929
} else {
3030
null
3131
}
@@ -44,6 +44,6 @@ private fun passwordValidationError(password: String): String {
4444
return "Invalid password"
4545
}
4646

47-
private fun passwordConfirmationError(password: String, confirmedPassword: String): String {
47+
private fun passwordConfirmationError(): String {
4848
return "Passwords don't match"
4949
}

‎Jetsurvey/app/src/main/java/com/example/compose/jetsurvey/signinsignup/SignInFragment.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ import android.os.Bundle
2020
import android.view.LayoutInflater
2121
import android.view.View
2222
import android.view.ViewGroup
23-
import android.widget.FrameLayout
24-
import androidx.compose.runtime.Recomposer
25-
import androidx.compose.ui.platform.setContent
23+
import androidx.compose.ui.platform.ComposeView
2624
import androidx.fragment.app.Fragment
2725
import androidx.fragment.app.viewModels
2826
import androidx.lifecycle.observe
@@ -43,21 +41,21 @@ class SignInFragment : Fragment() {
4341
container: ViewGroup?,
4442
savedInstanceState: Bundle?
4543
): View? {
46-
viewModel.navigateTo.observe(this) { navigateToEvent ->
44+
viewModel.navigateTo.observe(owner = viewLifecycleOwner) { navigateToEvent ->
4745
navigateToEvent.getContentIfNotHandled()?.let { navigateTo ->
4846
navigate(navigateTo, Screen.SignIn)
4947
}
5048
}
5149

52-
return FrameLayout(requireContext()).apply {
50+
return ComposeView(requireContext()).apply {
5351
// In order for savedState to work, the same ID needs to be used for all instances.
5452
id = R.id.sign_in_fragment
5553

5654
layoutParams = ViewGroup.LayoutParams(
5755
ViewGroup.LayoutParams.MATCH_PARENT,
5856
ViewGroup.LayoutParams.MATCH_PARENT
5957
)
60-
setContent(Recomposer.current()) {
58+
setContent {
6159
JetsurveyTheme {
6260
SignIn(
6361
onNavigationEvent = { event ->

‎Jetsurvey/app/src/main/java/com/example/compose/jetsurvey/signinsignup/SignInViewModel.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class SignInViewModel(private val userRepository: UserRepository) : ViewModel()
4949
}
5050
}
5151

52+
@Suppress("UNCHECKED_CAST")
5253
class SignInViewModelFactory : ViewModelProvider.Factory {
5354
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
5455
if (modelClass.isAssignableFrom(SignInViewModel::class.java)) {

‎Jetsurvey/app/src/main/java/com/example/compose/jetsurvey/signinsignup/SignUpFragment.kt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ import android.os.Bundle
2020
import android.view.LayoutInflater
2121
import android.view.View
2222
import android.view.ViewGroup
23-
import android.widget.FrameLayout
24-
import androidx.compose.runtime.Recomposer
25-
import androidx.compose.ui.platform.setContent
23+
import androidx.compose.ui.platform.ComposeView
2624
import androidx.fragment.app.Fragment
2725
import androidx.fragment.app.viewModels
2826
import androidx.lifecycle.observe
@@ -43,22 +41,21 @@ class SignUpFragment : Fragment() {
4341
container: ViewGroup?,
4442
savedInstanceState: Bundle?
4543
): View? {
46-
viewModel.navigateTo.observe(viewLifecycleOwner) { navigateToEvent ->
44+
viewModel.navigateTo.observe(owner = viewLifecycleOwner) { navigateToEvent ->
4745
navigateToEvent.getContentIfNotHandled()?.let { navigateTo ->
4846
navigate(navigateTo, Screen.SignUp)
4947
}
5048
}
5149

52-
return FrameLayout(requireContext()).apply {
53-
50+
return ComposeView(requireContext()).apply {
5451
// In order for savedState to work, the same ID needs to be used for all instances.
5552
id = R.id.sign_up_fragment
5653

5754
layoutParams = ViewGroup.LayoutParams(
5855
ViewGroup.LayoutParams.MATCH_PARENT,
5956
ViewGroup.LayoutParams.MATCH_PARENT
6057
)
61-
setContent(Recomposer.current()) {
58+
setContent {
6259
JetsurveyTheme {
6360
SignUp(
6461
onNavigationEvent = { event ->

‎Jetsurvey/app/src/main/java/com/example/compose/jetsurvey/signinsignup/WelcomeFragment.kt

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,10 @@ import android.os.Bundle
2020
import android.view.LayoutInflater
2121
import android.view.View
2222
import android.view.ViewGroup
23-
import android.widget.FrameLayout
24-
import androidx.compose.runtime.Recomposer
25-
import androidx.compose.ui.platform.setContent
23+
import androidx.compose.ui.platform.ComposeView
2624
import androidx.fragment.app.Fragment
2725
import androidx.fragment.app.viewModels
2826
import androidx.lifecycle.observe
29-
import com.example.compose.jetsurvey.R
3027
import com.example.compose.jetsurvey.Screen
3128
import com.example.compose.jetsurvey.navigate
3229
import com.example.compose.jetsurvey.theme.JetsurveyTheme
@@ -43,21 +40,14 @@ class WelcomeFragment : Fragment() {
4340
container: ViewGroup?,
4441
savedInstanceState: Bundle?
4542
): View? {
46-
viewModel.navigateTo.observe(viewLifecycleOwner) { navigateToEvent ->
43+
viewModel.navigateTo.observe(owner = viewLifecycleOwner) { navigateToEvent ->
4744
navigateToEvent.getContentIfNotHandled()?.let { navigateTo ->
4845
navigate(navigateTo, Screen.Welcome)
4946
}
5047
}
5148

52-
return FrameLayout(requireContext()).apply {
53-
// In order for savedState to work, the same ID needs to be used for all instances.
54-
id = R.id.welcome_fragment
55-
56-
layoutParams = ViewGroup.LayoutParams(
57-
ViewGroup.LayoutParams.MATCH_PARENT,
58-
ViewGroup.LayoutParams.MATCH_PARENT
59-
)
60-
setContent(Recomposer.current()) {
49+
return ComposeView(requireContext()).apply {
50+
setContent {
6151
JetsurveyTheme {
6252
WelcomeScreen(
6353
onEvent = { event ->

‎Jetsurvey/app/src/main/java/com/example/compose/jetsurvey/signinsignup/WelcomeViewModel.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class WelcomeViewModel(private val userRepository: UserRepository) : ViewModel()
4545
}
4646
}
4747

48+
@Suppress("UNCHECKED_CAST")
4849
class WelcomeViewModelFactory : ViewModelProvider.Factory {
4950
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
5051
if (modelClass.isAssignableFrom(WelcomeViewModel::class.java)) {

‎Jetsurvey/app/src/main/java/com/example/compose/jetsurvey/survey/SurveyFragment.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ import android.os.Bundle
2020
import android.view.LayoutInflater
2121
import android.view.View
2222
import android.view.ViewGroup
23-
import android.widget.FrameLayout
24-
import androidx.compose.runtime.Recomposer
2523
import androidx.compose.runtime.livedata.observeAsState
26-
import androidx.compose.ui.platform.setContent
24+
import androidx.compose.ui.platform.ComposeView
2725
import androidx.fragment.app.Fragment
2826
import androidx.fragment.app.viewModels
2927
import com.example.compose.jetsurvey.R
@@ -39,16 +37,15 @@ class SurveyFragment : Fragment() {
3937
container: ViewGroup?,
4038
savedInstanceState: Bundle?
4139
): View? {
42-
return FrameLayout(requireContext()).apply {
43-
40+
return ComposeView(requireContext()).apply {
4441
// In order for savedState to work, the same ID needs to be used for all instances.
4542
id = R.id.sign_in_fragment
4643

4744
layoutParams = ViewGroup.LayoutParams(
4845
ViewGroup.LayoutParams.MATCH_PARENT,
4946
ViewGroup.LayoutParams.MATCH_PARENT
5047
)
51-
setContent(Recomposer.current()) {
48+
setContent {
5249
JetsurveyTheme {
5350
viewModel.uiState.observeAsState().value?.let { surveyState ->
5451
when (surveyState) {

‎Jetsurvey/app/src/main/java/com/example/compose/jetsurvey/survey/SurveyRepository.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ private val jetpackQuestions = listOf(
7373
answer = PossibleAnswer.Slider(
7474
range = 1f..10f,
7575
steps = 3,
76-
startText = R.string.selfie_value_1,
77-
endText = R.string.selfie_value_3
76+
startText = R.string.selfie_min,
77+
endText = R.string.selfie_max
7878
)
7979
)
8080
)

‎Jetsurvey/app/src/main/res/layout/content_main.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
~ limitations under the License.
1616
~
1717
-->
18-
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
18+
<merge xmlns:android="http://schemas.android.com/apk/res/android"
1919
xmlns:app="http://schemas.android.com/apk/res-auto"
2020
android:layout_width="match_parent"
2121
android:layout_height="match_parent">
@@ -27,4 +27,4 @@
2727
android:layout_height="match_parent"
2828
app:defaultNavHost="true"
2929
app:navGraph="@navigation/nav_graph" />
30-
</FrameLayout>
30+
</merge>

‎Jetsurvey/app/src/main/res/values/strings.xml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
<string name="password">Password</string>
2222
<string name="confirm_password">Confirm password</string>
2323
<string name="sign_in">Sign in</string>
24-
<string name="sign_up">Sign up</string>
2524
<string name="sign_in_guest">Sign in as guest</string>
2625
<string name="sign_in_create_account">Sign in or create an account</string>
2726
<string name="or">or</string>
@@ -62,9 +61,8 @@
6261

6362
<!-- question 4-->
6463
<string name="selfies">How do you feel about selfies 🤳?</string>
65-
<string name="selfie_value_1">😒️</string>
66-
<string name="selfie_value_2">😐</string>
67-
<string name="selfie_value_3">🤩️</string>
64+
<string name="selfie_min">😒️</string>
65+
<string name="selfie_max">🤩️</string>
6866

6967
<!-- question 5-->
7068
<string name="selfie_skills">Show us your selfie skills!</string>

‎Jetsurvey/buildSrc/src/main/java/com/example/compose/jetsurvey/buildsrc/dependencies.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ object Versions {
2121
}
2222

2323
object Libs {
24-
const val androidGradlePlugin = "com.android.tools.build:gradle:4.2.0-alpha08"
24+
const val androidGradlePlugin = "com.android.tools.build:gradle:4.2.0-alpha09"
2525
const val jdkDesugar = "com.android.tools:desugar_jdk_libs:1.0.9"
2626

2727
const val junit = "junit:junit:4.13"

0 commit comments

Comments
 (0)