Created
August 21, 2019 09:49
-
-
Save realdm/7fe9dae6095ad9c7e66a169fc75d843a to your computer and use it in GitHub Desktop.
This file contains 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
class DeviceRegistrationWorker(appContext: Context, workerParams: WorkerParameters) : Worker(appContext, workerParams) { | |
companion object { | |
const val TOKEN = "token" | |
const val PROFILE_ID = "profile_id" | |
const val DEVICE_NAME = "device_name" | |
private val LOG_TAG = DeviceRegistrationWorker::class.java.simpleName | |
} | |
/** | |
* Esta variavel pode ser injectada utilizando Dependency Injection. | |
* Para ja vamos assumir que temos uma Object class chamada WorkerDependencyProvider | |
* que tem uma variavel estatica com o NetworkClient definido. | |
* | |
* Temos que utilizar esta abordagem porque nao temos a oportunidade de inicializar o Worker manualmente | |
* e por isso nao podemos passar a informacao utilizando o constructor. | |
*/ | |
val networkClient: NetworkClient | |
get() = WorkerDependencyProvider.networkClient() | |
private val token = inputData.getString(TOKEN) | |
private val profileId = inputData.getString(PROFILE_ID) | |
private val deviceName = inputData.getString(DEVICE_NAME) | |
/** | |
* Este metodo e executado e uma background thread por isso | |
* podemos fazer qualquer tipo de operacao aqui de forma sincrona. | |
*/ | |
override fun doWork(): Result { | |
if (inputData.keyValueMap.isEmpty()) { | |
Log.e(LOG_TAG, "Register device work enqueued without input data") | |
return Result.failure() | |
} | |
val response = try { | |
// Invocar a operacao para comunicar-se com o servidor e obter a resposta | |
} | |
catch (e: Exception) { | |
return Result.failure() | |
} | |
return if (response.isSuccessful) { | |
Result.success() | |
} else { | |
Result.failure() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment