Android Download Service Fixes

This commit is contained in:
shabinder 2021-09-05 17:14:04 +05:30
parent 9525c40e8c
commit 3a93cd5f91
3 changed files with 30 additions and 10 deletions

View File

@ -82,12 +82,11 @@ import java.io.File
@ExperimentalAnimationApi @ExperimentalAnimationApi
class MainActivity : ComponentActivity() { class MainActivity : ComponentActivity() {
private lateinit var root: SpotiFlyerRoot
private val fetcher: FetchPlatformQueryResult by inject() private val fetcher: FetchPlatformQueryResult by inject()
private val fileManager: FileManager by inject() private val fileManager: FileManager by inject()
private val preferenceManager: PreferenceManager by inject() private val preferenceManager: PreferenceManager by inject()
private val analyticsManager: AnalyticsManager by inject { parametersOf(this) } private val analyticsManager: AnalyticsManager by inject { parametersOf(this) }
private val callBacks: SpotiFlyerRootCallBacks get() = root.callBacks private val callBacks: SpotiFlyerRootCallBacks get() = this.rootComponent.callBacks
private val trackStatusFlow = MutableSharedFlow<HashMap<String, DownloadStatus>>(1) private val trackStatusFlow = MutableSharedFlow<HashMap<String, DownloadStatus>>(1)
private var permissionGranted = mutableStateOf(true) private var permissionGranted = mutableStateOf(true)
private val internetAvailability by lazy { ConnectionLiveData(applicationContext) } private val internetAvailability by lazy { ConnectionLiveData(applicationContext) }
@ -105,7 +104,7 @@ class MainActivity : ComponentActivity() {
preferenceManager.analyticsManager = analyticsManager preferenceManager.analyticsManager = analyticsManager
// This app draws behind the system bars, so we want to handle fitting system windows // This app draws behind the system bars, so we want to handle fitting system windows
WindowCompat.setDecorFitsSystemWindows(window, false) WindowCompat.setDecorFitsSystemWindows(window, false)
rootComponent = spotiFlyerRoot(defaultComponentContext()) this.rootComponent = spotiFlyerRoot(defaultComponentContext())
setContent { setContent {
SpotiFlyerTheme { SpotiFlyerTheme {
Surface(contentColor = colorOffWhite) { Surface(contentColor = colorOffWhite) {
@ -114,8 +113,8 @@ class MainActivity : ComponentActivity() {
val view = LocalView.current val view = LocalView.current
Box { Box {
root = SpotiFlyerRootContent( SpotiFlyerRootContent(
rootComponent, this@MainActivity.rootComponent,
Modifier.statusBarsPadding().navigationBarsPadding() Modifier.statusBarsPadding().navigationBarsPadding()
) )
Spacer( Spacer(
@ -284,7 +283,7 @@ class MainActivity : ComponentActivity() {
override fun sendTracksToService(array: List<TrackDetails>) { override fun sendTracksToService(array: List<TrackDetails>) {
for (chunk in array.chunked(25)) { for (chunk in array.chunked(25)) {
if (foregroundService == null) initForegroundService() if (foregroundService == null) initForegroundService()
foregroundService?.downloadAllTracks(array) foregroundService?.downloadAllTracks(chunk)
} }
} }
} }
@ -429,7 +428,7 @@ class MainActivity : ComponentActivity() {
val link = filterLinkRegex.find(string)?.value.toString() val link = filterLinkRegex.find(string)?.value.toString()
Log.i("Intent", link) Log.i("Intent", link)
lifecycleScope.launch { lifecycleScope.launch {
while (!this@MainActivity::root.isInitialized) { while (!this@MainActivity::rootComponent.isInitialized) {
delay(100) delay(100)
} }
if (methods.value.isInternetAvailable) callBacks.searchLink(link) if (methods.value.isInternetAvailable) callBacks.searchLink(link)

View File

@ -200,7 +200,7 @@ class ForegroundService : LifecycleService() {
// All Processing Completed for this Track // All Processing Completed for this Track
job.invokeOnCompletion { throwable -> job.invokeOnCompletion { throwable ->
if (throwable != null && throwable !is CancellationException) { if (throwable != null /*&& throwable !is CancellationException*/) {
// handle error // handle error
failed++ failed++
trackStatusFlowMap[track.title] = trackStatusFlowMap[track.title] =
@ -276,6 +276,7 @@ class ForegroundService : LifecycleService() {
} }
downloadService.close() downloadService.close()
updateNotification() updateNotification()
trackStatusFlowMap.clear()
cleanFiles(File(dir.defaultDir())) cleanFiles(File(dir.defaultDir()))
// cleanFiles(File(dir.imageCacheDir())) // cleanFiles(File(dir.imageCacheDir()))
messageList = messageList.getEmpty() messageList = messageList.getEmpty()

View File

@ -10,8 +10,28 @@ class TrackStatusFlowMap(
private val scope: CoroutineScope private val scope: CoroutineScope
) : HashMap<String, DownloadStatus>() { ) : HashMap<String, DownloadStatus>() {
override fun put(key: String, value: DownloadStatus): DownloadStatus? { override fun put(key: String, value: DownloadStatus): DownloadStatus? {
val res = super.put(key, value) synchronized(this) {
val res = super.put(key, value)
emitValue()
return res
}
}
override fun clear() {
synchronized(this) {
// Reset Statuses
this.forEach { (title, status) ->
if(status !is DownloadStatus.Failed && status !is DownloadStatus.Downloaded) {
super.put(title,DownloadStatus.NotDownloaded)
}
}
emitValue()
//super.clear()
//emitValue()
}
}
private fun emitValue() {
scope.launch { statusFlow.emit(this@TrackStatusFlowMap) } scope.launch { statusFlow.emit(this@TrackStatusFlowMap) }
return res
} }
} }