File tree Expand file tree Collapse file tree 5 files changed +27
-3
lines changed
java/com/geeks4ever/counter_app Expand file tree Collapse file tree 5 files changed +27
-3
lines changed Original file line number Diff line number Diff line change 88public class CountRepository {
99
1010 public static CountRepository repository ;
11- private CountModel countModel ;
11+ private final CountModel countModel ;
1212
1313 final MutableLiveData <CountModel > data = new MutableLiveData <>();
1414
1515 private CountRepository (){
16+ countModel = new CountModel ();
1617 countModel .setCount ("0" );
1718 }
1819
Original file line number Diff line number Diff line change 66import androidx .lifecycle .Observer ;
77
88import android .os .Bundle ;
9+ import android .view .View ;
910
1011import com .geeks4ever .counter_app .R ;
1112import com .geeks4ever .counter_app .databinding .HomeScreenBinding ;
1213import com .geeks4ever .counter_app .viewmodel .CounterViewModel ;
1314
14- public class HomeScreen extends AppCompatActivity {
15+ public class HomeScreen extends AppCompatActivity implements Listener {
1516
1617 private CounterViewModel viewModel ;
1718 private LiveData <String > count ;
@@ -23,6 +24,10 @@ protected void onCreate(Bundle savedInstanceState) {
2324 HomeScreenBinding binding = DataBindingUtil .setContentView (this , R .layout .home_screen );
2425 binding .setCount ("0" );
2526
27+ binding .setListeners (this );
28+
29+ viewModel = new CounterViewModel (getApplication ());
30+
2631 count = viewModel .getCount ();
2732
2833 count .observe (this , new Observer <String >() {
@@ -33,4 +38,9 @@ public void onChanged(String s) {
3338 });
3439
3540 }
41+
42+ @ Override
43+ public void onClick (View view ) {
44+ viewModel .IncrementCount ();
45+ }
3646}
Original file line number Diff line number Diff line change 1+ package com .geeks4ever .counter_app .view ;
2+
3+ import android .view .View ;
4+
5+ public interface Listener {
6+
7+ void onClick (View view );
8+ }
Original file line number Diff line number Diff line change @@ -25,7 +25,7 @@ public CounterViewModel(@NonNull Application application) {
2525
2626 countRepository = CountRepository .getInstance ();
2727 data = countRepository .getCount ();
28- data .observe ( getApplication (), new Observer <CountModel >() {
28+ data .observeForever ( new Observer <CountModel >() {
2929 @ Override
3030 public void onChanged (CountModel countModel ) {
3131 count .setValue (countModel .getCount ());
Original file line number Diff line number Diff line change 99 name =" count"
1010 type =" String" />
1111
12+ <variable
13+ name =" listeners"
14+ type =" com.geeks4ever.counter_app.view.Listener" />
15+
1216 </data >
1317
1418 <androidx .constraintlayout.widget.ConstraintLayout
4347 app : layout_constraintRight_toRightOf =" parent"
4448 app : layout_constraintBottom_toBottomOf =" parent"
4549 android : foreground =" @drawable/add_icon"
50+ android : onClick =" @{listeners::onClick}"
4651 android : layout_width =" wrap_content"
4752 android : layout_height =" wrap_content" />
4853
You can’t perform that action at this time.
0 commit comments