@ -3,27 +3,33 @@ package com.deniscerri.ytdlnis.ui
import android.app.Activity
import android.content.*
import android.net.Uri
import android.os.Binder
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.text.InputType
import android.util.Log
import android.view.*
import android.view.View. OnLongClickListener
import android.view.View. *
import android.widget.*
import androidx.appcompat.widget.SearchView
import androidx.core.content.FileProvider
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.deniscerri.ytdlnis.MainActivity
import com.deniscerri.ytdlnis.R
import com.deniscerri.ytdlnis.adapter.DownloadsRecyclerViewAdapter
import com.deniscerri.ytdlnis.database.DatabaseManager
import com.deniscerri.ytdlnis.database.Video
import com.deniscerri.ytdlnis.database.models.HistoryItem
import com.deniscerri.ytdlnis.database.viewmodel.DownloadViewModel
import com.deniscerri.ytdlnis.database.viewmodel.HistoryViewModel
import com.deniscerri.ytdlnis.databinding.FragmentDownloadsBinding
import com.deniscerri.ytdlnis.databinding.FragmentHomeBinding
import com.deniscerri.ytdlnis.service.DownloadInfo
import com.deniscerri.ytdlnis.util.FileUtil
import com.facebook.shimmer.ShimmerFrameLayout
@ -61,7 +67,7 @@ class DownloadsFragment : Fragment(), DownloadsRecyclerViewAdapter.OnItemClickLi
private var no _results : RelativeLayout ? = null
private var selectionChips : LinearLayout ? = null
private var websiteGroup : ChipGroup ? = null
private var downloadsObjects : ArrayList< Video ? > ? = null
private var downloadsObjects : List< HistoryItem ? > ? = null
var selectedObjects : ArrayList < Video ? > ? = null
private var progressBar : LinearProgressIndicator ? = null
private var deleteFab : ExtendedFloatingActionButton ? = null
@ -71,6 +77,8 @@ class DownloadsFragment : Fragment(), DownloadsRecyclerViewAdapter.OnItemClickLi
private var sort = " DESC "
private var searchQuery = " "
private var _binding : FragmentDownloadsBinding ? = null
override fun onCreate ( savedInstanceState : Bundle ? ) {
super . onCreate ( savedInstanceState )
setHasOptionsMenu ( true )
@ -80,21 +88,24 @@ class DownloadsFragment : Fragment(), DownloadsRecyclerViewAdapter.OnItemClickLi
inflater : LayoutInflater , container : ViewGroup ? ,
savedInstanceState : Bundle ?
) : View ? {
_binding = FragmentDownloadsBinding . inflate ( inflater , container , false )
fragmentView = inflater . inflate ( R . layout . fragment _downloads , container , false )
activity = getActivity ( )
mainActivity = activity as MainActivity ?
fragmentContext = context ?. applicationContext
layoutinflater = LayoutInflater . from ( context )
historyViewModel = ViewModelProvider ( this ) [ HistoryViewModel :: class . java ]
return fragmentView
}
override fun onViewCreated ( view : View , savedInstanceState : Bundle ? ) {
super . onViewCreated ( view , savedInstanceState )
shimmerCards = this . view ?. findViewById ( R . id . shimmer _downloads _framelayout )
topAppBar = this . view ?. findViewById ( R . id . downloads _toolbar )
no _results = this . view ?. findViewById ( R . id . downloads _no _results )
selectionChips = this . view ?. findViewById ( R . id . downloads _selection _chips )
websiteGroup = this . view ?. findViewById ( R . id . website _chip _group )
deleteFab = this . view ?. findViewById ( R . id . delete _selected _fab )
fragmentContext = context
shimmerCards = view . findViewById ( R . id . shimmer _downloads _framelayout )
topAppBar = view . findViewById ( R . id . downloads _toolbar )
no _results = view . findViewById ( R . id . downloads _no _results )
selectionChips = view . findViewById ( R . id . downloads _selection _chips )
websiteGroup = view . findViewById ( R . id . website _chip _group )
deleteFab = view . findViewById ( R . id . delete _selected _fab )
fileUtil = FileUtil ( )
deleteFab ?. tag = " deleteSelected "
deleteFab ?. setOnClickListener ( this )
@ -102,57 +113,74 @@ class DownloadsFragment : Fragment(), DownloadsRecyclerViewAdapter.OnItemClickLi
downloadsObjects = ArrayList ( )
selectedObjects = ArrayList ( )
downloading = mainActivity !! . isDownloadServiceRunning ( )
recyclerView = this . view ?. findViewById ( R . id . recycler _view _downloads )
downloadsObjects = mutableListOf ( )
downloadsRecyclerViewAdapter =
DownloadsRecyclerViewAdapter ( downloadsObjects , this , activity )
DownloadsRecyclerViewAdapter ( this , activity )
recyclerView = view . findViewById ( R . id . recyclerviewdownloadss )
recyclerView ?. layoutManager = LinearLayoutManager ( context )
recyclerView ?. adapter = downloadsRecyclerViewAdapter
recyclerView ?. isNestedScrollingEnabled = false
initMenu ( )
initChips ( )
initCards ( )
return fragmentView
}
fun initCards ( ) {
shimmerCards !! . startShimmer ( )
shimmerCards !! . visibility = View . VISIBLE
downloadsRecyclerViewAdapter !! . clear ( )
no _results !! . visibility = View . GONE
selectionChips !! . visibility = View . VISIBLE
databaseManager = DatabaseManager ( context )
try {
val thread = Thread {
if ( ! downloading ) databaseManager !! . clearDownloadingHistory ( )
downloadsObjects = databaseManager !! . getHistory ( " " , format , website , sort )
uiHandler !! . post {
downloadsRecyclerViewAdapter !! . add ( downloadsObjects )
shimmerCards !! . stopShimmer ( )
shimmerCards !! . visibility = View . GONE
updateWebsiteChips ( )
}
if ( ( downloadsObjects as ArrayList < Video > ? ) ?. size == 0 ) {
uiHandler !! . post {
no _results !! . visibility = View . VISIBLE
selectionChips !! . visibility = View . GONE
websiteGroup !! . removeAllViews ( )
}
}
databaseManager !! . close ( )
}
thread . start ( )
} catch ( e : Exception ) {
Log . e ( TAG , e . toString ( ) )
no _results ?. visibility = GONE
selectionChips ?. visibility = VISIBLE
shimmerCards ?. visibility = GONE
historyViewModel = ViewModelProvider ( this ) [ HistoryViewModel :: class . java ]
historyViewModel . allHistory . observe ( viewLifecycleOwner ) {
downloadsRecyclerViewAdapter !! . setVideoList ( it ) ;
Toast . makeText ( context , " CHANGED " , Toast . LENGTH _SHORT ) . show ( )
}
initMenu ( )
//initChips()
// initCards()
}
// fun initCards() {
// shimmerCards!!.startShimmer()
// shimmerCards!!.visibility = View.VISIBLE
// downloadsRecyclerViewAdapter!!.clear()
// no_results!!.visibility = View.GONE
// selectionChips!!.visibility = View.VISIBLE
// databaseManager = DatabaseManager(context)
// try {
// val thread = Thread {
// if (!downloading) databaseManager!!.clearDownloadingHistory()
// downloadsObjects = databaseManager!!.getHistory("", format, website, sort)
// uiHandler!!.post {
// downloadsRecyclerViewAdapter!!.add(downloadsObjects)
// shimmerCards!!.stopShimmer()
// shimmerCards!!.visibility = View.GONE
// updateWebsiteChips()
// }
// if ((downloadsObjects as ArrayList<Video>?)?.size == 0) {
// uiHandler!!.post {
// no_results!!.visibility = View.VISIBLE
// selectionChips!!.visibility = View.GONE
// websiteGroup!!.removeAllViews()
// }
// }
// databaseManager!!.close()
// }
// thread.start()
// } catch (e: Exception) {
// Log.e(TAG, e.toString())
// }
// }
//
fun scrollToTop ( ) {
recyclerView !! . scrollToPosition ( 0 )
Handler ( Looper . getMainLooper ( ) ) . post {
( topAppBar !! . parent as AppBarLayout ) . setExpanded (
true ,
true
)
}
// recyclerView!!.scrollToPosition(0)
// Handler(Looper.getMainLooper()).post {
// (topAppBar!!.parent as AppBarLayout).setExpanded(
// true,
// true
// )
// }
}
private fun initMenu ( ) {
@ -175,31 +203,31 @@ class DownloadsFragment : Fragment(), DownloadsRecyclerViewAdapter.OnItemClickLi
databaseManager = DatabaseManager ( context )
searchView . setOnQueryTextListener ( object : SearchView . OnQueryTextListener {
override fun onQueryTextSubmit ( query : String ) : Boolean {
searchQuery = query
topAppBar !! . menu . findItem ( R . id . search _downloads ) . collapseActionView ( )
downloadsObjects = databaseManager !! . getHistory ( query , format , website , sort )
downloadsRecyclerViewAdapter !! . clear ( )
downloadsRecyclerViewAdapter !! . add ( downloadsObjects )
if ( ( downloadsObjects as ArrayList < Video > ? ) ?. size == 0 ) {
no _results !! . visibility = View . VISIBLE
selectionChips !! . visibility = View . GONE
websiteGroup !! . removeAllViews ( )
}
// searchQuery = query
// topAppBar!!.menu.findItem(R.id.search_downloads).collapseActionView()
// downloadsObjects = databaseManager!!.getHistory(query, format, website, sort)
// downloadsRecyclerViewAdapter!!.clear()
// downloadsRecyclerViewAdapter!!.add(downloadsObjects)
// if ((downloadsObjects as ArrayList<Video>?)?.size == 0) {
// no_results!!.visibility = View.VISIBLE
// selectionChips!!.visibility = View.GONE
// websiteGroup!!.removeAllViews()
// }
return true
}
override fun onQueryTextChange ( newText : String ) : Boolean {
searchQuery = newText
downloadsObjects = databaseManager !! . getHistory ( newText , format , website , sort )
downloadsRecyclerViewAdapter !! . clear ( )
downloadsRecyclerViewAdapter !! . add ( downloadsObjects )
if ( ( downloadsObjects as ArrayList < Video > ? ) ?. size == 0 ) {
no _results !! . visibility = View . VISIBLE
selectionChips !! . visibility = View . GONE
} else {
no _results !! . visibility = View . GONE
selectionChips !! . visibility = View . VISIBLE
}
// searchQuery = newText
// downloadsObjects = databaseManager!!.getHistory(newText, format, website, sort)
// downloadsRecyclerViewAdapter!!.clear()
// downloadsRecyclerViewAdapter!!.add(downloadsObjects)
// if ((downloadsObjects as ArrayList<Video>?)?.size == 0) {
// no_results!!.visibility = View.VISIBLE
// selectionChips!!.visibility = View.GONE
// } else {
// no_results!!.visibility = View.GONE
// selectionChips!!.visibility = View.VISIBLE
// }
return true
}
} )
@ -207,266 +235,266 @@ class DownloadsFragment : Fragment(), DownloadsRecyclerViewAdapter.OnItemClickLi
topAppBar !! . setOnMenuItemClickListener { m : MenuItem ->
val itemID = m . itemId
if ( itemID == R . id . remove _downloads ) {
if ( downloadsObjects !! . size == 0 ) {
Toast . makeText ( context , R . string . history _is _empty , Toast . LENGTH _SHORT ) . show ( )
return @setOnMenuItemClickListener true
}
val delete _dialog = MaterialAlertDialogBuilder ( fragmentContext !! )
delete _dialog . setTitle ( getString ( R . string . confirm _delete _history ) )
delete _dialog . setMessage ( getString ( R . string . confirm _delete _history _desc ) )
delete _dialog . setNegativeButton ( getString ( R . string . cancel ) ) { dialogInterface : DialogInterface , i : Int -> dialogInterface . cancel ( ) }
delete _dialog . setPositiveButton ( getString ( R . string . ok ) ) { dialogInterface : DialogInterface ? , i : Int ->
databaseManager !! . clearHistory ( )
downloadsRecyclerViewAdapter !! . clear ( )
downloadsObjects !! . clear ( )
no _results !! . visibility = View . VISIBLE
selectionChips !! . visibility = View . GONE
websiteGroup !! . removeAllViews ( )
}
delete _dialog . show ( )
// if (downloadsObjects!!.size == 0) {
// Toast.makeText(context, R.string.history_is_empty, Toast.LENGTH_SHORT).show()
// return@setOnMenuItemClickListener true
// }
// val delete_dialog = MaterialAlertDialogBuilder(fragmentContext!!)
// delete_dialog.setTitle(getString(R.string.confirm_delete_history))
// delete_dialog.setMessage(getString(R.string.confirm_delete_history_desc))
// delete_dialog.setNegativeButton(getString(R.string.cancel)) { dialogInterface: DialogInterface, i: Int -> dialogInterface.cancel() }
// delete_dialog.setPositiveButton(getString(R.string.ok)) { dialogInterface: DialogInterface?, i: Int ->
// databaseManager!!.clearHistory()
// downloadsRecyclerViewAdapter!!.clear()
// downloadsObjects!!.clear()
// no_results!!.visibility = View.VISIBLE
// selectionChips!!.visibility = View.GONE
// websiteGroup!!.removeAllViews()
// }
// delete_dialog.show()
} else if ( itemID == R . id . remove _deleted _downloads ) {
if ( downloadsObjects !! . size == 0 ) {
Toast . makeText ( context , R . string . history _is _empty , Toast . LENGTH _SHORT ) . show ( )
return @setOnMenuItemClickListener true
}
val delete _dialog = MaterialAlertDialogBuilder ( fragmentContext !! )
delete _dialog . setTitle ( getString ( R . string . confirm _delete _history ) )
delete _dialog . setMessage ( getString ( R . string . confirm _delete _history _deleted _desc ) )
delete _dialog . setNegativeButton ( getString ( R . string . cancel ) ) { dialogInterface : DialogInterface , i : Int -> dialogInterface . cancel ( ) }
delete _dialog . setPositiveButton ( getString ( R . string . ok ) ) { dialogInterface : DialogInterface ? , i : Int ->
databaseManager !! . clearDeletedHistory ( )
initCards ( )
}
delete _dialog . show ( )
// if (downloadsObjects!!.size == 0) {
// Toast.makeText(context, R.string.history_is_empty, Toast.LENGTH_SHORT).show()
// return@setOnMenuItemClickListener true
// }
// val delete_dialog = MaterialAlertDialogBuilder(fragmentContext!!)
// delete_dialog.setTitle(getString(R.string.confirm_delete_history))
// delete_dialog.setMessage(getString(R.string.confirm_delete_history_deleted_desc))
// delete_dialog.setNegativeButton(getString(R.string.cancel)) { dialogInterface: DialogInterface, i: Int -> dialogInterface.cancel() }
// delete_dialog.setPositiveButton(getString(R.string.ok)) { dialogInterface: DialogInterface?, i: Int ->
// databaseManager!!.clearDeletedHistory()
// initCards()
// }
// delete_dialog.show()
} else if ( itemID == R . id . remove _duplicates ) {
if ( downloadsObjects !! . size == 0 ) {
Toast . makeText ( context , R . string . history _is _empty , Toast . LENGTH _SHORT ) . show ( )
return @setOnMenuItemClickListener true
}
val delete _dialog = MaterialAlertDialogBuilder ( fragmentContext !! )
delete _dialog . setTitle ( getString ( R . string . confirm _delete _history ) )
delete _dialog . setMessage ( getString ( R . string . confirm _delete _history _duplicates _desc ) )
delete _dialog . setNegativeButton ( getString ( R . string . cancel ) ) { dialogInterface : DialogInterface , i : Int -> dialogInterface . cancel ( ) }
delete _dialog . setPositiveButton ( getString ( R . string . ok ) ) { dialogInterface : DialogInterface ? , i : Int ->
databaseManager !! . clearDuplicateHistory ( )
initCards ( )
}
delete _dialog . show ( )
// if (downloadsObjects!!.size == 0) {
// Toast.makeText(context, R.string.history_is_empty, Toast.LENGTH_SHORT).show()
// return@setOnMenuItemClickListener true
// }
// val delete_dialog = MaterialAlertDialogBuilder(fragmentContext!!)
// delete_dialog.setTitle(getString(R.string.confirm_delete_history))
// delete_dialog.setMessage(getString(R.string.confirm_delete_history_duplicates_desc))
// delete_dialog.setNegativeButton(getString(R.string.cancel)) { dialogInterface: DialogInterface, i: Int -> dialogInterface.cancel() }
// delete_dialog.setPositiveButton(getString(R.string.ok)) { dialogInterface: DialogInterface?, i: Int ->
// databaseManager!!.clearDuplicateHistory()
// initCards()
// }
// delete_dialog.show()
} else if ( itemID == R . id . remove _downloading ) {
if ( downloadsObjects !! . size == 0 ) {
Toast . makeText ( context , R . string . history _is _empty , Toast . LENGTH _SHORT ) . show ( )
return @setOnMenuItemClickListener true
}
val delete _dialog = MaterialAlertDialogBuilder ( fragmentContext !! )
delete _dialog . setTitle ( getString ( R . string . confirm _delete _history ) )
delete _dialog . setMessage ( getString ( R . string . confirm _delete _downloading _desc ) )
delete _dialog . setNegativeButton ( getString ( R . string . cancel ) ) { dialogInterface : DialogInterface , i : Int -> dialogInterface . cancel ( ) }
delete _dialog . setPositiveButton ( getString ( R . string . ok ) ) { dialogInterface : DialogInterface ? , i : Int ->
databaseManager !! . clearDownloadingHistory ( )
mainActivity !! . cancelDownloadService ( )
initCards ( )
}
delete _dialog . show ( )
// if (downloadsObjects!!.size == 0) {
// Toast.makeText(context, R.string.history_is_empty, Toast.LENGTH_SHORT).show()
// return@setOnMenuItemClickListener true
// }
// val delete_dialog = MaterialAlertDialogBuilder(fragmentContext!!)
// delete_dialog.setTitle(getString(R.string.confirm_delete_history))
// delete_dialog.setMessage(getString(R.string.confirm_delete_downloading_desc))
// delete_dialog.setNegativeButton(getString(R.string.cancel)) { dialogInterface: DialogInterface, i: Int -> dialogInterface.cancel() }
// delete_dialog.setPositiveButton(getString(R.string.ok)) { dialogInterface: DialogInterface?, i: Int ->
// databaseManager!!.clearDownloadingHistory()
// mainActivity!!.cancelDownloadService()
// initCards()
// }
// delete_dialog.show()
}
true
}
}
private fun initChips ( ) {
//sort and history/downloading switch
val sortChip = fragmentView !! . findViewById < Chip > ( R . id . sort _chip )
sortChip . setOnClickListener { view : View ? ->
sortSheet = BottomSheetDialog ( fragmentContext !! )
sortSheet !! . requestWindowFeature ( Window . FEATURE _NO _TITLE )
sortSheet !! . setContentView ( R . layout . downloads _sort _sheet )
val newest = sortSheet !! . findViewById < TextView > ( R . id . newest )
val oldest = sortSheet !! . findViewById < TextView > ( R . id . oldest )
newest !! . setOnClickListener { view1 : View ? ->
sort = " DESC "
downloadsObjects = databaseManager !! . getHistory ( searchQuery , format , website , sort )
downloadsRecyclerViewAdapter !! . clear ( )
downloadsRecyclerViewAdapter !! . add ( downloadsObjects )
sortSheet !! . cancel ( )
}
oldest !! . setOnClickListener { view1 : View ? ->
sort = " ASC "
downloadsObjects = databaseManager !! . getHistory ( searchQuery , format , website , sort )
downloadsRecyclerViewAdapter !! . clear ( )
downloadsRecyclerViewAdapter !! . add ( downloadsObjects )
sortSheet !! . cancel ( )
}
val cancel = sortSheet !! . findViewById < TextView > ( R . id . cancel )
cancel !! . setOnClickListener { view1 : View ? -> sortSheet !! . cancel ( ) }
sortSheet !! . show ( )
sortSheet !! . window !! . setLayout (
ViewGroup . LayoutParams . MATCH _PARENT ,
ViewGroup . LayoutParams . MATCH _PARENT
)
}
//format
val audio = fragmentView !! . findViewById < Chip > ( R . id . audio _chip )
audio . setOnClickListener { view : View ? ->
if ( audio . isChecked ) {
format = " audio "
if ( recyclerView !! . visibility == View . GONE ) {
}
downloadsObjects = databaseManager !! . getHistory ( searchQuery , format , website , sort )
audio . isChecked = true
} else {
format = " "
downloadsObjects = databaseManager !! . getHistory ( searchQuery , format , website , sort )
audio . isChecked = false
}
downloadsRecyclerViewAdapter !! . clear ( )
downloadsRecyclerViewAdapter !! . add ( downloadsObjects )
}
val video = fragmentView !! . findViewById < Chip > ( R . id . video _chip )
video . setOnClickListener { view : View ? ->
if ( video . isChecked ) {
format = " video "
downloadsObjects = databaseManager !! . getHistory ( searchQuery , format , website , sort )
video . isChecked = true
} else {
format = " "
downloadsObjects = databaseManager !! . getHistory ( searchQuery , format , website , sort )
video . isChecked = false
}
downloadsRecyclerViewAdapter !! . clear ( )
downloadsRecyclerViewAdapter !! . add ( downloadsObjects )
}
}
private fun updateWebsiteChips ( ) {
websiteGroup !! . removeAllViews ( )
val websites = downloadsRecyclerViewAdapter !! . websites
for ( i in websites . indices ) {
val w = websites [ i ]
val tmp = layoutinflater !! . inflate ( R . layout . filter _chip , websiteGroup , false ) as Chip
tmp . text = w
tmp . id = i
tmp . setOnClickListener { view : View ->
if ( tmp . isChecked ) {
website = tmp . text as String
downloadsObjects =
databaseManager !! . getHistory ( searchQuery , format , website , sort )
websiteGroup !! . check ( view . id )
} else {
website = " "
downloadsObjects =
databaseManager !! . getHistory ( searchQuery , format , website , sort )
websiteGroup !! . clearCheck ( )
}
downloadsRecyclerViewAdapter !! . clear ( )
downloadsRecyclerViewAdapter !! . add ( downloadsObjects )
}
websiteGroup !! . addView ( tmp )
}
}
// private fun initChips() {
// //sort and history/downloading switch
// val sortChip = fragmentView!!.findViewById<Chip>(R.id.sort_chip)
// sortChip.setOnClickListener { view: View? ->
// sortSheet = BottomSheetDialog(fragmentContext!!)
// sortSheet!!.requestWindowFeature(Window.FEATURE_NO_TITLE)
// sortSheet!!.setContentView(R.layout.downloads_sort_sheet)
// val newest = sortSheet!!.findViewById<TextView>(R.id.newest)
// val oldest = sortSheet!!.findViewById<TextView>(R.id.oldest)
// newest!!.setOnClickListener { view1: View? ->
// sort = "DESC"
// downloadsObjects = databaseManager!!.getHistory(searchQuery, format, website, sort)
// downloadsRecyclerViewAdapter!!.clear()
// downloadsRecyclerViewAdapter!!.add(downloadsObjects)
// sortSheet!!.cancel()
// }
// oldest!!.setOnClickListener { view1: View? ->
// sort = "ASC"
// downloadsObjects = databaseManager!!.getHistory(searchQuery, format, website, sort)
// downloadsRecyclerViewAdapter!!.clear()
// downloadsRecyclerViewAdapter!!.add(downloadsObjects)
// sortSheet!!.cancel()
// }
// val cancel = sortSheet!!.findViewById<TextView>(R.id.cancel)
// cancel!!.setOnClickListener { view1: View? -> sortSheet!!.cancel() }
// sortSheet!!.show()
// sortSheet!!.window!!.setLayout(
// ViewGroup.LayoutParams.MATCH_PARENT,
// ViewGroup.LayoutParams.MATCH_PARENT
// )
// }
//
// //format
// val audio = fragmentView!!.findViewById<Chip>(R.id.audio_chip)
// audio.setOnClickListener { view: View? ->
// if (audio.isChecked) {
// format = "audio"
// if (recyclerView!!.visibility == View.GONE) {
// }
// downloadsObjects = databaseManager!!.getHistory(searchQuery, format, website, sort)
// audio.isChecked = true
// } else {
// format = ""
// downloadsObjects = databaseManager!!.getHistory(searchQuery, format, website, sort)
// audio.isChecked = false
// }
// downloadsRecyclerViewAdapter!!.clear()
// downloadsRecyclerViewAdapter!!.add(downloadsObjects)
// }
// val video = fragmentView!!.findViewById<Chip>(R.id.video_chip)
// video.setOnClickListener { view: View? ->
// if (video.isChecked) {
// format = "video"
// downloadsObjects = databaseManager!!.getHistory(searchQuery, format, website, sort)
// video.isChecked = true
// } else {
// format = ""
// downloadsObjects = databaseManager!!.getHistory(searchQuery, format, website, sort)
// video.isChecked = false
// }
// downloadsRecyclerViewAdapter!!.clear()
// downloadsRecyclerViewAdapter!!.add(downloadsObjects)
// }
// }
//
// private fun updateWebsiteChips() {
// websiteGroup!!.removeAllViews()
// val websites = downloadsRecyclerViewAdapter!!.websites
// for (i in websites.indices) {
// val w = websites[i]
// val tmp = layoutinflater!!.inflate(R.layout.filter_chip, websiteGroup, false) as Chip
// tmp.text = w
// tmp.id = i
// tmp.setOnClickListener { view: View ->
// if (tmp.isChecked) {
// website = tmp.text as String
// downloadsObjects =
// databaseManager!!.getHistory(searchQuery, format, website, sort)
// websiteGroup!!.check(view.id)
// } else {
// website = ""
// downloadsObjects =
// databaseManager!!.getHistory(searchQuery, format, website, sort)
// websiteGroup!!.clearCheck()
// }
// downloadsRecyclerViewAdapter!!.clear()
// downloadsRecyclerViewAdapter!!.add(downloadsObjects)
// }
// websiteGroup!!.addView(tmp)
// }
// }
override fun onClick ( v : View ) {
val id = v . id
if ( id == R . id . bottomsheet _remove _button ) {
removedownloadsItem ( v . tag as Int )
} else if ( id == R . id . bottom _sheet _link ) {
openLinkIntent ( v . tag as Int )
} else if ( id == R . id . bottomsheet _open _file _button ) {
openFileIntent ( v . tag as Int )
} else if ( id == R . id . delete _selected _fab ) {
removeSelectedItems ( )
}
// val id = v.id
// if (id == R.id.bottomsheet_remove_button) {
// removedownloadsItem(v.tag as Int)
// } else if (id == R.id.bottom_sheet_link) {
// openLinkIntent(v.tag as Int)
// } else if (id == R.id.bottomsheet_open_file_button) {
// openFileIntent(v.tag as Int)
// } else if (id == R.id.delete_selected_fab) {
// removeSelectedItems()
// }
}
override fun onLongClick ( v : View ) : Boolean {
val id = v . id
if ( id == R . id . bottom _sheet _link ) {
copyLinkToClipBoard ( v . tag as Int )
return true
}
// val id = v.id
// if (id == R.id.bottom_sheet_link) {
// copyLinkToClipBoard(v.tag as Int)
// return true
// }
return false
}
private fun removeSelectedItems ( ) {
if ( bottomSheet != null ) bottomSheet !! . hide ( )
val delete _file = booleanArrayOf ( false )
databaseManager = DatabaseManager ( context )
val delete _dialog = MaterialAlertDialogBuilder ( fragmentContext !! )
delete _dialog . setTitle ( getString ( R . string . you _are _going _to _delete _multiple _items ) )
delete _dialog . setMultiChoiceItems (
arrayOf ( getString ( R . string . delete _files _too ) ) ,
booleanArrayOf ( false )
) { dialogInterface : DialogInterface ? , i : Int , b : Boolean -> delete _file [ 0 ] = b }
delete _dialog . setNegativeButton ( getString ( R . string . cancel ) ) { dialogInterface : DialogInterface , i : Int -> dialogInterface . cancel ( ) }
delete _dialog . setPositiveButton ( getString ( R . string . ok ) ) { dialogInterface : DialogInterface ? , i : Int ->
for ( j in selectedObjects !! . indices ) {
val v = selectedObjects !! [ j ]
val position = downloadsObjects !! . indexOf ( v )
downloadsObjects !! . remove ( v )
downloadsRecyclerViewAdapter !! . remove ( position )
databaseManager !! . clearHistoryItem ( v , delete _file [ 0 ] )
}
updateWebsiteChips ( )
databaseManager !! . close ( )
selectedObjects = ArrayList ( )
downloadsRecyclerViewAdapter !! . clearCheckedVideos ( )
deleteFab !! . visibility = View . GONE
if ( downloadsObjects !! . size == 0 ) {
uiHandler !! . post {
no _results !! . visibility = View . VISIBLE
selectionChips !! . visibility = View . GONE
websiteGroup !! . removeAllViews ( )
}
}
}
delete _dialog . show ( )
}
private fun removedownloadsItem ( position : Int ) {
if ( bottomSheet != null ) bottomSheet !! . hide ( )
val delete _file = booleanArrayOf ( false )
databaseManager = DatabaseManager ( context )
val v = downloadsObjects !! [ position ]
val delete _dialog = MaterialAlertDialogBuilder ( fragmentContext !! )
delete _dialog . setTitle ( getString ( R . string . you _are _going _to _delete ) + " \" " + v !! . title + " \" ! " )
delete _dialog . setMultiChoiceItems (
arrayOf ( getString ( R . string . delete _file _too ) ) ,
booleanArrayOf ( false )
) { dialogInterface : DialogInterface ? , i : Int , b : Boolean -> delete _file [ 0 ] = b }
delete _dialog . setNegativeButton ( getString ( R . string . cancel ) ) { dialogInterface : DialogInterface , i : Int -> dialogInterface . cancel ( ) }
delete _dialog . setPositiveButton ( getString ( R . string . ok ) ) { dialogInterface : DialogInterface ? , i : Int ->
downloadsObjects !! . removeAt ( position )
downloadsRecyclerViewAdapter !! . remove ( position )
updateWebsiteChips ( )
databaseManager !! . clearHistoryItem ( v , delete _file [ 0 ] )
databaseManager !! . close ( )
if ( downloadsObjects !! . size == 0 ) {
uiHandler !! . post {
no _results !! . visibility = View . VISIBLE
selectionChips !! . visibility = View . GONE
websiteGroup !! . removeAllViews ( )
}
}
}
delete _dialog . show ( )
}
private fun copyLinkToClipBoard ( position : Int ) {
val url = downloadsObjects !! [ position ] !! . getURL ( )
val clipboard = context ?. getSystemService ( Context . CLIPBOARD _SERVICE ) as ClipboardManager
val clip = ClipData . newPlainText ( getString ( R . string . url ) , url )
clipboard . setPrimaryClip ( clip )
if ( bottomSheet != null ) bottomSheet !! . hide ( )
Toast . makeText ( context , getString ( R . string . link _copied _to _clipboard ) , Toast . LENGTH _SHORT )
. show ( )
}
private fun openLinkIntent ( position : Int ) {
val url = downloadsObjects !! [ position ] !! . getURL ( )
val i = Intent ( Intent . ACTION _VIEW )
i . data = Uri . parse ( url )
if ( bottomSheet != null ) bottomSheet !! . hide ( )
startActivity ( i )
}
// private fun removeSelectedItems() {
// if (bottomSheet != null) bottomSheet!!.hide()
// val delete_file = booleanArrayOf(false)
// databaseManager = DatabaseManager(context)
// val delete_dialog = MaterialAlertDialogBuilder(fragmentContext!!)
// delete_dialog.setTitle(getString(R.string.you_are_going_to_delete_multiple_items))
// delete_dialog.setMultiChoiceItems(
// arrayOf(getString(R.string.delete_files_too)),
// booleanArrayOf(false)
// ) { dialogInterface: DialogInterface?, i: Int, b: Boolean -> delete_file[0] = b }
// delete_dialog.setNegativeButton(getString(R.string.cancel)) { dialogInterface: DialogInterface, i: Int -> dialogInterface.cancel() }
// delete_dialog.setPositiveButton(getString(R.string.ok)) { dialogInterface: DialogInterface?, i: Int ->
// for (j in selectedObjects!!.indices) {
// val v = selectedObjects!![j]
// val position = downloadsObjects!!.indexOf(v)
// downloadsObjects!!.remove(v)
// downloadsRecyclerViewAdapter!!.remove(position)
// databaseManager!!.clearHistoryItem(v, delete_file[0])
// }
// updateWebsiteChips()
// databaseManager!!.close()
// selectedObjects = ArrayList()
// downloadsRecyclerViewAdapter!!.clearCheckedVideos()
// deleteFab!!.visibility = View.GONE
// if (downloadsObjects!!.size == 0) {
// uiHandler!!.post {
// no_results!!.visibility = View.VISIBLE
// selectionChips!!.visibility = View.GONE
// websiteGroup!!.removeAllViews()
// }
// }
// }
// delete_dialog.show()
// }
//
// private fun removedownloadsItem(position: Int) {
// if (bottomSheet != null) bottomSheet!!.hide()
// val delete_file = booleanArrayOf(false)
// databaseManager = DatabaseManager(context)
// val v = downloadsObjects!![position]
// val delete_dialog = MaterialAlertDialogBuilder(fragmentContext!!)
// delete_dialog.setTitle(getString(R.string.you_are_going_to_delete) + " \"" + v!!.title + "\"!")
// delete_dialog.setMultiChoiceItems(
// arrayOf(getString(R.string.delete_file_too)),
// booleanArrayOf(false)
// ) { dialogInterface: DialogInterface?, i: Int, b: Boolean -> delete_file[0] = b }
// delete_dialog.setNegativeButton(getString(R.string.cancel)) { dialogInterface: DialogInterface, i: Int -> dialogInterface.cancel() }
// delete_dialog.setPositiveButton(getString(R.string.ok)) { dialogInterface: DialogInterface?, i: Int ->
// downloadsObjects!!.removeAt(position)
// downloadsRecyclerViewAdapter!!.remove(position)
// updateWebsiteChips()
// databaseManager!!.clearHistoryItem(v, delete_file[0])
// databaseManager!!.close()
// if (downloadsObjects!!.size == 0) {
// uiHandler!!.post {
// no_results!!.visibility = View.VISIBLE
// selectionChips!!.visibility = View.GONE
// websiteGroup!!.removeAllViews()
// }
// }
// }
// delete_dialog.show()
// }
//
// private fun copyLinkToClipBoard(position: Int) {
// val url = downloadsObjects!![position]!!.getURL()
// val clipboard = context?.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
// val clip = ClipData.newPlainText(getString(R.string.url), url)
// clipboard.setPrimaryClip(clip)
// if (bottomSheet != null) bottomSheet!!.hide()
// Toast.makeText(context, getString(R.string.link_copied_to_clipboard), Toast.LENGTH_SHORT)
// .show()
// }
//
// private fun openLinkIntent(position: Int) {
// val url = downloadsObjects!![position]!!.getURL()
// val i = Intent(Intent.ACTION_VIEW)
// i.data = Uri.parse(url)
// if (bottomSheet != null) bottomSheet!!.hide()
// startActivity(i)
// }
private fun openFileIntent ( position : Int ) {
val downloadPath = downloadsObjects !! [ position ] !! . downloadPath
@ -493,7 +521,7 @@ class DownloadsFragment : Fragment(), DownloadsRecyclerViewAdapter.OnItemClickLi
val author = bottomSheet !! . findViewById < TextView > ( R . id . bottom _sheet _author )
author !! . text = video . author
val link = bottomSheet !! . findViewById < Button > ( R . id . bottom _sheet _link )
val url = video . getURL( )
val url = video . url
link !! . text = url
link . tag = position
link . setOnClickListener ( this )
@ -504,7 +532,7 @@ class DownloadsFragment : Fragment(), DownloadsRecyclerViewAdapter.OnItemClickLi
val openFile = bottomSheet !! . findViewById < Button > ( R . id . bottomsheet _open _file _button )
openFile !! . tag = position
openFile . setOnClickListener ( this )
if ( !is FilePresent ) openFile . visibility = View . GONE
if ( !is FilePresent ) openFile . visibility = GONE
bottomSheet !! . show ( )
bottomSheet !! . window !! . setLayout (
ViewGroup . LayoutParams . MATCH _PARENT ,
@ -513,30 +541,30 @@ class DownloadsFragment : Fragment(), DownloadsRecyclerViewAdapter.OnItemClickLi
}
override fun onCardSelect ( position : Int , add : Boolean ) {
val video = downloadsObjects !! [ position ]
if ( add ) selectedObjects !! . add ( video ) else selectedObjects !! . remove ( video )
if ( selectedObjects !! . size > 1 ) {
deleteFab !! . visibility = View . VISIBLE
} else {
deleteFab !! . visibility = View . GONE
}
// val video = downloadsObjects!![position]
// if (add) selectedObjects!!.add(video) else selectedObjects!!.remove(video)
// if (selectedObjects!!.size > 1) {
// deleteFab!!.visibility = View.VISIBLE
// } else {
// deleteFab!!.visibility = View.GONE
// }
}
//
override fun onButtonClick ( position : Int ) {
val vid = downloadsObjects !! [ position ]
try {
mainActivity !! . removeItemFromDownloadQueue ( vid , vid !! . downloadedType )
} catch ( e : Exception ) {
val info = DownloadInfo ( )
info . video = vid
info . downloadType = vid !! . downloadedType
}
// val vid = downloadsObjects!![position]
// try {
// mainActivity!!.removeItemFromDownloadQueue(vid, vid!!.downloadedType)
// } catch (e: Exception) {
// val info = DownloadInfo()
// info.video = vid
// info.downloadType = vid!!.downloadedType
// }
}
fun findVideo ( url : String , type : String ) : Video ? {
//
fun findVideo ( url : String , type : String ) : HistoryItem ? {
for ( i in downloadsObjects !! . indices ) {
val v = downloadsObjects !! [ i ]
if ( v !! . getURL( ) == url && v . downloadedType == type && v . isQueuedDownload ) {
if ( v !! . url == url && v . type == type ) {
return v
}
}