Counter App Jetpack Compose

abhinesh chandra
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 named count
  • 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 of CounterModel named _counter by initializing its count value to 0.
  • We have defined a method named getCounter inside the CounterRepository to fetch _counter object
  • we have defined method incrementCounter and decrementCounter inside CounterRepository to increment or decrement count 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 with CounterRepository
  • _count will fetch the data from CounterRepository and acts as a data source for CounterViewModel
  • we have declared _count as private and exposing its value using public state variable count
  • we have defined method increment and decrement , which is calling repository increment and decrement method to update count 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 contains viewModelCounter object of type CounterViewModel
  • Notice here we have used inbuilt viewModel() class to create viewModelCounter object of type CounterViewModel
  • we are updating Text composable of TheCounterApp using count state variable of the viewmodel, whenever the count state variable value or data changes Text 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.

--

--