Commit cc12e8e8 by Aleksandr Tamakov

fix: отправка страниц на сервер

parent 09ddf787
package com.isidroid.utils
import android.app.NotificationChannel
import android.content.Context
import android.os.Build
import androidx.core.app.NotificationManagerCompat
internal fun isNotificationChannelSupported() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
fun Context.checkIfNotificationChannelExists(channelId: String): String? {
if (!isNotificationChannelSupported()) return null
val manager = NotificationManagerCompat.from(this)
return manager.getNotificationChannel(channelId)?.id
}
fun Context.removeNotificationChannel(channelId: String?) {
if (!isNotificationChannelSupported() || channelId.isNullOrBlank()) return
val manager = NotificationManagerCompat.from(this)
manager.deleteNotificationChannel(channelId)
}
fun Context.createNotificationChannel(
channelId: String,
name: String,
importance: Int,
description: String? = null,
hasVibration: Boolean? = null,
hasSound: Boolean = false,
) {
if (!isNotificationChannelSupported()) return
// check whether exists
if (checkIfNotificationChannelExists(channelId) != null) return
val channel = NotificationChannel(channelId, name, importance)
description?.also { channel.description = it }
hasVibration?.also { channel.enableVibration(it) }
if (!hasSound)
channel.setSound(null, null)
NotificationManagerCompat.from(this).createNotificationChannel(channel)
}
\ No newline at end of file
......@@ -5,7 +5,7 @@ plugins {
}
android {
namespace = "com.isidroid.job"
namespace = "com.isidroid.job_sender"
compileSdk = BuildVersions.COMPILE_SDK
defaultConfig {
......
package com.isidroid.job_sender
import android.Manifest
import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationManager
import android.content.Context
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.hilt.work.HiltWorker
import androidx.work.Constraints
import androidx.work.CoroutineWorker
......@@ -10,7 +16,11 @@ import androidx.work.NetworkType
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.WorkerParameters
import com.isidroid.job_sender.domain.dto.JobSenderResult
import com.isidroid.job_sender.domain.use_case.SendPrintJobsUseCase
import com.isidroid.utils.checkIfNotificationChannelExists
import com.isidroid.utils.createNotificationChannel
import com.isidroid.utils.hasPermission
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import kotlinx.coroutines.coroutineScope
......@@ -19,6 +29,8 @@ import timber.log.Timber
import java.util.UUID
import java.util.concurrent.TimeUnit
private const val CHANNEL_ID = "UPLOAD_DATA"
@HiltWorker
class SendJobWorker @AssistedInject constructor(
@Assisted appContext: Context,
......@@ -27,28 +39,68 @@ class SendJobWorker @AssistedInject constructor(
private val collector: SendJobEventCollectorFlow
) : CoroutineWorker(appContext, workerParams) {
init {
if (appContext.checkIfNotificationChannelExists(CHANNEL_ID) == null)
applicationContext.createNotificationChannel(channelId = CHANNEL_ID, name = "Upload print jobs", importance = NotificationManager.IMPORTANCE_DEFAULT)
}
override suspend fun doWork(): Result = coroutineScope {
val collectorJob = launch { runCollector() }
var result: Result?
var notification: Notification?
Timber.i("==> 1")
try {
useCase.start()
Result.success()
notification = createNotification("Upload successfully completed")
result = Result.success()
} catch (t: Throwable) {
Timber.e(t)
Result.failure()
notification = createNotification("Upload complete with errors")
result = Result.failure()
} finally {
collectorJob.cancel()
}
showNotification(notification)
result ?: Result.failure()
}
private suspend fun runCollector() {
coroutineScope {
collector.eventsFlow.collect {
collector.eventsFlow.collect { event ->
val notification = when (event) {
is JobSenderResult.RenderProgress -> createNotification(title = "render", event.position, event.total, true)
is JobSenderResult.UploadProgress -> createNotification(title = "upload", event.position, event.total, true)
else -> null
}
showNotification(notification)
}
}
}
private fun createNotification(title: String, position: Int? = null, total: Int? = null, isOngoning: Boolean = false): Notification {
val builder = NotificationCompat.Builder(applicationContext, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_upload_notification)
.setContentTitle(title)
.setOngoing(isOngoning)
if (position != null && total != null)
builder.setProgress(total, position, false)
return builder.build()
}
@SuppressLint("MissingPermission")
private fun showNotification(notification: Notification?) {
notification ?: return
if (applicationContext.hasPermission(Manifest.permission.POST_NOTIFICATIONS))
NotificationManagerCompat.from(applicationContext).notify(100, notification)
}
companion object {
fun create(context: Context) {
val workManager = WorkManager.getInstance(context)
......
......@@ -11,7 +11,7 @@ import retrofit2.http.Path
interface ApiUpload {
@Multipart
@POST("api/job/{jobId}")
@POST("api/page/{jobId}")
fun upload(
@Path("jobId") jobId: String,
@Header("X-Access-Token") token: String,
......
......@@ -57,9 +57,6 @@ internal class JobSendRepositoryImpl(
for ((index, item) in items.withIndex()) {
eventCollector.updateProgress(index, total)
delay(10_000)
try {
val uploadResult = sendJobNetworkSource.uploadPage(jobId = item.printJobId, token = item.accessToken, filePath = item.sourceFile)
if (!uploadResult)
......
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="960" android:viewportWidth="960" android:width="24dp">
<path android:fillColor="@android:color/white" android:pathData="M440,593L440,720Q440,737 451.5,748.5Q463,760 480,760Q497,760 508.5,748.5Q520,737 520,720L520,593L556,629Q562,635 569.5,638Q577,641 584.5,640.5Q592,640 599,637Q606,634 612,628Q623,616 623.5,600Q624,584 612,572L508,468Q502,462 495,459.5Q488,457 480,457Q472,457 465,459.5Q458,462 452,468L348,572Q336,584 336.5,600Q337,616 349,628Q361,639 377,639.5Q393,640 405,628L440,593ZM240,880Q207,880 183.5,856.5Q160,833 160,800L160,160Q160,127 183.5,103.5Q207,80 240,80L527,80Q543,80 557.5,86Q572,92 583,103L777,297Q788,308 794,322.5Q800,337 800,353L800,800Q800,833 776.5,856.5Q753,880 720,880L240,880ZM520,320L520,160L240,160Q240,160 240,160Q240,160 240,160L240,800Q240,800 240,800Q240,800 240,800L720,800Q720,800 720,800Q720,800 720,800L720,360L560,360Q543,360 531.5,348.5Q520,337 520,320ZM240,160L240,160L240,320Q240,337 240,348.5Q240,360 240,360L240,360L240,160L240,320Q240,337 240,348.5Q240,360 240,360L240,360L240,800Q240,800 240,800Q240,800 240,800L240,800Q240,800 240,800Q240,800 240,800L240,160Q240,160 240,160Q240,160 240,160Z"/>
</vector>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment