Skip to content

Commit 406bb00

Browse files
author
praveen
committed
Added view, model and viewmodel structures with code that increments the counter when the floating action button is clicked via livedata.
1 parent 4a85cf9 commit 406bb00

File tree

11 files changed

+196
-35
lines changed

11 files changed

+196
-35
lines changed

app/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ android {
1616
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1717
}
1818

19+
buildFeatures {
20+
dataBinding true
21+
}
22+
1923
buildTypes {
2024
release {
2125
minifyEnabled false

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
android:roundIcon="@mipmap/ic_launcher_round"
1010
android:supportsRtl="true"
1111
android:theme="@style/Theme.CounterAppSimpleMVVMExample">
12-
<activity android:name=".MainActivity">
12+
<activity android:name=".view.HomeScreen">
1313
<intent-filter>
1414
<action android:name="android.intent.action.MAIN" />
1515

app/src/main/java/com/geeks4ever/counter_app/MainActivity.java

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.geeks4ever.counter_app.model;
2+
3+
public class CountModel {
4+
5+
private String count;
6+
7+
public String getCount() {
8+
return count;
9+
}
10+
11+
public void setCount(String count) {
12+
this.count = count;
13+
}
14+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.geeks4ever.counter_app.model.repository;
2+
3+
import androidx.lifecycle.LiveData;
4+
import androidx.lifecycle.MutableLiveData;
5+
6+
import com.geeks4ever.counter_app.model.CountModel;
7+
8+
public class CountRepository {
9+
10+
public static CountRepository repository;
11+
private CountModel countModel;
12+
13+
final MutableLiveData<CountModel> data = new MutableLiveData<>();
14+
15+
private CountRepository(){
16+
countModel.setCount("0");
17+
}
18+
19+
//Singleton Implementation
20+
public static CountRepository getInstance(){
21+
22+
23+
if(repository == null)
24+
repository = new CountRepository();
25+
return repository;
26+
}
27+
28+
29+
public LiveData<CountModel> getCount(){
30+
data.setValue(countModel);
31+
return data;
32+
}
33+
34+
public void setCount(int count){
35+
36+
countModel.setCount(String.valueOf(count));
37+
data.postValue(countModel);
38+
}
39+
40+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.geeks4ever.counter_app.view;
2+
3+
import androidx.appcompat.app.AppCompatActivity;
4+
import androidx.databinding.DataBindingUtil;
5+
import androidx.lifecycle.LiveData;
6+
import androidx.lifecycle.Observer;
7+
8+
import android.os.Bundle;
9+
10+
import com.geeks4ever.counter_app.R;
11+
import com.geeks4ever.counter_app.databinding.HomeScreenBinding;
12+
import com.geeks4ever.counter_app.viewmodel.CounterViewModel;
13+
14+
public class HomeScreen extends AppCompatActivity {
15+
16+
private CounterViewModel viewModel;
17+
private LiveData<String> count;
18+
19+
@Override
20+
protected void onCreate(Bundle savedInstanceState) {
21+
super.onCreate(savedInstanceState);
22+
23+
HomeScreenBinding binding = DataBindingUtil.setContentView(this, R.layout.home_screen);
24+
binding.setCount("0");
25+
26+
count = viewModel.getCount();
27+
28+
count.observe(this, new Observer<String>() {
29+
@Override
30+
public void onChanged(String s) {
31+
binding.setCount(s);
32+
}
33+
});
34+
35+
}
36+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.geeks4ever.counter_app.viewmodel;
2+
3+
import android.app.Application;
4+
5+
import androidx.annotation.NonNull;
6+
import androidx.lifecycle.AndroidViewModel;
7+
import androidx.lifecycle.LiveData;
8+
import androidx.lifecycle.MutableLiveData;
9+
import androidx.lifecycle.Observer;
10+
11+
import com.geeks4ever.counter_app.model.CountModel;
12+
import com.geeks4ever.counter_app.model.repository.CountRepository;
13+
14+
import java.util.Objects;
15+
16+
public class CounterViewModel extends AndroidViewModel {
17+
18+
private final CountRepository countRepository;
19+
20+
final MutableLiveData<String> count = new MutableLiveData<>();
21+
final LiveData<CountModel> data;
22+
23+
public CounterViewModel(@NonNull Application application) {
24+
super(application);
25+
26+
countRepository = CountRepository.getInstance();
27+
data = countRepository.getCount();
28+
data.observe(getApplication(), new Observer<CountModel>() {
29+
@Override
30+
public void onChanged(CountModel countModel) {
31+
count.setValue(countModel.getCount());
32+
}
33+
});
34+
}
35+
36+
public LiveData<String> getCount(){
37+
return count;
38+
}
39+
40+
public void IncrementCount(){
41+
int countint = Integer.parseInt(Objects.requireNonNull(count.getValue()));
42+
countRepository.setCount(countint+1);
43+
}
44+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<vector android:height="24dp" android:tint="#FFFFFF"
2+
android:viewportHeight="24" android:viewportWidth="24"
3+
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
4+
<path android:fillColor="@android:color/white" android:pathData="M19,13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
5+
</vector>

app/src/main/res/layout/activity_main.xml

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<layout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools">
5+
6+
<data>
7+
8+
<variable
9+
name="count"
10+
type="String" />
11+
12+
</data>
13+
14+
<androidx.constraintlayout.widget.ConstraintLayout
15+
android:layout_width="match_parent"
16+
android:layout_height="match_parent"
17+
tools:context=".view.HomeScreen">
18+
19+
<TextView
20+
android:layout_width="wrap_content"
21+
android:layout_height="wrap_content"
22+
android:text="click the Button to increase the counter"
23+
app:layout_constraintBottom_toTopOf="@+id/count"
24+
app:layout_constraintLeft_toLeftOf="parent"
25+
app:layout_constraintRight_toRightOf="parent"
26+
app:layout_constraintTop_toTopOf="parent" />
27+
28+
<TextView
29+
android:id="@+id/count"
30+
android:layout_width="wrap_content"
31+
android:layout_height="wrap_content"
32+
android:text="@{count}"
33+
android:textSize="30sp"
34+
android:textColor="@color/black"
35+
android:textStyle="bold"
36+
app:layout_constraintBottom_toBottomOf="parent"
37+
app:layout_constraintLeft_toLeftOf="parent"
38+
app:layout_constraintRight_toRightOf="parent"
39+
app:layout_constraintTop_toTopOf="parent" />
40+
41+
<com.google.android.material.floatingactionbutton.FloatingActionButton
42+
android:layout_margin="16dp"
43+
app:layout_constraintRight_toRightOf="parent"
44+
app:layout_constraintBottom_toBottomOf="parent"
45+
android:foreground="@drawable/add_icon"
46+
android:layout_width="wrap_content"
47+
android:layout_height="wrap_content"/>
48+
49+
</androidx.constraintlayout.widget.ConstraintLayout>
50+
</layout>

0 commit comments

Comments
 (0)