Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

163817425: Push cache data to api #95

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions app/src/main/java/com/andela/art/checkin/CheckInActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.andela.art.root.ApplicationModule;
import com.andela.art.root.ArtApplication;
import com.andela.art.root.BaseMenuActivity;
import com.andela.art.utils.NetworkUtil;
import com.andela.art.securitydashboard.presentation.NfcSecurityDashboardActivity;
import com.squareup.picasso.Picasso;
import java.util.Locale;
Expand All @@ -40,12 +41,19 @@ public class CheckInActivity extends BaseMenuActivity implements CheckInView {
Bundle bundle;
private View mProgressView;
private CheckInRepository mRepository;
public NetworkUtil networkUtil = new NetworkUtil();


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_check_in);
mRepository = new CheckInRepository(getApplication());
if (!networkUtil.isNetworkAvailable(this)) {
//TODO: Should be different layout for offline devices
binding = DataBindingUtil.setContentView(this, R.layout.cached_activity_check_in);
}

mProgressView = findViewById(R.id.check_in_view_progressbar);
applicationComponent = ((ArtApplication) getApplication())
.applicationComponent();
Expand All @@ -60,6 +68,8 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
setSupportActionBar(binding.checkInToolbar);
binding.checkInToolbar.setTitleTextAppearance(this, R.style.CheckInTitle);
presenter.attachView(this);
mRepository.setPresenter(presenter);
mRepository.query();
displayDetails();
}

Expand Down
17 changes: 17 additions & 0 deletions app/src/main/java/com/andela/art/room/ArtDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Insert;
import android.arch.persistence.room.Query;

import java.util.List;


/**
Expand All @@ -18,4 +21,18 @@ public interface ArtDao {
*/
@Insert
void insertCheckIn(CheckInEntity checkInEntity);

/**
* Get checkin data.
* @return Return list of checkIn entities.
*/
@Query("SELECT * FROM checkIn")
List<CheckInEntity> getAllCheckInData();

/**
* Delete all Check In Records.
*/
@Query("DELETE FROM checkIn")
void deleteAllRecords();

}
2 changes: 1 addition & 1 deletion app/src/main/java/com/andela/art/room/ArtDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ static ArtDatabase getDatabase(final Context context) {
synchronized (ArtDatabase.class) {
if (instance == null) {
instance = Room.databaseBuilder(context.getApplicationContext(),
ArtDatabase.class, "artdb").build();
ArtDatabase.class, "artdb").build();
}
}
return instance;
Expand Down
52 changes: 52 additions & 0 deletions app/src/main/java/com/andela/art/room/CheckInRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import android.app.Application;
import android.os.AsyncTask;

import com.andela.art.checkin.CheckInPresenter;

import java.util.List;

/**
* Abstracted Repository as promoted by the Architecture Guide.
* https://developer.android.com/topic/libraries/architecture/guide.html
Expand All @@ -11,6 +15,15 @@
@SuppressWarnings("PMD.ImmutableField")
public class CheckInRepository {
private ArtDao mArtDao;
private CheckInPresenter presenter;

/**
* Set presenter value.
* @param presenter CheckinPresenter
*/
public void setPresenter(CheckInPresenter presenter) {
this.presenter = presenter;
}

/**
* Check in repository constructor.
Expand All @@ -29,6 +42,13 @@ public void insert(CheckInEntity mCheckInEntity) {
new InsertAsyncTask(mArtDao).execute(mCheckInEntity);
}

/**
*
*/
public void query() {
new QueryAsyncTask(mArtDao).execute();
}

/**
* non-UI thread to insert data into the DB.
*/
Expand All @@ -51,4 +71,36 @@ protected Void doInBackground(CheckInEntity... checkInEntities) {
return null;
}
}

/**
* Non-UI thread to query data from the DB.
*/
private class QueryAsyncTask extends AsyncTask<Void, Void, Void> {

private ArtDao mAsyncTaskDao;

/**
* Constructor.
* @param mArtDao ArtDao.
*/
QueryAsyncTask(ArtDao mArtDao) {
mAsyncTaskDao = mArtDao;
}

@Override
protected Void doInBackground(Void... voids) {
List<CheckInEntity> checkInData = mAsyncTaskDao.getAllCheckInData();
if (checkInData.isEmpty()) {
//Do nothing
return null;
} else {
presenter.checkIn(checkInData.get(0).getId(), //NOPMD
checkInData.get(0).getLogStatus()); //NOPMD
mAsyncTaskDao.deleteAllRecords();

}
return null;
}
}

}
Loading