Skip to content

Commit 3b86712

Browse files
committed
WorkManager worker to preload local media
1 parent df4d6fe commit 3b86712

File tree

6 files changed

+100
-4
lines changed

6 files changed

+100
-4
lines changed

bazaar/build.gradle

+3
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,7 @@ dependencies {
7272
// Kotlin coroutines
7373
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2'
7474
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.2'
75+
76+
// WorkManager
77+
implementation "androidx.work:work-runtime-ktx:2.5.0"
7578
}

bazaar/src/main/java/kz/zhombie/bazaar/Bazaar.kt

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import kz.zhombie.bazaar.api.core.ImageLoader
77
import kz.zhombie.bazaar.api.core.exception.ImageLoaderNullException
88
import kz.zhombie.bazaar.api.core.settings.CameraSettings
99
import kz.zhombie.bazaar.api.core.settings.Mode
10+
import kz.zhombie.bazaar.api.core.worker.MediaSyncWorker
1011
import kz.zhombie.bazaar.api.event.EventListener
1112
import kz.zhombie.bazaar.api.result.ResultCallback
1213
import kz.zhombie.bazaar.core.media.MediaScanManager
@@ -23,10 +24,19 @@ class Bazaar private constructor() {
2324
Settings.setLoggingEnabled(isLoggingEnabled)
2425
}
2526

27+
suspend fun sync(context: Context): Boolean {
28+
return MediaSyncWorker.startWork(context)
29+
}
30+
2631
suspend fun preload(context: Context, mode: Mode) {
2732
MediaScanManager.preload(context, mode)
2833
}
2934

35+
suspend fun preloadAll(context: Context) {
36+
MediaScanManager.preload(context, Mode.IMAGE_AND_VIDEO)
37+
MediaScanManager.preload(context, Mode.AUDIO)
38+
}
39+
3040
suspend fun clearCache() {
3141
MediaScanManager.clearCache()
3242
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package kz.zhombie.bazaar.api.core.worker
2+
3+
import android.content.Context
4+
import androidx.work.*
5+
import kz.zhombie.bazaar.Bazaar
6+
import kz.zhombie.bazaar.api.core.settings.Mode
7+
8+
class MediaSyncWorker constructor(
9+
private val context: Context,
10+
params: WorkerParameters
11+
) : CoroutineWorker(context, params) {
12+
13+
companion object {
14+
private val TAG = MediaSyncWorker::class.java.simpleName
15+
16+
const val UNIQUE_WORK_NAME = "BazaarMediaSyncWork"
17+
18+
suspend fun startWork(context: Context): Boolean {
19+
val constraints = Constraints.Builder()
20+
.setRequiresBatteryNotLow(true)
21+
.setRequiresCharging(false)
22+
.setRequiresStorageNotLow(false)
23+
.setRequiresDeviceIdle(false)
24+
.setRequiredNetworkType(NetworkType.NOT_REQUIRED)
25+
.build()
26+
27+
val operation = WorkManager.getInstance(context)
28+
.enqueueUniqueWork(
29+
UNIQUE_WORK_NAME,
30+
ExistingWorkPolicy.REPLACE,
31+
OneTimeWorkRequestBuilder<MediaSyncWorker>()
32+
.setConstraints(constraints)
33+
.build()
34+
)
35+
return try {
36+
val success = operation.await()
37+
@Suppress("USELESS_IS_CHECK")
38+
return success is Operation.State.SUCCESS
39+
} catch (e: Exception) {
40+
e.printStackTrace()
41+
false
42+
}
43+
}
44+
45+
suspend fun cancelWork(context: Context): Boolean {
46+
val operation = WorkManager.getInstance(context)
47+
.cancelUniqueWork(UNIQUE_WORK_NAME)
48+
49+
return try {
50+
val success = operation.await()
51+
@Suppress("USELESS_IS_CHECK")
52+
return success is Operation.State.SUCCESS
53+
} catch (e: Exception) {
54+
e.printStackTrace()
55+
false
56+
}
57+
}
58+
}
59+
60+
override suspend fun doWork(): Result {
61+
Bazaar.preload(context, Mode.IMAGE_AND_VIDEO)
62+
Bazaar.preload(context, Mode.AUDIO)
63+
return Result.success()
64+
}
65+
66+
}

bazaar/src/main/java/kz/zhombie/bazaar/core/cache/Cache.kt

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ internal class Cache private constructor() {
2929
}
3030
}
3131

32+
fun isEmpty(): Boolean {
33+
return multimedia.isNullOrEmpty()
34+
}
35+
3236
suspend fun getMultimedia(): List<Multimedia>? = withContext(Dispatchers.IO) {
3337
Logger.d(TAG, "getMultimedia() -> multimedia: ${multimedia?.size}")
3438
return@withContext multimedia

bazaar/src/main/java/kz/zhombie/bazaar/core/media/MediaScanManager.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ internal class MediaScanManager constructor(private val context: Context) {
5050
Mode.IMAGE -> mediaScanManager.loadLocalMediaImages()
5151
Mode.VIDEO -> mediaScanManager.loadLocalMediaVideos()
5252
Mode.IMAGE_AND_VIDEO -> mediaScanManager.loadLocalMediaImagesAndVideos()
53-
else ->
54-
null
53+
else -> null
5554
}
5655
Logger.d(TAG, "preload() -> media: ${media?.size}")
5756
if (!media.isNullOrEmpty()) {

sample/src/main/java/kz/zhombie/bazaar/MainActivity.kt

+16-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@ import android.Manifest
44
import android.content.pm.PackageManager
55
import android.os.Bundle
66
import android.util.Log
7+
import android.widget.Toast
78
import androidx.appcompat.app.AppCompatActivity
89
import androidx.core.app.ActivityCompat
910
import androidx.lifecycle.lifecycleScope
1011
import androidx.recyclerview.widget.RecyclerView
12+
import androidx.work.*
1113
import com.google.android.material.button.MaterialButton
1214
import com.google.android.material.dialog.MaterialAlertDialogBuilder
1315
import com.google.android.material.textview.MaterialTextView
1416
import kotlinx.coroutines.Dispatchers
1517
import kotlinx.coroutines.launch
18+
import kotlinx.coroutines.withContext
1619
import kz.zhombie.bazaar.api.core.ImageLoader
1720
import kz.zhombie.bazaar.api.core.settings.CameraSettings
1821
import kz.zhombie.bazaar.api.core.settings.Mode
@@ -150,9 +153,20 @@ class MainActivity : AppCompatActivity(), ResultCallback {
150153
}
151154
}
152155

156+
// lifecycleScope.launch(Dispatchers.IO) {
157+
// Bazaar.preloadAll(this@MainActivity)
158+
// }
159+
}
160+
161+
override fun onResume() {
162+
super.onResume()
163+
153164
lifecycleScope.launch(Dispatchers.IO) {
154-
Bazaar.preload(this@MainActivity, Mode.IMAGE_AND_VIDEO)
155-
Bazaar.preload(this@MainActivity, Mode.AUDIO)
165+
withContext(Dispatchers.Main) {
166+
Toast.makeText(this@MainActivity, "launch", Toast.LENGTH_SHORT).show()
167+
}
168+
169+
Bazaar.sync(applicationContext)
156170
}
157171
}
158172

0 commit comments

Comments
 (0)