1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-12-23 05:16:58 +00:00

improve downloading (creating files)

* creating files is offloaded to task thread so it doesn't hang the ui
* add notification to inform the above happening
This commit is contained in:
Austin Huang 2021-07-01 20:26:34 -04:00
parent b1f31ab736
commit 0e788c9889
No known key found for this signature in database
GPG Key ID: 84C23AA04587A91F
2 changed files with 23 additions and 5 deletions

View File

@ -9,6 +9,8 @@ import android.view.MenuItem
import android.view.View
import androidx.appcompat.view.ContextThemeWrapper
import androidx.appcompat.widget.PopupMenu
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.documentfile.provider.DocumentFile
import androidx.work.*
import awais.instagrabber.R
@ -16,6 +18,7 @@ import awais.instagrabber.fragments.settings.PreferenceKeys
import awais.instagrabber.models.enums.MediaItemType
import awais.instagrabber.repositories.responses.Media
import awais.instagrabber.repositories.responses.stories.StoryMedia
import awais.instagrabber.utils.AppExecutors.tasksThread
import awais.instagrabber.utils.TextUtils.isEmpty
import awais.instagrabber.workers.DownloadWorker
import com.google.gson.Gson
@ -24,6 +27,7 @@ import java.io.IOException
import java.io.OutputStreamWriter
import java.util.*
import java.util.regex.Pattern
import kotlin.math.abs
object DownloadUtils {
@ -211,7 +215,6 @@ object DownloadUtils {
return Pair(fileName, mimeType!!)
}
// can't convert to checkFiles() due to lack of Context
fun getTempFile(fileName: String?, extension: String): DocumentFile? {
val dir = tempDir
var name = fileName
@ -382,12 +385,28 @@ object DownloadUtils {
download(context, listOf(feedModel), position)
}
// this must be used for bulk download, but ONLY bulk download
@JvmStatic
fun download(
context: Context,
feedModels: List<Media>
) {
download(context, feedModels, -1)
val builder = NotificationCompat.Builder(context, Constants.DOWNLOAD_CHANNEL_ID)
.setCategory(NotificationCompat.CATEGORY_PROGRESS)
.setSmallIcon(R.drawable.ic_download)
.setOngoing(true)
.setProgress(1, 0, true)
.setAutoCancel(false)
.setOnlyAlertOnce(true)
.setContentTitle(context.getString(R.string.downloader_preparing))
val notification = builder.build()
val nid = abs(UUID.randomUUID().hashCode())
val nManager = NotificationManagerCompat.from(context.applicationContext)
nManager.notify(nid, notification)
tasksThread.execute {
download(context, feedModels, -1)
nManager.cancel(nid)
}
}
private fun download(
@ -438,9 +457,7 @@ object DownloadUtils {
val url = getUrlOfType(child)
val usernamePrepend =
if (Utils.settingsHelper.getBoolean(PreferenceKeys.DOWNLOAD_PREPEND_USER_NAME) && mediaUser != null) mediaUser.username else ""
val pair = getDownloadChildSavePaths(
media.code, i + 1, url, usernamePrepend
)
val pair = getDownloadChildSavePaths(media.code, i + 1, url, usernamePrepend)
map[url!!] = pair
i++
}

View File

@ -205,6 +205,7 @@
<string name="direct_download_desc">Downloads posts directly to the phone!</string>
<string name="direct_download_loading">Fetching post(s)</string>
<string name="downloader_complete">Download completed</string>
<string name="downloader_preparing">Preparing to download…</string>
<string name="downloader_downloading_post">Downloading post…</string>
<string name="downloader_downloading_media">Downloading media</string>
<string name="downloader_downloading_pfp">Downloading profile picture</string>