Counter App Jetpack Compose
3 min readMay 23, 2024
Overview
- Created a counter app using basic jetpack compose and MVVM architecture
Features
- we can increment or decrement the counter and the counter will survive or remain the same on configuration change.
Topics Covered
- View Model
- MVVM
Screenshots
Business Logic
- We are following MVVM pattern here.
- We have view model for business logic and interacting with model and views
- We have repository as a model, to store and update data.
- UI layer is basic and already covered earlier, so will cover only business logic here
Model
- Our model is defined inside the file
CounterModel.kt
- Inside our model we have data class named
CounterModel
CounterModel
has an attribute namedcount
- We have an repository named
CounterRepository
- Repository is used to fetch data from local or remote source
- Here repository
CounterRepository
is fetching data from local source by creating an object ofCounterModel
named_counter
by initializing its count value to 0. - We have defined a method named
getCounter
inside theCounterRepository
to fetch_counter
object - we have defined method
incrementCounter
anddecrementCounter
insideCounterRepository
to increment or decrementcount
value of the_counter
object
View Model
- we have used
CounterViewModel
as the view model for our view - we have created an object of
CounterRepository
named_repository
- we have created a state variable named
_count
of type_counter
object’s count value, which holds the data, and act as single source of truth along withCounterRepository
_count
will fetch the data fromCounterRepository
and acts as a data source forCounterViewModel
- we have declared
_count
as private and exposing its value using public state variablecount
- we have defined method
increment
anddecrement
, which is calling repository increment and decrement method to updatecount
value of_counter
object. - then we are assigning updated count value using repository object to
_count
variable.
View
- we have defined our view using
TheCounterApp
composable TheCounterApp
containsviewModelCounter
object of typeCounterViewModel
- Notice here we have used inbuilt
viewModel()
class to createviewModelCounter
object of typeCounterViewModel
- we are updating
Text
composable ofTheCounterApp
usingcount
state variable of the viewmodel, whenever the count state variable value or data changesText
composable is recomposed automatically to reflect current value. - Whenever we click on
Button
increment or decrement, viewmodel increment or decrement method is called which update the data in the repository. - The updated repository data is again fetched by the viewmodel increment and decrement methods to update its local data which in turn updates the UI automatically.
Learning’s
Why do we declare Private variables in Java?
- It comes under the concept of data hiding
- we are not protecting the variable’s value from other devs, it comes under another topic of data integrity
- When the variable is private its value is protected when the code runs.
- It keeps the variable hidden from the other classes which use the class
- Other classes should only access behavior by calling methods on the class and not changing variable value directly.
- If any class can change the variable value then it is difficult to determine the validity of the value. eg — you have a variable to count manufactured products of the factory. If you make the variable public any class can make its value negative and it can crash the other parts of the code. On the other hand if the variable is private, you can ensure its value is never negative.
- Sometimes even the methods that modifies these variables locally are made private for extra security.