Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ dependencies {
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

//room
implementation "android.arch.persistence.room:runtime:1.1.1"
annotationProcessor "android.arch.persistence.room:compiler:1.1.1"
}
27 changes: 23 additions & 4 deletions app/src/main/java/com/geeks4ever/counter_app/model/CountModel.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
package com.geeks4ever.counter_app.model;

import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;

@Entity(tableName = "countmodel")
public class CountModel {

@PrimaryKey
public int key;

@ColumnInfo(name = "count")
private String count;

public String getCount() {
return count;
public CountModel(int key, String count){
this.key = key;
this.count = count;
}

public void setCount(String count) {
this.count = count;
public String getCount(){
Log.e("db", "getCount"+this.count);
return this.count;
}

public void setCount(String count){

Log.e("db", "setCount"+count);
this.count = count;}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.geeks4ever.counter_app.model.repository;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import com.geeks4ever.counter_app.model.CountModel;

import java.util.List;

@Dao
public interface CountDao {

@Query("Select * from countmodel where `key`= 0")
LiveData<List<CountModel>> getCount();

@Update
void setCount(CountModel count);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.geeks4ever.counter_app.model.repository;

import android.content.Context;
import android.util.Log;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;

import com.geeks4ever.counter_app.model.CountModel;

import java.util.concurrent.Executors;

@Database(entities = {CountModel.class}, version = 1, exportSchema = false)
public abstract class CountDatabase extends RoomDatabase {

private static CountDatabase db;

public abstract CountDao countDao();

public static synchronized CountDatabase getInstance(Context context){

if(db == null){
Log.e("db", "created");
db = Room.databaseBuilder(context.getApplicationContext(),
CountDatabase.class, "count_db")
.fallbackToDestructiveMigration()
.build();
}
return db;
}

}
Original file line number Diff line number Diff line change
@@ -1,43 +1,54 @@
package com.geeks4ever.counter_app.model.repository;

import android.app.Application;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;

import com.geeks4ever.counter_app.model.CountModel;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CountRepository {

public static CountRepository repository;
private final CountModel countModel;
private CountDao countDao;
private LiveData<List<CountModel>> countModelLiveData;

final MutableLiveData<CountModel> data = new MutableLiveData<>();
public CountRepository(Application application){

private CountRepository(){
countModel = new CountModel();
countModel.setCount("0");
}
countDao = CountDatabase.getInstance(application).countDao();
countModelLiveData = countDao.getCount();

//Singleton Implementation
public static CountRepository getInstance(){
}

public LiveData<List<CountModel>> getCount(){
return countModelLiveData;
}

if(repository == null)
repository = new CountRepository();
return repository;
public void setCount(int count){
new SetCountAsyncTask(countDao).execute(new CountModel(0, String.valueOf(count)));
}

private static class SetCountAsyncTask extends AsyncTask<CountModel, Void, Void>{

public LiveData<CountModel> getCount(){
data.setValue(countModel);
return data;
}
private CountDao countDao;

public void setCount(int count){
private SetCountAsyncTask(CountDao countDao){
this.countDao = countDao;
}

countModel.setCount(String.valueOf(count));
data.postValue(countModel);
@Override
protected Void doInBackground(CountModel... countModels) {
countDao.setCount(countModels[0]);
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.geeks4ever.counter_app.R;
import com.geeks4ever.counter_app.databinding.HomeScreenBinding;
import com.geeks4ever.counter_app.viewmodel.CounterViewModel;
import com.geeks4ever.counter_app.viewmodel.viewModelFactory;

public class HomeScreen extends AppCompatActivity implements Listener {

Expand All @@ -28,7 +27,8 @@ protected void onCreate(Bundle savedInstanceState) {

binding.setListeners(this);

viewModel = new ViewModelProvider(this, new viewModelFactory()).get(CounterViewModel.class);
viewModel = new ViewModelProvider(this, new ViewModelProvider
.AndroidViewModelFactory(getApplication())).get(CounterViewModel.class);

count = viewModel.getCount();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,47 @@
package com.geeks4ever.counter_app.viewmodel;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModel;

import com.geeks4ever.counter_app.model.CountModel;
import com.geeks4ever.counter_app.model.repository.CountDatabase;
import com.geeks4ever.counter_app.model.repository.CountRepository;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CounterViewModel extends ViewModel {
public class CounterViewModel extends AndroidViewModel {

private final CountRepository countRepository;

ExecutorService executorService;

final MutableLiveData<String> count = new MutableLiveData<>();
final LiveData<CountModel> data;

public CounterViewModel() {
countRepository = CountRepository.getInstance();
data = countRepository.getCount();
data.observeForever( new Observer<CountModel>() {
public CounterViewModel(@NonNull Application application) {
super(application);

count.setValue("0");

countRepository = new CountRepository(application);
countRepository.getCount().observeForever( new Observer<List<CountModel>>() {
@Override
public void onChanged(CountModel countModel) {
count.setValue(countModel.getCount());
public void onChanged(List<CountModel> countModel) {
count.setValue(countModel.get(0).getCount());
}
});
}


public LiveData<String> getCount(){
return count;
}
Expand Down

This file was deleted.