-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[gateway] send message state by
Worker
- Loading branch information
Showing
9 changed files
with
113 additions
and
65 deletions.
There are no files selected for viewing
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
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
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
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
13 changes: 11 additions & 2 deletions
13
...way/modules/gateway/RegistrationWorker.kt → ...les/gateway/workers/RegistrationWorker.kt
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
67 changes: 67 additions & 0 deletions
67
app/src/main/java/me/capcom/smsgateway/modules/gateway/workers/SendStateWorker.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package me.capcom.smsgateway.modules.gateway.workers | ||
|
||
import android.content.Context | ||
import androidx.work.BackoffPolicy | ||
import androidx.work.Constraints | ||
import androidx.work.CoroutineWorker | ||
import androidx.work.NetworkType | ||
import androidx.work.OneTimeWorkRequestBuilder | ||
import androidx.work.WorkManager | ||
import androidx.work.WorkRequest | ||
import androidx.work.WorkerParameters | ||
import androidx.work.workDataOf | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import me.capcom.smsgateway.modules.gateway.GatewayModule | ||
import me.capcom.smsgateway.modules.messages.MessagesService | ||
import org.koin.core.component.KoinComponent | ||
import org.koin.core.component.inject | ||
import java.util.concurrent.TimeUnit | ||
|
||
class SendStateWorker(appContext: Context, params: WorkerParameters) : | ||
CoroutineWorker(appContext, params), KoinComponent { | ||
private val messagesService: MessagesService by inject() | ||
private val gatewayModule: GatewayModule by inject() | ||
override suspend fun doWork(): Result { | ||
try { | ||
val messageId = inputData.getString(MESSAGE_ID) ?: return Result.failure() | ||
val message = messagesService.getMessage(messageId) ?: return Result.failure() | ||
|
||
withContext(Dispatchers.IO) { | ||
gatewayModule.sendState(message) | ||
} | ||
return Result.success() | ||
} catch (th: Throwable) { | ||
th.printStackTrace() | ||
return when { | ||
this.runAttemptCount < RETRY_COUNT -> Result.retry() | ||
else -> Result.failure() | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
private const val RETRY_COUNT = 3 | ||
|
||
private const val MESSAGE_ID = "messageId" | ||
|
||
fun start(context: Context, messageId: String) { | ||
val work = OneTimeWorkRequestBuilder<SendStateWorker>() | ||
.setInputData(workDataOf(MESSAGE_ID to messageId)) | ||
.setBackoffCriteria( | ||
BackoffPolicy.EXPONENTIAL, | ||
WorkRequest.MIN_BACKOFF_MILLIS, | ||
TimeUnit.MILLISECONDS | ||
) | ||
.setConstraints( | ||
Constraints.Builder() | ||
.setRequiredNetworkType(NetworkType.CONNECTED) | ||
.build() | ||
) | ||
.build() | ||
|
||
WorkManager.getInstance(context) | ||
.enqueue(work) | ||
} | ||
} | ||
} |
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
9 changes: 0 additions & 9 deletions
9
app/src/main/java/me/capcom/smsgateway/modules/messages/events/MessageStateChangedEvent.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,13 @@ | ||
package me.capcom.smsgateway.modules.messages.events | ||
|
||
import me.capcom.smsgateway.data.entities.Message | ||
import me.capcom.smsgateway.modules.events.AppEvent | ||
import me.capcom.smsgateway.modules.messages.data.MessageSource | ||
|
||
class MessageStateChangedEvent( | ||
val id: String, | ||
val state: Message.State, | ||
val source: MessageSource, | ||
val recipients: List<Recipient>, | ||
): AppEvent(NAME) { | ||
companion object { | ||
const val NAME = "MessageStateChangedEvent" | ||
} | ||
|
||
data class Recipient( | ||
val phoneNumber: String, | ||
val state: Message.State, | ||
val error: String?, | ||
) | ||
} |
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