Created
May 26, 2020 14:28
-
-
Save Ikhiloya/1983fd8f9ce8def7af95b0810baa8778 to your computer and use it in GitHub Desktop.
A worker class to save Person entity to a remote server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.ikhiloyaimokhai.dependentworkmanagerperiodictask_kotlin.worker | |
| import android.content.Context | |
| import androidx.work.WorkerParameters | |
| import com.ikhiloyaimokhai.dependentworkmanagerperiodictask_kotlin.CommunityApp | |
| import com.ikhiloyaimokhai.dependentworkmanagerperiodictask_kotlin.db.CommunityDao | |
| import com.ikhiloyaimokhai.dependentworkmanagerperiodictask_kotlin.model.Person | |
| import com.ikhiloyaimokhai.dependentworkmanagerperiodictask_kotlin.service.CommunityService | |
| import com.ikhiloyaimokhai.dependentworkmanagerperiodictask_kotlin.util.Constant | |
| import com.ikhiloyaimokhai.dependentworkmanagerperiodictask_kotlin.util.WorkerUtils | |
| import retrofit2.Call | |
| import timber.log.Timber | |
| class PersonWorker(context: Context, workerParams: WorkerParameters) : | |
| WorkerContract<Person, Person>(context, workerParams) { | |
| private val tag: String = PersonWorker::class.java.simpleName | |
| private var communityService: CommunityService | |
| private var communityDao: CommunityDao | |
| init { | |
| val communityApp: CommunityApp = CommunityApp.instance | |
| communityService = communityApp.getEmployeeService() | |
| communityDao = communityApp.getEmployeeDao() | |
| } | |
| override fun loadPendingItemsFromDb(): MutableList<Person> { | |
| Timber.i("loading pending People from db..........") | |
| return communityDao.findPendingPeople() | |
| } | |
| override fun hasParentRelationship(resultType: Person): Boolean { | |
| //return false since this is the parent Entity | |
| return false | |
| } | |
| override fun checkParentStatus(resultType: Person): Boolean { | |
| //return false since this is the parent Entity | |
| return false | |
| } | |
| override fun createCall(resultType: Person): Call<Person>{ | |
| Timber.i("%s: creating call...", tag) | |
| return communityService.createPerson(resultType) | |
| } | |
| /* updates the local data with the remote ID as well as the state to [SYNCED] **/ | |
| override fun saveCallResult(responseData: Person, localData: Person) { | |
| localData.id = responseData.id | |
| localData.state = Constant.SYNCED | |
| communityDao.savePerson(localData) | |
| } | |
| override fun onPostFailed(resultType: Person) { | |
| WorkerUtils.makeStatusNotification( | |
| """failed to save ${resultType.firstName} ${resultType.lastName}""", | |
| applicationContext | |
| ) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment