DroidFS/app/src/main/java/sushi/hardcore/droidfs/adapters/SavedVolumesAdapter.kt

109 lines
4.3 KiB
Kotlin
Raw Normal View History

2020-07-17 16:35:39 +02:00
package sushi.hardcore.droidfs.adapters
import android.content.Context
import android.content.SharedPreferences
import android.content.SharedPreferences.Editor
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
import sushi.hardcore.droidfs.ConstValues
import sushi.hardcore.droidfs.R
import sushi.hardcore.droidfs.util.WidgetUtil
2020-07-27 16:20:52 +02:00
import sushi.hardcore.droidfs.widgets.ColoredAlertDialogBuilder
2020-08-05 14:06:54 +02:00
import sushi.hardcore.droidfs.widgets.NonScrollableColoredBorderListView
2020-07-17 16:35:39 +02:00
import java.util.*
2020-08-05 14:06:54 +02:00
class SavedVolumesAdapter(val context: Context, private val sharedPrefs: SharedPreferences) : BaseAdapter() {
2020-07-17 16:35:39 +02:00
private val inflater: LayoutInflater = LayoutInflater.from(context)
2020-08-05 14:06:54 +02:00
private lateinit var nonScrollableColoredBorderListView: NonScrollableColoredBorderListView
private val savedVolumesPaths: MutableList<String> = ArrayList()
private val sharedPrefsEditor: Editor = sharedPrefs.edit()
2020-07-17 16:35:39 +02:00
init {
2020-08-05 14:06:54 +02:00
val savedVolumesPathsSet = sharedPrefs.getStringSet(ConstValues.saved_volumes_key, HashSet()) as Set<String>
for (volume_path in savedVolumesPathsSet) {
savedVolumesPaths.add(volume_path)
2020-07-17 16:35:39 +02:00
}
}
2020-08-05 14:06:54 +02:00
private fun updateSharedPrefs() {
val savedVolumesPathsSet = savedVolumesPaths.toSet()
sharedPrefsEditor.remove(ConstValues.saved_volumes_key)
sharedPrefsEditor.putStringSet(ConstValues.saved_volumes_key, savedVolumesPathsSet)
sharedPrefsEditor.apply()
2020-07-17 16:35:39 +02:00
}
override fun getCount(): Int {
2020-08-05 14:06:54 +02:00
return savedVolumesPaths.size
2020-07-17 16:35:39 +02:00
}
override fun getItem(position: Int): String {
2020-08-05 14:06:54 +02:00
return savedVolumesPaths[position]
2020-07-17 16:35:39 +02:00
}
override fun getItemId(position: Int): Long {
return 0
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
2020-08-05 14:06:54 +02:00
if (!::nonScrollableColoredBorderListView.isInitialized){
nonScrollableColoredBorderListView = parent as NonScrollableColoredBorderListView
}
2020-07-17 16:35:39 +02:00
val view: View = convertView ?: inflater.inflate(R.layout.adapter_saved_volume, parent, false)
2020-08-05 14:06:54 +02:00
val volumeNameTextview = view.findViewById<TextView>(R.id.volume_name_textview)
2020-07-17 16:35:39 +02:00
val currentVolume = getItem(position)
2020-08-05 14:06:54 +02:00
volumeNameTextview.text = currentVolume
val deleteImageview = view.findViewById<ImageView>(R.id.delete_imageview)
deleteImageview.setOnClickListener {
val volumePath = savedVolumesPaths[position]
2020-07-27 16:20:52 +02:00
val dialog = ColoredAlertDialogBuilder(context)
2020-07-17 16:35:39 +02:00
dialog.setTitle(R.string.warning)
2020-08-05 14:06:54 +02:00
if (sharedPrefs.getString(volumePath, null) != null){
2020-07-17 16:35:39 +02:00
dialog.setMessage(context.getString(R.string.delete_hash_or_all))
dialog.setPositiveButton(context.getString(R.string.delete_all)) { _, _ ->
2020-08-05 14:06:54 +02:00
savedVolumesPaths.removeAt(position)
sharedPrefsEditor.remove(volumePath)
updateSharedPrefs()
2020-07-17 16:35:39 +02:00
refresh(parent)
}
dialog.setNegativeButton(context.getString(R.string.delete_hash)) { _, _ ->
2020-08-05 14:06:54 +02:00
sharedPrefsEditor.remove(volumePath)
sharedPrefsEditor.apply()
2020-07-17 16:35:39 +02:00
}
} else {
dialog.setMessage(context.getString(R.string.ask_delete_volume_path))
dialog.setPositiveButton(R.string.ok) {_, _ ->
2020-08-05 14:06:54 +02:00
savedVolumesPaths.removeAt(position)
updateSharedPrefs()
2020-07-17 16:35:39 +02:00
refresh(parent)
}
dialog.setNegativeButton(R.string.cancel, null)
}
dialog.show()
}
return view
}
private fun refresh(parent: ViewGroup) {
notifyDataSetChanged()
if (count == 0){
WidgetUtil.hide(parent)
2020-08-05 14:06:54 +02:00
} else {
nonScrollableColoredBorderListView.layoutParams.height = nonScrollableColoredBorderListView.computeHeight()
2020-07-17 16:35:39 +02:00
}
}
2020-10-15 17:43:13 +02:00
fun isPathSaved(volume_path: String): Boolean {
return savedVolumesPaths.contains(volume_path)
}
2020-07-17 16:35:39 +02:00
fun addVolumePath(volume_path: String) {
2020-10-15 17:43:13 +02:00
if (!isPathSaved(volume_path)) {
2020-08-05 14:06:54 +02:00
savedVolumesPaths.add(volume_path)
updateSharedPrefs()
2020-07-17 16:35:39 +02:00
}
}
}