I am trying to create MVVM architecture in flutter native platform code. so that i can use viewmodel and livedata in either FLutter android plugins or android platform side.
Can anyone give a correct solution for this implementation?
Stacked package in flutter is nearby the MVVM architecture,
But in MVVM, ViewModel can be shared with more than one View but in Stacked state management, we View and ViewModel intact to each other
Here is the Stacked package.
https://pub.dev/packages/stacked
Related
I am new in Kmm I have create a UI with Jetpack compose for android and swift ui for IOS in simple application in kmm, so in shared module I just want to create viewModels for both android and iOS activities but I don't find any proper solution for this.
Had tried with this library to https://github.com/icerockdev/moko-mvvm
to achieve Clean MVVM in KMM project
I am new to android development using kotlin, I have no problem implementing functions, but i dont know how to go about the UI, does kotlin has a framework for that like flutter or i am to build my UI using XML
Jetpack Compose launched recently which has a declarative approach for building the UI just like flutter. It works with Kotlin.
Check more about it here :) ( you don't need to use XML if you are using Jetpack Compose in your project )
I'm new to KMM (Kotlin Multiplatform Mobile). I need to use MVVM in this project and I just need to use jetpack for ViewModels. So, how can I organize MVVM architecture in the KMM project?
You can create an expect/actual declaration for your viewmodels, where you can use the architectural ViewModels in your Android actual declarations.
This example does the exact same thing.
The expect/actual documentation you can find here
After searching about android architecture components I realized that MVVM is better than other architectures at some points so I started to learn MVVM and I'm using Kotlin language but the problem is when I search for tutorials to start learning I found many components like RxJava, RxAndroid, Dagger2, Room, Lifecycle and etc. but they don't say why they are using them and why they choose some specific components. i will show you some examples :
1- Android MVVM with Dagger2, Retrofit, RxJava
2- Android MVVM with Dagger2, Retrofit, RxAndroid
3- Android app with MVVM dagger2 RxJava Livedata and Room
My questions are :
1- How should I decide to choose some of these components and work with them?
2- How should I know that I need to use some of them together(for example Livedata and RxJava)?
This is really confusing for me and I don't know how to start.
First you need to individually understand why to use Kotlin, MVVM, Dagger, RxJava, Retrofit.
MVVM and Kotlin:
https://medium.com/#zhangqichuan/android-development-with-mvvm-and-kotlin-9598c3623ce1
RxJava and RxAndroid:
RxAndroid is an extension to RxJava(Used for handling threads and asynchronous tasks), for more details check following links:
Difference between RxJava and RxAndroid?
https://www.quora.com/What-is-the-difference-between-Retrofit-and-RxJava-RxAndroid
Retrofit:
https://medium.com/#elye.project/kotlin-and-retrofit-2-tutorial-with-working-codes-333a4422a890
LiveData and room with kotlin:
Room is a database layer on top of an SQLite database which used to remove boilter plate while fetching data from local sqlite database, use following links:
https://codelabs.developers.google.com/codelabs/android-room-with-a-view-kotlin/#0
After understanding this components(Kotlin, MVVM, Dagger, RxJava and RxAndroid, Retrofit, LiveData) individually then you need to do all this things in single project, use following link:
https://github.com/egek92/Kotlin-MVVM-LiveData-Room
As a jr. android developer, i was thinking like you but i choose a route like :
Learn MVVM with ViewModel(AAC),LiveData,Observable pattern.
Make your async and multi threading processes with Coroutines.
Make your Dependency Injection with Koin.
(If you want you can see how to mix them. Koin will be added soon. https://github.com/furkanaskin/CoinStalker)
Future?
Dagger 2, maybe Rxjava etc..
If you know the Core Android basics and has done at least 1 or 2 projects then you can jump to this, https://android.jlelse.eu/why-to-choose-mvvm-over-mvp-android-architecture-33c0f2de5516
Maybe what you need is start from basics.
There are a couple of google official code labs about Android architecture.
You can start looking at this code lab and then the others.
https://codelabs.developers.google.com/codelabs/kotlin-android-training-view-model
Simple Note App is written in Kotlin using MVVM architecture
https://github.com/prakashshuklahub/Simple-Notes-Kotlin-App
Architecture Components used in the app are as follows
LiveData
Room:
ViewModel:
Repository:
You may check the GitHub Project using
Kotlin
MVVM
Jetpack
Retrofit2
Here is full source GitHUb Link: https://github.com/enamul95/kotlin_MVVM_Jetpack
New Android Architecture Components released on google IO 17.
So should we use MVP with architecture components and MVVM?
In google sample on Github, they have used the MVVM model.
Google Sample
About the selection of architectural pattern and its implementation, it always depends on many factors such as team members, app's types, and so on.
In Google's blog post, they have already said that Architecture Components are mainly
Opinions not Prescriptions.
However, three main components (Room, ViewModel, ViewData) are developed with wide-range usage but not focus on any specific pattern.
For example,
If you are using SQLite to persist local data in your app, you can
you Room no matter what kind of pattern you are using, it is really a
great library with a lot of benefits such as reducing boiler plate
codes, validating SQLite syntax in compilation time, etc.
Besides, LifeCycle, LiveData, and ViewModel have their own strong
points. Especially, they address lifecycle-handling issues really well.
LiveData gives you one more tool of observer pattern which handles data stream reactively. (Like a great feature RxJava provides us).
...
At the end, for your question.
So should we use MVP with architecture components?
The answer is "it's nice part of Android framework, so why not".
Updated:
If you want to see how a MVP pattern project works with new Architecture Components, please checkout my repository on Github. In which I enhance previous MVP model by using Room for local persistence and LifeCycle to create lifecycle-aware Presenter.
I'm currently building a template project that uses MVP pattern along with Google Architecture components (Room, LiveData), and compare to mix version of Realm and LiveData:
https://github.com/duyp/mvp-template
I think some of Google Architecture Components are compatible with MVP Pattern, not only for MVVM :D
You can check out 2 following branches:
realm_livedata: MVP with Realm and LiveData
room_livedata: MVP with Room persistence library and LiveData
It's much more complex than Google sample project (I used Dagger 2, RxAndroid, Retrofit, Gson, and clean architecture) hope you find a better solution for your work. If any question or suggestion, you can leave comments or issues on my github project.
I also created a project using MVVM with new Google architecture components with Realm, Dagger 2, Live data: https://github.com/duyp/mvvm-template
Happy coding!