1
1
package com.djangofiles.djangofiles.ui.files
2
2
3
+ import android.app.DownloadManager
3
4
import android.content.Context
4
5
import android.content.Intent
5
6
import android.net.ConnectivityManager
6
7
import android.os.Bundle
8
+ import android.os.Environment
7
9
import android.os.Handler
8
10
import android.os.Looper
9
11
import android.util.Log
@@ -29,6 +31,7 @@ import com.bumptech.glide.integration.okhttp3.OkHttpUrlLoader
29
31
import com.bumptech.glide.load.model.GlideUrl
30
32
import com.djangofiles.djangofiles.R
31
33
import com.djangofiles.djangofiles.ServerApi
34
+ import com.djangofiles.djangofiles.ServerApi.FileResponse
32
35
import com.djangofiles.djangofiles.ServerApi.FilesEditRequest
33
36
import com.djangofiles.djangofiles.databinding.FragmentFilesBinding
34
37
import com.djangofiles.djangofiles.db.AlbumDao
@@ -62,6 +65,7 @@ class FilesFragment : Fragment() {
62
65
63
66
private lateinit var api: ServerApi
64
67
private lateinit var filesAdapter: FilesViewAdapter
68
+ private lateinit var downloadManager: DownloadManager
65
69
66
70
private val viewModel: FilesViewModel by activityViewModels()
67
71
@@ -411,6 +415,30 @@ class FilesFragment : Fragment() {
411
415
}
412
416
requireContext().deleteConfirmDialog(ids, selectedPositions, ::callback)
413
417
}
418
+
419
+ downloadManager =
420
+ requireContext().getSystemService(Context .DOWNLOAD_SERVICE ) as DownloadManager
421
+ binding.downloadAllButton.setOnClickListener {
422
+ if (viewModel.selected.value.isNullOrEmpty()) return @setOnClickListener
423
+ val filesData = viewModel.filesData.value!! .toList()
424
+ val positions: List <Int > = viewModel.selected.value!! .toList()
425
+ Log .d(" File[downloadAllButton]" , " positions: $positions " )
426
+ fun callback () {
427
+ // val fileUrls: List<String> = positions.map { index -> "${savedUrl}/raw/${data[index].name}" }
428
+ // Log.d("File[callback]", "fileUrls: $fileUrls")
429
+ lifecycleScope.launch {
430
+ for (pos in positions) {
431
+ val data = filesData[pos]
432
+ Log .d(" File[callback]" , " data: $data " )
433
+ val request = getDownloadRequest(data)
434
+ val downloadId = downloadManager.enqueue(request)
435
+ Log .d(" downloadButton" , " Download ID: $downloadId " )
436
+ }
437
+ }
438
+ }
439
+ requireContext().downloadConfirmDialog(positions, ::callback)
440
+ }
441
+
414
442
binding.expireAllButton.setOnClickListener {
415
443
Log .d(" File[expireAllButton]" , " viewModel.selected.value: ${viewModel.selected.value} " )
416
444
fun callback (newExpr : String ) {
@@ -426,6 +454,7 @@ class FilesFragment : Fragment() {
426
454
Log .d(" File[expireAllButton]" , " fileIds: $fileIds " )
427
455
requireContext().showExpireDialog(fileIds, ::callback)
428
456
}
457
+
429
458
binding.albumAllButton.setOnClickListener {
430
459
Log .d(" File[albumAllButton]" , " viewModel.selected.value: ${viewModel.selected.value} " )
431
460
setFragmentResultListener(" albums_result" ) { _, bundle ->
@@ -604,6 +633,33 @@ class FilesFragment : Fragment() {
604
633
}
605
634
.show()
606
635
}
636
+
637
+ private fun Context.downloadConfirmDialog (positions : List <Int >, callback : () -> Unit ) {
638
+ Log .d(" downloadConfirmDialog" , " positions: $positions " )
639
+ val count = positions.count()
640
+ val s = if (count > 1 ) " s" else " "
641
+ MaterialAlertDialogBuilder (this , R .style.AlertDialogTheme )
642
+ .setTitle(" Download $count File${s} " )
643
+ .setIcon(R .drawable.md_download_24px)
644
+ .setMessage(" Files are saved to the Downloads directory." )
645
+ .setNegativeButton(" Cancel" , null )
646
+ .setPositiveButton(" Download $count File${s} " ) { _, _ -> callback() }
647
+ .show()
648
+ }
649
+
650
+ fun getDownloadRequest (data : FileResponse ): DownloadManager .Request {
651
+ Log .d(" getDownloadRequest" , " ${data.name} - ${data.raw} " )
652
+ return DownloadManager .Request (data.raw.toUri()).apply {
653
+ setTitle(data.name)
654
+ setMimeType(data.mime)
655
+ setDescription(" Django Files" )
656
+ setNotificationVisibility(DownloadManager .Request .VISIBILITY_VISIBLE_NOTIFY_COMPLETED )
657
+ setDestinationInExternalPublicDir(Environment .DIRECTORY_DOWNLOADS , data.name)
658
+ setAllowedOverMetered(true )
659
+ setAllowedOverRoaming(true )
660
+ setRequiresCharging(false )
661
+ }
662
+ }
607
663
}
608
664
609
665
fun Context.showExpireDialog (
0 commit comments