Hi Phil, Thank you for this great content. Just a quick revision when you do the following: if(hasError) { state = state.copy( emailError = emailResult.errorMessage, passwordError = passwordResult.errorMessage, repeatedPasswordError = repeatedPasswordResult.errorMessage, termsError = termsResult.errorMessage ) return } The state should be modified regardless of an error here. While I was watching the video at the end you get the toast and the emailError is still not null because of hasError being false. So the state modification should get extracted out of that block and placed before the hasError check: state = state.copy( emailError = emailResult.errorMessage, passwordError = passwordResult.errorMessage, repeatedPasswordError = repeatedPasswordResult.errorMessage, termsError = termsResult.errorMessage ) if(hasError) { return }
One of my favorites about kotlin is, its Sealed Class. And I think it suits best for this kind of case. I created sealed class with children of every possible outcome, make domain layer don't care about wording and let presentation layer map it.
I prefer to create an after text changed listener on the edit texts that saves all the edit texts each time any text is changed. All the text is then tested in the use case and the returned messages are reviewed within the UI with a "when" bracket that will only show one message at a time. When there are no messages to show, the last option in the "when" bracket will enable the button to be clickable.
The state changes any time any of the edit texts change. If there's an issue, the variable in the state for that edit text will have a message or it will be null if it passes the chack. So the when statement will stop at the first variable with a message and do what you want it to do in that instance. If there are only nulls remaining in the state, the else branch in the when statement can make the confirm button enabled. But you have to make sure to make it disabled each time the text changes until all the data is validated.
Great video. Regarding unit testing, isn't it better to use interface impl pair for the use cases? Also, when asserting values, when you use assertEquals, the first parameter should be the value you expect, and the second value should be the value that you test. Regardless, when you test booleans, you can use assertTrue and assertFalse.
Setting error messages in the use case is definitely not clean architecture, interactor response should only contains litterals like boolean error flags, the presenter is responsible for error strings, and it's not in application layer.
We should step into DDD with a solid Use Case in MVVM, then convert to vertical slice architecture with a few independently buildable and testable modules...then we are styling Phil.
Great content again. Thanks for making such wonderful video. Complete this video by making network call handling success and failure case. How to propagate error to UI .
really really good video one gently critic the error message should disappear when the user is editing the input field. Thanks a lot for the video!! :))
1. How to handle navigation with this approach? Seems like the events passes directly from the composable to the viewmodel but what if i want to navigate in the navGraph itself in some events? 2. Isn't it too much to maintain 2 sides events (The channel for UI events)? Can't it be determined through the state value?
Your example shows that you are using strings in usecase. I think what you missed is that we need to map useCase to presenter object. Beacuse in standard compay project you would like to have strings in string resources.
What is the way to make the error messages support translation? Context can not be passed in use cases since these are designed to be independent of the platform.
Hello there, great and pretty complete video! Thank you for your hard work Philipp I have a question: how do you End-to-End test it? I explicitly followed this video and the one with testing (linked to your Note app) and whenever I ask the test to click on the Submit button and check if I have a new Text() displayed (with the explicit error), my test fails since it doesn't seem to reload the layout. Did it already happen to somebody?
i am using a pure java and kotlin module for the domain layer so i'm doing the validation in the datasource class (data layer) and injecting that in the usecase class...
I have a weird question. Is there any chance that when using a state hosted in the viewmodel or in a data class as we did in this tutorial, the UI is rendered slower? For example, when I am creating a Switch, if I create the state in the same file of the Switch composable, the switch toggles fast. But whenever I host the state in the viewmodel as you described here, looks like it has a minor delay when I click the Switch. Do you know anything that can affect that? By the way, awesome tutorial!
Hi, can you tell me an exact case when should we use SharedFlow (I watched some previous videos and you use them only for 1 observer(UI)), you mentioned it must have multiple observers. I just want to have a more accurate overview because there is another video where you compare LiveData vs Flows but also falls into the 1 observer category. Thanks.
Bro can you teach about how to use socket io using mvvm clean architecture pattern as there was no videos about socket io. It may help ful for my career as I came to android development by seeing your videos and I am be an good android developer because of you bro. If you don't mind can you teach us about socket io?
Hi I hope you can Answer my question. I am following your steps but at the last part I get lost because I have an layout xml and I dont know how to connect it and use the validations. I hope you can help me thank you. New subscriber
Nah, that's kind of the convention for a use case, since you can execute them. It therefore makes more sense to think of them like a function, not like a class
If you strictly want to follow clean architecture, yes. However, you might be able to reuse some use cases, for example if the logic for validating first names and last names is the same, then you might just use one ValidateName use case
Doesn't give you many benefits, but requires some setup. In the end I want to run tests, that's it. And JUnit4 works perfectly for that. And AFAIK, JUnit5 doesn't work for instrumented tests
Hi nice thks. Didn't understand your comment on "abstracting the test on valid Email to avoid using Patterns in the validation class". What do you mean by that ?
The Patterns class comes from the Android framework. In local unit tests as we did it here, you can't use Android dependencies, so you need to write some kind of abstraction (interface) for this pattern stuff. For example like this: interface PatternValidator { fun isValid(pattern: String): Boolean } class EmailValidator: PatternValidator { override fun isValid(pattern: String): Boolean { return Patterns.EMAIL_ADDRESS.matcher(pattern).matches() } } Then you can pass a PatternValidator to your use case and use it without having the Patterns import in the use case. That way it stays unit testable.
Thanks but I’m never gonna use this 😂😂😂 it is actually way too overkill just to validate Authentication fields and if the forma is very big then the number of files gonna overkill just for a single form submission.
Thanks for a great video and great content, can you make a video on how to implement firebase auth and firebase with clean architecture and jetpack compose
I think this would be more readable way for MVVM when click button and Submit is success, then action. Instead handle the next action on top and hard to read. ex: Button(onClick = { viewModel.onEvent(Event.Submit) { gotoNextFragment() } }) but I don't know, I try it but not very clean in VM. WDYT? waiting for you video if you like this way. :D thank youu
Thanks for including the test part. That's always missing in dev videos!
Glad to help!
Hi Phil, Thank you for this great content. Just a quick revision when you do the following:
if(hasError) {
state = state.copy(
emailError = emailResult.errorMessage,
passwordError = passwordResult.errorMessage,
repeatedPasswordError = repeatedPasswordResult.errorMessage,
termsError = termsResult.errorMessage
)
return
}
The state should be modified regardless of an error here. While I was watching the video at the end you get the toast and the emailError is still not null because of hasError being false. So the state modification should get extracted out of that block and placed before the hasError check:
state = state.copy(
emailError = emailResult.errorMessage,
passwordError = passwordResult.errorMessage,
repeatedPasswordError = repeatedPasswordResult.errorMessage,
termsError = termsResult.errorMessage
)
if(hasError) { return }
Nice catch 👌
Correct, thanks :)
You speak calmly and you remind me of Bob Ross, but showing expertise in Android development. What a great video 😁
Hahaha thanks mate!
One of my favorites about kotlin is, its Sealed Class. And I think it suits best for this kind of case. I created sealed class with children of every possible outcome, make domain layer don't care about wording and let presentation layer map it.
I love it too
@@PhilippLackner Wow, it's an honor to get replied by you. 😂 I had been watching many of your videos to learn android..
I prefer to create an after text changed listener on the edit texts that saves all the edit texts each time any text is changed. All the text is then tested in the use case and the returned messages are reviewed within the UI with a "when" bracket that will only show one message at a time. When there are no messages to show, the last option in the "when" bracket will enable the button to be clickable.
So you change the state based on the message and not on a Event.. ?
The state changes any time any of the edit texts change. If there's an issue, the variable in the state for that edit text will have a message or it will be null if it passes the chack. So the when statement will stop at the first variable with a message and do what you want it to do in that instance. If there are only nulls remaining in the state, the else branch in the when statement can make the confirm button enabled. But you have to make sure to make it disabled each time the text changes until all the data is validated.
Nice video bro 👌
I like the use of Channels, they seem quite good in a combination with Compose.
Thanks bud! Yeah it's the only real way to receive events in the composable (other than SharedFlow)
@@PhilippLackner Why you use a Channel and not a StateFlow or mutableStateOf?
Another quality tutorial from Phillip! Thank you man!
I've mix this video and the "This Is My FAVORITE Error Handling Class" one to valid custom user input and it work greats !
Great video.
Regarding unit testing, isn't it better to use interface impl pair for the use cases?
Also, when asserting values, when you use assertEquals, the first parameter should be the value you expect, and the second value should be the value that you test.
Regardless, when you test booleans, you can use assertTrue and assertFalse.
Setting error messages in the use case is definitely not clean architecture, interactor response should only contains litterals like boolean error flags, the presenter is responsible for error strings, and it's not in application layer.
Excellent as always.
We could add on the TextFields a maxLines = 1 and singleLine = true otherwise we could have many lines or an '
'
We should step into DDD with a solid Use Case in MVVM, then convert to vertical slice architecture with a few independently buildable and testable modules...then we are styling Phil.
Super PRO Champions League content Phillipp! Thanks!!
Great content again. Thanks for making such wonderful video. Complete this video by making network call handling success and failure case. How to propagate error to UI .
really really good video one gently critic the error message should disappear when the user is editing the input field. Thanks a lot for the video!! :))
1. How to handle navigation with this approach? Seems like the events passes directly from the composable to the viewmodel but what if i want to navigate in the navGraph itself in some events?
2. Isn't it too much to maintain 2 sides events (The channel for UI events)? Can't it be determined through the state value?
Duuuuuuude! This is supper cool example! Respect!!!
Your example shows that you are using strings in usecase.
I think what you missed is that we need to map useCase to presenter object.
Beacuse in standard compay project you would like to have strings in string resources.
What is the way to make the error messages support translation? Context can not be passed in use cases since these are designed to be independent of the platform.
Thanks a lot. Very interesting and useful video 👍
I wish Jetpack Compose could have a Form widget just like Flutter which could remove the amount of boilerplate code to create such.
Hello there, great and pretty complete video! Thank you for your hard work Philipp
I have a question: how do you End-to-End test it? I explicitly followed this video and the one with testing (linked to your Note app) and whenever I ask the test to click on the Submit button and check if I have a new Text() displayed (with the explicit error), my test fails since it doesn't seem to reload the layout. Did it already happen to somebody?
Thank you for the great content 🌻
But if we need to access the string resources what the use case will looks like?
i am using a pure java and kotlin module for the domain layer so i'm doing the validation in the datasource class (data layer) and injecting that in the usecase class...
Amazing quality content!
Hey great Video,
how can I reset the Flow so I can validate the next inputs after
How do you handle a 'loading' state? When the submit button is pressed, I would like to show a spinner and disable all input fields
very helpful, Thank you!
Thanks Philipp
unable to import mutableStateOf in my xml android project any alternative
Philipp this is a very nice tutorial that... Thanks
Glad you liked it!
Hi Philipp, does this way of using state recomposes everything whenever you type a character on the textfields? Is there a more optimized way?
Very cool video
Thank you Bro
You are the best 👍👍👍🔥🔥🔥
Do you have this but without Jetpack?
Hey, I'm from Brazil and I love your videos, but I always miss something. unit test
A suggestion, whenever possible add unit tests
I have a whole playlist about testing
I have a weird question.
Is there any chance that when using a state hosted in the viewmodel or in a data class as we did in this tutorial, the UI is rendered slower?
For example, when I am creating a Switch, if I create the state in the same file of the Switch composable, the switch toggles fast.
But whenever I host the state in the viewmodel as you described here, looks like it has a minor delay when I click the Switch.
Do you know anything that can affect that?
By the way, awesome tutorial!
Hi Phillip, How could you handle multiple fields for example 5 swipeable pages with 10 fields on each page?
I think it is supposed to be assertEquals(false, result.successful)? Since the second param is `actual`
Hi, can you tell me an exact case when should we use SharedFlow (I watched some previous videos and you use them only for 1 observer(UI)), you mentioned it must have multiple observers. I just want to have a more accurate overview because there is another video where you compare LiveData vs Flows but also falls into the 1 observer category. Thanks.
Bro can you teach about how to use socket io using mvvm clean architecture pattern as there was no videos about socket io. It may help ful for my career as I came to android development by seeing your videos and I am be an good android developer because of you bro. If you don't mind can you teach us about socket io?
Is it a good idea to expose mutable state from the ViewModel? Perhaps use "private set"?
Hi I hope you can Answer my question. I am following your steps but at the last part I get lost because I have an layout xml and I dont know how to connect it and use the validations. I hope you can help me thank you. New subscriber
Hii phil, thanks for the tutorial iam facing the issue like after succesful validation it is showing toast message pls provide me the solution.thanks
Could you guide full test course for android?
Why did you choose channel instead of LiveData? Great video, by the way!
It will be emitted when activity re-creates ( scree rotation, theme change) .
Correct me if I'm wrong.
on UseCase you could use "operator fun invoke()", than call on ViewModel like function: validateEmail(email)
Hi @Phil, Any way can we improve recomposition, Here for any one of field value change all other fields also effecting .
Great work!
it is better to call it EmailValidator instead of ValidateEmail
Nah, that's kind of the convention for a use case, since you can execute them. It therefore makes more sense to think of them like a function, not like a class
@@PhilippLackner good point of view
Super cool 😎
22:00 What should I do if I have a large number of textfields? Should I repeat the proccess for each one still?
If you strictly want to follow clean architecture, yes. However, you might be able to reuse some use cases, for example if the logic for validating first names and last names is the same, then you might just use one ValidateName use case
@@PhilippLackner wow thanks for responding so quickly
Hi Phillip, which theme you are using for android studio
Xcode dark
thanks
Hello! Why are you using JUnit4 over 5? Thanks for your videos!
Doesn't give you many benefits, but requires some setup. In the end I want to run tests, that's it. And JUnit4 works perfectly for that. And AFAIK, JUnit5 doesn't work for instrumented tests
Hi nice thks. Didn't understand your comment on "abstracting the test on valid Email to avoid using Patterns in the validation class". What do you mean by that ?
The Patterns class comes from the Android framework. In local unit tests as we did it here, you can't use Android dependencies, so you need to write some kind of abstraction (interface) for this pattern stuff. For example like this:
interface PatternValidator {
fun isValid(pattern: String): Boolean
}
class EmailValidator: PatternValidator {
override fun isValid(pattern: String): Boolean {
return Patterns.EMAIL_ADDRESS.matcher(pattern).matches()
}
}
Then you can pass a PatternValidator to your use case and use it without having the Patterns import in the use case. That way it stays unit testable.
@@PhilippLackner very clear thks
@@PhilippLackner how can we avoid using string ressources in the usecase where this should only be in a pure kotlin module ?
You were right, I was doing it wrong lol
Great 👌
very nice!!!
Thanks!!
Btw your instagram links are broken everywhere on youtube (video desc and on channel social media buttons)
Thanks man, because I changed the name. I'll fix it later. Appreciated
What a nasty manner of not putting a space between if and the parenthesis.
Thanks but I’m never gonna use this 😂😂😂 it is actually way too overkill just to validate Authentication fields and if the forma is very big then the number of files gonna overkill just for a single form submission.
Overkill in a small app? Yes. Necessary to make big apps scale further? Absolutely.
where is the link from 11:52? also your insta link is dead 😸 luv ur videos btw!
Thanks for a great video and great content, can you make a video on how to implement firebase auth and firebase with clean architecture and jetpack compose
bloc architecture...
⭐⭐⭐⭐⭐
when programming , show me the best practices, u not doing that , don't like that attitude sheesh
channel shouldn't' be use here right ?? its bug prone.
I think this would be more readable way for MVVM when click button and Submit is success, then action. Instead handle the next action on top and hard to read.
ex:
Button(onClick = {
viewModel.onEvent(Event.Submit) {
gotoNextFragment()
}
})
but I don't know, I try it but not very clean in VM.
WDYT? waiting for you video if you like this way. :D
thank youu
But not every event you send to the VM returns success/failure, so that would only help for those that have
Return me ❤️❤️❤️