Skip to content

Commit

Permalink
Fixed: Still displaying old storage configurator (at first download).
Browse files Browse the repository at this point in the history
  • Loading branch information
MohitMaliDeveloper committed Nov 4, 2024
1 parent 1576095 commit 9573253
Show file tree
Hide file tree
Showing 57 changed files with 215 additions and 242 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import org.kiwix.kiwixmobile.Findable.Text
import org.kiwix.kiwixmobile.Findable.ViewId
import org.kiwix.kiwixmobile.R
import org.kiwix.kiwixmobile.core.R.string
import org.kiwix.kiwixmobile.core.R.id
import org.kiwix.kiwixmobile.core.utils.files.Log
import org.kiwix.kiwixmobile.testutils.TestUtils
import org.kiwix.kiwixmobile.testutils.TestUtils.testFlakyView
Expand Down Expand Up @@ -98,7 +99,7 @@ class InitialDownloadRobot : BaseRobot() {
}

fun assertStorageConfigureDialogDisplayed() {
testFlakyView({ isVisible(Text("Download book to internal storage?")) })
testFlakyView({ onView(withText(string.choose_storage_to_download_book)) })
}

fun assertStopDownloadDialogDisplayed() {
Expand All @@ -109,6 +110,17 @@ class InitialDownloadRobot : BaseRobot() {
testFlakyView({ onView(withText("YES")).perform(click()) })
}

fun clickOnInternalStorage() {
pauseForBetterTestPerformance()
testFlakyView({
onView(
RecyclerViewMatcher(id.device_list).atPosition(
0
)
).perform(click())
})
}

fun assertDownloadStart() {
testFlakyView({ isVisible(ViewId(R.id.stop)) }, 10)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class InitialDownloadTest : BaseActivityTest() {
stopDownloadIfAlreadyStarted()
downloadZimFile()
assertStorageConfigureDialogDisplayed()
clickOnYesToConfirm()
clickOnInternalStorage()
assertDownloadStart()
stopDownload()
assertStopDownloadDialogDisplayed()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import eu.mhutti1.utils.storage.STORAGE_SELECT_STORAGE_TITLE_TEXTVIEW_SIZE
import eu.mhutti1.utils.storage.StorageDevice
import eu.mhutti1.utils.storage.StorageSelectDialog
import org.kiwix.kiwixmobile.R
Expand Down Expand Up @@ -419,6 +420,7 @@ class OnlineLibraryFragment : BaseFragment(), FragmentActivityExtensions {
private fun storeDeviceInPreferences(
storageDevice: StorageDevice
) {
sharedPreferenceUtil.showStorageOption = false
sharedPreferenceUtil.putPrefStorage(
sharedPreferenceUtil.getPublicDirectoryPath(storageDevice.name)
)
Expand Down Expand Up @@ -528,7 +530,7 @@ class OnlineLibraryFragment : BaseFragment(), FragmentActivityExtensions {
}

else -> if (sharedPreferenceUtil.showStorageOption) {
showStorageConfigureDialog()
showStorageSelectDialog()
} else if (!requireActivity().isManageExternalStoragePermissionGranted(
sharedPreferenceUtil
)
Expand Down Expand Up @@ -561,22 +563,9 @@ class OnlineLibraryFragment : BaseFragment(), FragmentActivityExtensions {
private fun showStorageSelectDialog() = StorageSelectDialog()
.apply {
onSelectAction = ::storeDeviceInPreferences
titleSize = STORAGE_SELECT_STORAGE_TITLE_TEXTVIEW_SIZE
}
.show(parentFragmentManager, getString(string.pref_storage))

private fun showStorageConfigureDialog() {
alertDialogShower.show(
KiwixDialog.StorageConfigure,
{
showStorageSelectDialog()
sharedPreferenceUtil.showStorageOption = false
},
{
sharedPreferenceUtil.showStorageOption = false
clickOnBookItem()
}
)
}
.show(parentFragmentManager, getString(string.choose_storage_to_download_book))

private fun clickOnBookItem() {
if (!requireActivity().isManageExternalStoragePermissionGranted(sharedPreferenceUtil)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import org.kiwix.kiwixmobile.core.R
import org.kiwix.kiwixmobile.core.extensions.getFreeSpace
import org.kiwix.kiwixmobile.core.extensions.getUsedSpace
import org.kiwix.kiwixmobile.core.extensions.storagePathAndTitle
import org.kiwix.kiwixmobile.core.extensions.usedPercentage
import org.kiwix.kiwixmobile.core.navigateToSettings
import org.kiwix.kiwixmobile.core.settings.CorePrefsFragment
import org.kiwix.kiwixmobile.core.settings.StorageRadioButtonPreference
Expand Down Expand Up @@ -80,69 +84,18 @@ class KiwixPrefsFragment : CorePrefsFragment() {
onStorageDeviceSelected(storageDevice)
true
}
setPathAndTitleForStorage(
buildStoragePathAndTitle(
storageDevice,
index,
selectedStoragePosition,
sharedPreferenceUtil
storageCalculator?.let {
setPathAndTitleForStorage(
storageDevice.storagePathAndTitle(context, index, sharedPreferenceUtil, it)
)
)
setFreeSpace(getFreeSpaceText(storageDevice))
setUsedSpace(getUsedSpaceText(storageDevice))
setProgress(calculateUsedPercentage(storageDevice))
setFreeSpace(storageDevice.getFreeSpace(context, it))
setUsedSpace(storageDevice.getUsedSpace(context, it))
setProgress(storageDevice.usedPercentage(it))
}
}
}
}

private fun getFreeSpaceText(storageDevice: StorageDevice): String {
val freeSpace = storageCalculator?.calculateAvailableSpace(storageDevice.file)
return getString(R.string.pref_free_storage, freeSpace)
}

private fun getUsedSpaceText(storageDevice: StorageDevice): String {
val usedSpace = storageCalculator?.calculateUsedSpace(storageDevice.file)
return getString(R.string.pref_storage_used, usedSpace)
}

private fun buildStoragePathAndTitle(
storageDevice: StorageDevice,
index: Int,
selectedStoragePosition: Int,
sharedPreferenceUtil: SharedPreferenceUtil
): String {
val storageName = if (storageDevice.isInternal) {
getString(R.string.internal_storage)
} else {
getString(R.string.external_storage)
}
val storagePath = if (index == selectedStoragePosition) {
sharedPreferenceUtil.prefStorage
} else {
getStoragePathForNonSelectedStorage(storageDevice, sharedPreferenceUtil)
}
val totalSpace = storageCalculator?.calculateTotalSpace(storageDevice.file)
return "$storageName - $totalSpace\n$storagePath/Kiwix"
}

private fun getStoragePathForNonSelectedStorage(
storageDevice: StorageDevice,
sharedPreferenceUtil: SharedPreferenceUtil
): String =
if (storageDevice.isInternal) {
sharedPreferenceUtil.getPublicDirectoryPath(storageDevice.name)
} else {
storageDevice.name
}

@Suppress("MagicNumber")
private fun calculateUsedPercentage(storageDevice: StorageDevice): Int {
val totalSpace = storageCalculator?.totalBytes(storageDevice.file) ?: 1
val availableSpace = storageCalculator?.availableBytes(storageDevice.file) ?: 0
val usedSpace = totalSpace - availableSpace
return (usedSpace.toDouble() / totalSpace * 100).toInt()
}

private fun showExternalPreferenceIfAvailable() {
findPreference<StorageRadioButtonPreference>(PREF_EXTERNAL_STORAGE)?.isVisible =
storageDeviceList.size > 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package eu.mhutti1.utils.storage

import android.content.res.Configuration
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
Expand All @@ -33,13 +34,19 @@ import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import org.kiwix.kiwixmobile.core.CoreApp
import org.kiwix.kiwixmobile.core.R.dimen
import org.kiwix.kiwixmobile.core.databinding.StorageSelectDialogBinding
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.isLandScapeMode
import org.kiwix.kiwixmobile.core.settings.StorageCalculator
import org.kiwix.kiwixmobile.core.utils.DimenUtils.getWindowWidth
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import javax.inject.Inject

const val STORAGE_SELECT_STORAGE_TITLE_TEXTVIEW_SIZE = 16F

class StorageSelectDialog : DialogFragment() {
var onSelectAction: ((StorageDevice) -> Unit)? = null
var titleSize: Float? = null

@Inject lateinit var storageCalculator: StorageCalculator
@Inject lateinit var sharedPreferenceUtil: SharedPreferenceUtil
Expand Down Expand Up @@ -68,7 +75,10 @@ class StorageSelectDialog : DialogFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
CoreApp.coreComponent.inject(this)
storageSelectDialogViewBinding?.title?.text = aTitle
storageSelectDialogViewBinding?.title?.apply {
text = aTitle
titleSize?.let(::setTextSize)
}
storageSelectDialogViewBinding?.deviceList?.run {
adapter = storageAdapter
layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
Expand All @@ -90,6 +100,31 @@ class StorageSelectDialog : DialogFragment() {
super.show(fm, text)
}

override fun onStart() {
super.onStart()
setStorageSelectDialogWidth()
}

override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
setStorageSelectDialogWidth()
}

Check warning on line 111 in core/src/main/java/eu/mhutti1/utils/storage/StorageSelectDialog.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/eu/mhutti1/utils/storage/StorageSelectDialog.kt#L109-L111

Added lines #L109 - L111 were not covered by tests

@Suppress("MagicNumber")
private fun setStorageSelectDialogWidth() {
val windowWidth = requireActivity().getWindowWidth()
val maximumStorageSelectDialogWidth =
requireActivity().resources.getDimensionPixelSize(dimen.maximum_donation_popup_width)

val width =
if (windowWidth > maximumStorageSelectDialogWidth || requireActivity().isLandScapeMode()) {
maximumStorageSelectDialogWidth
} else {
(windowWidth * 0.9).toInt()

Check warning on line 123 in core/src/main/java/eu/mhutti1/utils/storage/StorageSelectDialog.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/eu/mhutti1/utils/storage/StorageSelectDialog.kt#L123

Added line #L123 was not covered by tests
}
dialog?.window?.setLayout(width, ViewGroup.LayoutParams.WRAP_CONTENT)
}

override fun onDestroyView() {
super.onDestroyView()
storageDisposable?.dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import eu.mhutti1.utils.storage.StorageDevice
import org.kiwix.kiwixmobile.core.base.adapter.AdapterDelegate
import org.kiwix.kiwixmobile.core.databinding.DeviceItemBinding
import org.kiwix.kiwixmobile.core.databinding.ItemStoragePreferenceBinding
import org.kiwix.kiwixmobile.core.extensions.ViewGroupExtensions.viewBinding
import org.kiwix.kiwixmobile.core.settings.StorageCalculator
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
Expand All @@ -34,7 +34,7 @@ class StorageDelegate(
) : AdapterDelegate<StorageDevice> {
override fun createViewHolder(parent: ViewGroup): ViewHolder {
return StorageViewHolder(
parent.viewBinding(DeviceItemBinding::inflate, false),
parent.viewBinding(ItemStoragePreferenceBinding::inflate, false),
storageCalculator,
sharedPreferenceUtil,
onClickAction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,84 @@
package eu.mhutti1.utils.storage.adapter

import android.annotation.SuppressLint
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.AbsoluteSizeSpan
import android.view.View.VISIBLE
import eu.mhutti1.utils.storage.StorageDevice
import org.kiwix.kiwixmobile.core.R
import org.kiwix.kiwixmobile.core.base.adapter.BaseViewHolder
import org.kiwix.kiwixmobile.core.databinding.DeviceItemBinding
import org.kiwix.kiwixmobile.core.databinding.ItemStoragePreferenceBinding
import org.kiwix.kiwixmobile.core.downloader.downloadManager.ZERO
import org.kiwix.kiwixmobile.core.extensions.getFreeSpace
import org.kiwix.kiwixmobile.core.extensions.getUsedSpace
import org.kiwix.kiwixmobile.core.extensions.setToolTipWithContentDescription
import org.kiwix.kiwixmobile.core.extensions.storagePathAndTitle
import org.kiwix.kiwixmobile.core.extensions.usedPercentage
import org.kiwix.kiwixmobile.core.settings.StorageCalculator
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil

const val FREE_SPACE_TEXTVIEW_SIZE = 12F
const val STORAGE_TITLE_TEXTVIEW_SIZE = 15

@SuppressLint("SetTextI18n")
internal class StorageViewHolder(
private val deviceItemBinding: DeviceItemBinding,
private val itemStoragePreferenceBinding: ItemStoragePreferenceBinding,
private val storageCalculator: StorageCalculator,
private val sharedPreferenceUtil: SharedPreferenceUtil,
private val onClickAction: (StorageDevice) -> Unit
) : BaseViewHolder<StorageDevice>(deviceItemBinding.root) {
) : BaseViewHolder<StorageDevice>(itemStoragePreferenceBinding.root) {

override fun bind(item: StorageDevice) {
deviceItemBinding.fileName.setText(
if (item.isInternal) R.string.internal_storage
else R.string.external_storage
)
with(itemStoragePreferenceBinding) {
storagePathAndTitle.text =
resizeStoragePathAndTitle(
item.storagePathAndTitle(
root.context,
adapterPosition,
sharedPreferenceUtil,
storageCalculator
)
)

if (adapterPosition == sharedPreferenceUtil.storagePosition) {
deviceItemBinding.fileName.isChecked = true
}
deviceItemBinding.fileSize.text = storageCalculator.calculateAvailableSpace(item.file) + " / " +
storageCalculator.calculateTotalSpace(item.file) + " "
deviceItemBinding.clickOverlay.apply {
setToolTipWithContentDescription(
deviceItemBinding.root.context.getString(
R.string.storage_selection_dialog_accessibility_description
radioButton.isChecked =
adapterPosition == sharedPreferenceUtil.storagePosition
freeSpace.apply {
text = item.getFreeSpace(root.context, storageCalculator)
textSize = FREE_SPACE_TEXTVIEW_SIZE
}
usedSpace.apply {
text = item.getUsedSpace(root.context, storageCalculator)
textSize = FREE_SPACE_TEXTVIEW_SIZE
}
storageProgressBar.progress = item.usedPercentage(storageCalculator)
clickOverlay.apply {
visibility = VISIBLE
setToolTipWithContentDescription(
root.context.getString(
R.string.storage_selection_dialog_accessibility_description
)
)
)
setOnClickListener {
onClickAction.invoke(item)
setOnClickListener {
onClickAction.invoke(item)
}
}
}
}

private fun resizeStoragePathAndTitle(storagePathAndTitle: String): CharSequence =
SpannableStringBuilder(storagePathAndTitle).apply {
setSpan(
AbsoluteSizeSpan(STORAGE_TITLE_TEXTVIEW_SIZE, true),
ZERO,
storagePathAndTitle.indexOf('\n'),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
setSpan(
AbsoluteSizeSpan(FREE_SPACE_TEXTVIEW_SIZE.toInt(), true),
storagePathAndTitle.indexOf('\n') + 1,
storagePathAndTitle.length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
}
Loading

0 comments on commit 9573253

Please sign in to comment.