Changeset View
Changeset View
Standalone View
Standalone View
app/src/main/java/it/reyboz/bustorino/data/MatoTripsDownloadWorker.kt
| Show First 20 Lines • Show All 76 Lines • ▼ Show 20 Lines | private fun downloadGtfsTrips():Result{ | ||||
| " succeded: ${downloadedMatoTrips.size}") | " succeded: ${downloadedMatoTrips.size}") | ||||
| //check if we can insert the trips | //check if we can insert the trips | ||||
| requestCountDown.countDown() | requestCountDown.countDown() | ||||
| } | } | ||||
| } | } | ||||
| requestCountDown.await() | requestCountDown.await() | ||||
| val tripsIDsCompleted = downloadedMatoTrips.map { trip-> trip.tripID } | val tripsIDsCompleted = downloadedMatoTrips.map { trip-> trip.tripID } | ||||
| if (tripsIDsCompleted.isEmpty()){ | |||||
| Log.d(DEBUG_TAG, "No trips have been downloaded, set work to fail") | |||||
| return Result.failure() | |||||
| } else { | |||||
| val doInsert = (queriedMatoTrips subtract failedMatoTripsDownload).containsAll(tripsIDsCompleted) | val doInsert = (queriedMatoTrips subtract failedMatoTripsDownload).containsAll(tripsIDsCompleted) | ||||
| Log.i(DEBUG_TAG, "Inserting missing GtfsTrips in the database, should insert $doInsert") | Log.i(DEBUG_TAG, "Inserting missing GtfsTrips in the database, should insert $doInsert") | ||||
| if(doInsert){ | if (doInsert) { | ||||
| gtfsRepository.gtfsDao.insertTrips(downloadedMatoTrips) | gtfsRepository.gtfsDao.insertTrips(downloadedMatoTrips) | ||||
| } | } | ||||
| return Result.success() | return Result.success() | ||||
| } | } | ||||
| } | |||||
| override suspend fun getForegroundInfo(): ForegroundInfo { | override suspend fun getForegroundInfo(): ForegroundInfo { | ||||
| val notificationManager = | val notificationManager = | ||||
| applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | ||||
| val context = applicationContext | val context = applicationContext | ||||
| Notifications.createDBNotificationChannel(context) | Notifications.createDBNotificationChannel(context) | ||||
| return ForegroundInfo(NOTIFICATION_ID, Notifications.makeMatoDownloadNotification(context)) | return ForegroundInfo(NOTIFICATION_ID, Notifications.makeMatoDownloadNotification(context)) | ||||
| } | } | ||||
| companion object{ | companion object{ | ||||
| const val TRIPS_KEYS = "tripsToDownload" | const val TRIPS_KEYS = "tripsToDownload" | ||||
| const val DEBUG_TAG="BusTO:MatoTripDownWRK" | const val DEBUG_TAG="BusTO:MatoTripDownWRK" | ||||
| const val NOTIFICATION_ID=42424221 | const val NOTIFICATION_ID=42424221 | ||||
| const val TAG_TRIPS ="gtfsTripsDownload" | const val TAG_TRIPS ="gtfsTripsDownload" | ||||
| fun downloadTripsFromMato(trips: List<String>, context: Context, debugTag: String): Boolean{ | fun requestMatoTripsDownload(trips: List<String>, context: Context, debugTag: String): OneTimeWorkRequest? { | ||||
| if (trips.isEmpty()) return false | if (trips.isEmpty()) return null | ||||
| val workManager = WorkManager.getInstance(context) | val workManager = WorkManager.getInstance(context) | ||||
| val info = workManager.getWorkInfosForUniqueWork(TAG_TRIPS).get() | val info = workManager.getWorkInfosForUniqueWork(TAG_TRIPS).get() | ||||
| val runNewWork = if(info.isEmpty()) true | val runNewWork = if(info.isEmpty()) true | ||||
| else info[0].state!= WorkInfo.State.RUNNING && info[0].state!= WorkInfo.State.ENQUEUED | else info[0].state!= WorkInfo.State.RUNNING && info[0].state!= WorkInfo.State.ENQUEUED | ||||
| val addDat = if(info.isEmpty()) | val addDat = if(info.isEmpty()) | ||||
| null else info[0].state | null else info[0].state | ||||
| Log.d(debugTag, "Request to download and insert ${trips.size} trips, proceed: $runNewWork, workstate: $addDat") | Log.d(debugTag, "Request to download and insert ${trips.size} trips, proceed: $runNewWork, workstate: $addDat") | ||||
| if(runNewWork) { | if(runNewWork) { | ||||
| val tripsArr = trips.toTypedArray() | val tripsArr = trips.toTypedArray() | ||||
| val dataBuilder = Data.Builder().putStringArray(TRIPS_KEYS, tripsArr) | val dataBuilder = Data.Builder().putStringArray(TRIPS_KEYS, tripsArr) | ||||
| //build() | //build() | ||||
| val requ = OneTimeWorkRequest.Builder(MatoTripsDownloadWorker::class.java) | val requ = OneTimeWorkRequest.Builder(MatoTripsDownloadWorker::class.java) | ||||
| .setInputData(dataBuilder.build()).setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST) | .setInputData(dataBuilder.build()).setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST) | ||||
| .addTag(TAG_TRIPS) | .addTag(TAG_TRIPS) | ||||
| .build() | .build() | ||||
| workManager.enqueueUniqueWork(TAG_TRIPS, ExistingWorkPolicy.KEEP, requ) | workManager.enqueueUniqueWork(TAG_TRIPS, ExistingWorkPolicy.KEEP, requ) | ||||
| } | return requ | ||||
| return true | } else return null; | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register