X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Fdialogs%2FSaveDialog.kt;h=63a6eaa301c0d8f76a41e0ab3b589b2c97d0719e;hp=77cd1c1acb7b16434b6c7614b58e71842450cb72;hb=c2b5bdf009503f6761dc830fb65502ad2910c284;hpb=58dbf864ccd5dd341517e1ff0adaf681476d4a4f diff --git a/app/src/main/java/com/stoutner/privacybrowser/dialogs/SaveDialog.kt b/app/src/main/java/com/stoutner/privacybrowser/dialogs/SaveDialog.kt index 77cd1c1a..63a6eaa3 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/dialogs/SaveDialog.kt +++ b/app/src/main/java/com/stoutner/privacybrowser/dialogs/SaveDialog.kt @@ -1,5 +1,5 @@ /* - * Copyright © 2019-2022 Soren Stoutner . + * Copyright 2019-2022 Soren Stoutner . * * This file is part of Privacy Browser Android . * @@ -22,7 +22,6 @@ package com.stoutner.privacybrowser.dialogs import android.app.Dialog import android.content.Context import android.content.DialogInterface -import android.os.AsyncTask import android.os.Bundle import android.text.Editable import android.text.InputType @@ -36,7 +35,14 @@ import androidx.fragment.app.DialogFragment import androidx.preference.PreferenceManager import com.stoutner.privacybrowser.R -import com.stoutner.privacybrowser.asynctasks.GetUrlSize +import com.stoutner.privacybrowser.helpers.GetUrlSizeHelper + +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext + +import java.net.URL // Define the class constants. private const val URL_STRING = "url_string" @@ -49,9 +55,6 @@ class SaveDialog : DialogFragment() { // Declare the class variables. private lateinit var saveListener: SaveListener - // Define the class variables. - private var getUrlSize: AsyncTask<*, *, *>? = null - // The public interface is used to send information back to the parent activity. interface SaveListener { fun onSaveUrl(originalUrlString: String, fileNameString: String, dialogFragment: DialogFragment) @@ -170,26 +173,29 @@ class SaveDialog : DialogFragment() { } override fun afterTextChanged(editable: Editable) { - // Cancel the get URL size AsyncTask if it is running. - if (getUrlSize != null) { - getUrlSize!!.cancel(true) - } - // Get the current URL to save. val urlToSave = urlEditText.text.toString() - // Wipe the file size text view. - fileSizeTextView.text = "" - - // Get the file size for the current URL. - getUrlSize = GetUrlSize(context, alertDialog, userAgentString, cookiesEnabled).execute(urlToSave) - // Enable the save button if the URL is populated. saveButton.isEnabled = urlToSave.isNotEmpty() + + CoroutineScope(Dispatchers.Main).launch { + // Create a URL size string. + var urlSize: String + + // Get the URL size on the IO thread. + withContext(Dispatchers.IO) { + // Get the URL size. + urlSize = GetUrlSizeHelper.getUrl(requireContext(), URL(urlToSave), userAgentString, cookiesEnabled) + } + + // Display the updated URL. + fileSizeTextView.text = urlSize + } } }) // Return the alert dialog. return alertDialog } -} \ No newline at end of file +}