From 7b890eae3e89737f5ede133922bfb64d71f1eb7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Tue, 11 Jul 2023 10:48:38 +0200 Subject: [PATCH] Make PropertiesDialog reusable for stuff other than files --- .../commons/dialogs/BasePropertiesDialog.kt | 68 +++++++++++++++++++ .../commons/dialogs/PropertiesDialog.kt | 54 +-------------- 2 files changed, 71 insertions(+), 51 deletions(-) create mode 100644 commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/BasePropertiesDialog.kt diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/BasePropertiesDialog.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/BasePropertiesDialog.kt new file mode 100644 index 000000000..9a4cf2204 --- /dev/null +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/BasePropertiesDialog.kt @@ -0,0 +1,68 @@ +package com.simplemobiletools.commons.dialogs + +import android.app.Activity +import android.content.res.Resources +import android.net.Uri +import android.os.Environment +import android.provider.MediaStore +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.LinearLayout +import android.widget.TextView +import androidx.appcompat.app.AlertDialog +import androidx.exifinterface.media.ExifInterface +import com.simplemobiletools.commons.R +import com.simplemobiletools.commons.activities.BaseSimpleActivity +import com.simplemobiletools.commons.extensions.* +import com.simplemobiletools.commons.helpers.* +import com.simplemobiletools.commons.models.FileDirItem +import kotlinx.android.synthetic.main.dialog_properties.view.* +import kotlinx.android.synthetic.main.item_property.view.* +import java.io.File +import java.util.* + +abstract class BasePropertiesDialog(activity: Activity) { + protected val mInflater: LayoutInflater + protected val mPropertyView: ViewGroup + protected val mResources: Resources + protected val mActivity: Activity = activity + protected val mDialogView: View + + init { + mInflater = LayoutInflater.from(activity) + mResources = activity.resources + mDialogView = mInflater.inflate(R.layout.dialog_properties, null) + mPropertyView = mDialogView.properties_holder!! + } + + protected fun addProperty(labelId: Int, value: String?, viewId: Int = 0) { + if (value == null) { + return + } + + mInflater.inflate(R.layout.item_property, mPropertyView, false).apply { + property_value.setTextColor(mActivity.getProperTextColor()) + property_label.setTextColor(mActivity.getProperTextColor()) + + property_label.text = mResources.getString(labelId) + property_value.text = value + mPropertyView.properties_holder.addView(this) + + setOnLongClickListener { + mActivity.copyToClipboard(property_value.value) + true + } + + if (labelId == R.string.gps_coordinates) { + setOnClickListener { + mActivity.showLocationOnMap(value) + } + } + + if (viewId != 0) { + id = viewId + } + } + } +} diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/PropertiesDialog.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/PropertiesDialog.kt index 8345443e1..3039bd3b5 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/PropertiesDialog.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/PropertiesDialog.kt @@ -1,13 +1,10 @@ package com.simplemobiletools.commons.dialogs import android.app.Activity -import android.content.res.Resources import android.net.Uri import android.os.Environment import android.provider.MediaStore -import android.view.LayoutInflater import android.view.View -import android.view.ViewGroup import android.widget.LinearLayout import android.widget.TextView import androidx.appcompat.app.AlertDialog @@ -22,12 +19,7 @@ import kotlinx.android.synthetic.main.item_property.view.* import java.io.File import java.util.* -class PropertiesDialog() { - private lateinit var mInflater: LayoutInflater - private lateinit var mPropertyView: ViewGroup - private lateinit var mResources: Resources - private lateinit var mActivity: Activity - private lateinit var mDialogView: View +class PropertiesDialog : BasePropertiesDialog { private var mCountHiddenItems = false /** @@ -37,18 +29,13 @@ class PropertiesDialog() { * @param path the file path * @param countHiddenItems toggle determining if we will count hidden files themselves and their sizes (reasonable only at directory properties) */ - constructor(activity: Activity, path: String, countHiddenItems: Boolean = false) : this() { + constructor(activity: Activity, path: String, countHiddenItems: Boolean = false) : super(activity) { if (!activity.getDoesFilePathExist(path) && !path.startsWith("content://")) { activity.toast(String.format(activity.getString(R.string.source_file_doesnt_exist), path)) return } - mActivity = activity - mInflater = LayoutInflater.from(activity) - mResources = activity.resources - mDialogView = mInflater.inflate(R.layout.dialog_properties, null) mCountHiddenItems = countHiddenItems - mPropertyView = mDialogView.properties_holder!! addProperties(path) val builder = activity.getAlertDialogBuilder() @@ -215,13 +202,8 @@ class PropertiesDialog() { * @param path the file path * @param countHiddenItems toggle determining if we will count hidden files themselves and their sizes */ - constructor(activity: Activity, paths: List, countHiddenItems: Boolean = false) : this() { - mActivity = activity - mInflater = LayoutInflater.from(activity) - mResources = activity.resources - mDialogView = mInflater.inflate(R.layout.dialog_properties, null) + constructor(activity: Activity, paths: List, countHiddenItems: Boolean = false) : super(activity) { mCountHiddenItems = countHiddenItems - mPropertyView = mDialogView.properties_holder val fileDirItems = ArrayList(paths.size) paths.forEach { @@ -340,34 +322,4 @@ class PropertiesDialog() { } return true } - - private fun addProperty(labelId: Int, value: String?, viewId: Int = 0) { - if (value == null) { - return - } - - mInflater.inflate(R.layout.item_property, mPropertyView, false).apply { - property_value.setTextColor(mActivity.getProperTextColor()) - property_label.setTextColor(mActivity.getProperTextColor()) - - property_label.text = mResources.getString(labelId) - property_value.text = value - mPropertyView.properties_holder.addView(this) - - setOnLongClickListener { - mActivity.copyToClipboard(property_value.value) - true - } - - if (labelId == R.string.gps_coordinates) { - setOnClickListener { - mActivity.showLocationOnMap(value) - } - } - - if (viewId != 0) { - id = viewId - } - } - } }