diff --git a/app/src/main/java/com/andela/art/checkin/CheckInActivity.java b/app/src/main/java/com/andela/art/checkin/CheckInActivity.java index b2b193f..b99e5d7 100644 --- a/app/src/main/java/com/andela/art/checkin/CheckInActivity.java +++ b/app/src/main/java/com/andela/art/checkin/CheckInActivity.java @@ -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; @@ -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(); @@ -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(); } diff --git a/app/src/main/java/com/andela/art/room/ArtDao.java b/app/src/main/java/com/andela/art/room/ArtDao.java index d0c8ccf..c8dcffc 100644 --- a/app/src/main/java/com/andela/art/room/ArtDao.java +++ b/app/src/main/java/com/andela/art/room/ArtDao.java @@ -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; /** @@ -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 getAllCheckInData(); + + /** + * Delete all Check In Records. + */ + @Query("DELETE FROM checkIn") + void deleteAllRecords(); + } diff --git a/app/src/main/java/com/andela/art/room/ArtDatabase.java b/app/src/main/java/com/andela/art/room/ArtDatabase.java index 47e1206..6b5d05c 100644 --- a/app/src/main/java/com/andela/art/room/ArtDatabase.java +++ b/app/src/main/java/com/andela/art/room/ArtDatabase.java @@ -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; diff --git a/app/src/main/java/com/andela/art/room/CheckInRepository.java b/app/src/main/java/com/andela/art/room/CheckInRepository.java index 87914a3..d615884 100644 --- a/app/src/main/java/com/andela/art/room/CheckInRepository.java +++ b/app/src/main/java/com/andela/art/room/CheckInRepository.java @@ -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 @@ -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. @@ -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. */ @@ -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 { + + private ArtDao mAsyncTaskDao; + + /** + * Constructor. + * @param mArtDao ArtDao. + */ + QueryAsyncTask(ArtDao mArtDao) { + mAsyncTaskDao = mArtDao; + } + + @Override + protected Void doInBackground(Void... voids) { + List 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; + } + } + } diff --git a/app/src/main/res/layout/cached_activity_check_in.xml b/app/src/main/res/layout/cached_activity_check_in.xml new file mode 100644 index 0000000..85b08c8 --- /dev/null +++ b/app/src/main/res/layout/cached_activity_check_in.xml @@ -0,0 +1,256 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +