2020-07-17 16:35:39 +02:00
package sushi.hardcore.droidfs.adapters
import android.content.Context
2021-11-11 19:46:26 +01:00
import android.content.res.ColorStateList
2020-07-17 16:35:39 +02:00
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.R
2020-11-04 18:42:04 +01:00
import sushi.hardcore.droidfs.Volume
import sushi.hardcore.droidfs.VolumeDatabase
2021-11-11 19:46:26 +01:00
import sushi.hardcore.droidfs.util.PathUtils
2020-07-17 16:35:39 +02:00
import sushi.hardcore.droidfs.util.WidgetUtil
2021-11-09 11:12:09 +01:00
import sushi.hardcore.droidfs.widgets.CustomAlertDialogBuilder
2020-08-05 14:06:54 +02:00
import sushi.hardcore.droidfs.widgets.NonScrollableColoredBorderListView
2020-11-04 18:42:04 +01:00
import java.io.File
2020-07-17 16:35:39 +02:00
2021-11-09 11:12:09 +01:00
class SavedVolumesAdapter ( private val context : Context , private val themeValue : String , private val volumeDatabase : VolumeDatabase ) : 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
2020-07-17 16:35:39 +02:00
2020-11-04 18:42:04 +01:00
override fun getCount ( ) : Int {
return volumeDatabase . getVolumes ( ) . size
2020-07-17 16:35:39 +02:00
}
2020-11-04 18:42:04 +01:00
override fun getItem ( position : Int ) : Volume {
return volumeDatabase . getVolumes ( ) [ position ]
2020-07-17 16:35:39 +02:00
}
2020-11-04 18:42:04 +01:00
override fun getItemId ( position : Int ) : Long {
return 0
2020-07-17 16:35:39 +02:00
}
2020-11-04 18:42:04 +01:00
private fun deletePasswordHash ( volume : Volume ) {
volumeDatabase . removeHash ( volume )
volume . hash = null
volume . iv = null
2020-07-17 16:35:39 +02:00
}
2020-11-04 18:42:04 +01:00
private fun deleteVolumeData ( volume : Volume , parent : ViewGroup ) {
volumeDatabase . removeVolume ( volume )
refresh ( parent )
2020-07-17 16:35:39 +02:00
}
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-11-04 18:42:04 +01:00
val volumeNameTextView = view . findViewById < TextView > ( R . id . volume _name _textview )
2020-07-17 16:35:39 +02:00
val currentVolume = getItem ( position )
2020-11-04 18:42:04 +01:00
volumeNameTextView . text = currentVolume . name
val deleteImageView = view . findViewById < ImageView > ( R . id . delete _imageview )
2021-11-11 19:46:26 +01:00
deleteImageView . imageTintList = ColorStateList . valueOf ( nonScrollableColoredBorderListView . colorAccent ) //fix a strange bug that sometimes displays the icon in white
2020-11-04 18:42:04 +01:00
deleteImageView . setOnClickListener {
2021-11-09 11:12:09 +01:00
val dialog = CustomAlertDialogBuilder ( context , themeValue )
2020-07-17 16:35:39 +02:00
dialog . setTitle ( R . string . warning )
2020-11-04 18:42:04 +01:00
if ( currentVolume . isHidden ) {
if ( currentVolume . hash != null ) {
dialog . setMessage ( R . string . hidden _volume _delete _question _hash )
dialog . setPositiveButton ( R . string . password _hash ) { _ , _ ->
deletePasswordHash ( currentVolume )
}
dialog . setNegativeButton ( R . string . password _hash _and _path ) { _ , _ ->
deleteVolumeData ( currentVolume , parent )
}
dialog . setNeutralButton ( R . string . whole _volume ) { _ , _ ->
PathUtils . recursiveRemoveDirectory ( File ( PathUtils . pathJoin ( context . filesDir . path , currentVolume . name ) ) )
deleteVolumeData ( currentVolume , parent )
}
} else {
dialog . setMessage ( R . string . hidden _volume _delete _question )
dialog . setPositiveButton ( R . string . path _only ) { _ , _ ->
deleteVolumeData ( currentVolume , parent )
}
dialog . setNegativeButton ( R . string . whole _volume ) { _ , _ ->
PathUtils . recursiveRemoveDirectory ( File ( PathUtils . pathJoin ( context . filesDir . path , currentVolume . name ) ) )
deleteVolumeData ( currentVolume , parent )
}
2020-07-17 16:35:39 +02:00
}
} else {
2020-11-04 18:42:04 +01:00
if ( currentVolume . hash != null ) {
dialog . setMessage ( R . string . delete _hash _or _all )
dialog . setNegativeButton ( R . string . password _hash _and _path ) { _ , _ ->
deleteVolumeData ( currentVolume , parent )
}
dialog . setPositiveButton ( R . string . password _hash ) { _ , _ ->
deletePasswordHash ( currentVolume )
}
} else {
dialog . setMessage ( R . string . ask _delete _volume _path )
dialog . setPositiveButton ( R . string . ok ) { _ , _ ->
deleteVolumeData ( currentVolume , parent )
}
dialog . setNegativeButton ( R . string . cancel , null )
2020-07-17 16:35:39 +02:00
}
}
dialog . show ( )
}
return view
}
private fun refresh ( parent : ViewGroup ) {
notifyDataSetChanged ( )
if ( count == 0 ) {
2020-11-04 18:42:04 +01:00
WidgetUtil . hideWithPadding ( parent )
2020-08-05 14:06:54 +02:00
} else {
nonScrollableColoredBorderListView . layoutParams . height = nonScrollableColoredBorderListView . computeHeight ( )
2020-07-17 16:35:39 +02:00
}
}
}