diff --git a/app/src/main/java/com/deniscerri/ytdlnis/database/dao/SearchHistoryDao.kt b/app/src/main/java/com/deniscerri/ytdlnis/database/dao/SearchHistoryDao.kt index f2457c9b..63646d78 100644 --- a/app/src/main/java/com/deniscerri/ytdlnis/database/dao/SearchHistoryDao.kt +++ b/app/src/main/java/com/deniscerri/ytdlnis/database/dao/SearchHistoryDao.kt @@ -18,4 +18,7 @@ interface SearchHistoryDao { @Query("DELETE FROM searchHistory") suspend fun deleteAll() + + @Query("DELETE FROM searchHistory WHERE query=:query") + suspend fun delete(query: String) } \ No newline at end of file diff --git a/app/src/main/java/com/deniscerri/ytdlnis/database/repository/SearchHistoryRepository.kt b/app/src/main/java/com/deniscerri/ytdlnis/database/repository/SearchHistoryRepository.kt index 813e5042..492229f0 100644 --- a/app/src/main/java/com/deniscerri/ytdlnis/database/repository/SearchHistoryRepository.kt +++ b/app/src/main/java/com/deniscerri/ytdlnis/database/repository/SearchHistoryRepository.kt @@ -19,4 +19,8 @@ class SearchHistoryRepository(private val searchHistoryDao: SearchHistoryDao) { suspend fun deleteAll(){ searchHistoryDao.deleteAll() } + + suspend fun delete(query: String){ + searchHistoryDao.delete(query) + } } \ No newline at end of file diff --git a/app/src/main/java/com/deniscerri/ytdlnis/database/viewmodel/ResultViewModel.kt b/app/src/main/java/com/deniscerri/ytdlnis/database/viewmodel/ResultViewModel.kt index 2cb9759a..e73bed38 100644 --- a/app/src/main/java/com/deniscerri/ytdlnis/database/viewmodel/ResultViewModel.kt +++ b/app/src/main/java/com/deniscerri/ytdlnis/database/viewmodel/ResultViewModel.kt @@ -124,6 +124,14 @@ class ResultViewModel(application: Application) : AndroidViewModel(application) } + fun removeSearchQueryFromHistory(query: String) = viewModelScope.launch(Dispatchers.IO) { + val allQueries = searchHistoryRepository.getAll() + if (allQueries.any { it.query == query }){ + searchHistoryRepository.delete(query) + } + + } + fun deleteAllSearchQueryHistory() = viewModelScope.launch(Dispatchers.IO){ searchHistoryRepository.deleteAll() } diff --git a/app/src/main/java/com/deniscerri/ytdlnis/ui/HomeFragment.kt b/app/src/main/java/com/deniscerri/ytdlnis/ui/HomeFragment.kt index b368e75d..9515dec6 100644 --- a/app/src/main/java/com/deniscerri/ytdlnis/ui/HomeFragment.kt +++ b/app/src/main/java/com/deniscerri/ytdlnis/ui/HomeFragment.kt @@ -2,18 +2,17 @@ package com.deniscerri.ytdlnis.ui import android.annotation.SuppressLint import android.app.Activity -import android.content.Context -import android.content.Intent -import android.content.SharedPreferences +import android.content.* +import android.content.Context.CLIPBOARD_SERVICE import android.content.res.Configuration import android.os.Bundle import android.os.Handler import android.os.Looper import android.util.Log import android.view.* -import android.view.View.GONE -import android.view.View.VISIBLE +import android.view.View.* import android.widget.* +import androidx.constraintlayout.widget.ConstraintLayout import androidx.coordinatorlayout.widget.CoordinatorLayout import androidx.core.widget.addTextChangedListener import androidx.fragment.app.Fragment @@ -37,10 +36,11 @@ import com.deniscerri.ytdlnis.util.InfoUtil import com.facebook.shimmer.ShimmerFrameLayout import com.google.android.material.appbar.AppBarLayout import com.google.android.material.button.MaterialButton +import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton import com.google.android.material.search.SearchBar +import com.google.android.material.search.SearchView import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import java.util.* @@ -51,8 +51,12 @@ class HomeFragment : Fragment(), HomeAdapter.OnItemClickListener, View.OnClickLi private var inputQueries: LinkedList? = null private var inputQueriesLength = 0 private var homeAdapter: HomeAdapter? = null + private var searchSuggestions: ScrollView? = null private var searchSuggestionsLinearLayout: LinearLayout? = null + private var searchHistory: ScrollView? = null + private var searchHistoryLinearLayout: LinearLayout? = null + private var downloadFabs: CoordinatorLayout? = null private var downloadAllFabCoordinator: CoordinatorLayout? = null private var homeFabs: CoordinatorLayout? = null @@ -115,6 +119,8 @@ class HomeFragment : Fragment(), HomeAdapter.OnItemClickListener, View.OnClickLi searchBar = view.findViewById(R.id.search_bar) searchSuggestions = view.findViewById(R.id.search_suggestions_scroll_view) searchSuggestionsLinearLayout = view.findViewById(R.id.search_suggestions_linear_layout) + searchHistory = view.findViewById(R.id.search_history_scroll_view) + searchHistoryLinearLayout = view.findViewById(R.id.search_history_linear_layout) homeAdapter = HomeAdapter( @@ -201,29 +207,45 @@ class HomeFragment : Fragment(), HomeAdapter.OnItemClickListener, View.OnClickLi } else { resultViewModel.checkTrending() } - -// fragmentView!!.post{ -// if (shimmerCards!!.visibility == VISIBLE) return@post -// val clipboard = requireContext().getSystemService(CLIPBOARD_SERVICE) as ClipboardManager -// val regex = "(https?:\\/\\/(?:www\\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\\.[^\\s]{2,}|www\\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\\.[^\\s]{2,}|https?:\\/\\/(?:www\\.|(?!www))[a-zA-Z0-9]+\\.[^\\s]{2,}|www\\.[a-zA-Z0-9]+\\.[^\\s]{2,})".toRegex() -// val clip = clipboard.primaryClip!!.getItemAt(0).text -// if (regex.containsMatchIn(clip.toString())){ -// val snackbar = Snackbar.make(fragmentView!!, clip, Snackbar.LENGTH_INDEFINITE) -// snackbar.setAction(getString(R.string.download)+"?") { -// resultViewModel.deleteAll() -// resultViewModel.parseQuery(clip.toString(), true) -// } -// snackbar.show() -// } -// } } private fun initMenu() { - val searchView = requireView().findViewById(R.id.search_view) + val searchView = requireView().findViewById(R.id.search_view) infoUtil = InfoUtil(requireContext()) + val linkYouCopied = searchView.findViewById(R.id.link_you_copied) + searchView.addTransitionListener { view, previousState, newState -> + if (newState == SearchView.TransitionState.SHOWN) { + val clipboard = + requireContext().getSystemService(CLIPBOARD_SERVICE) as ClipboardManager + val regex = + "(https?://(?:www\\.|(?!www))[a-zA-Z\\d][a-zA-Z\\d-]+[a-zA-Z\\d]\\.\\S{2,}|www\\.[a-zA-Z\\d][a-zA-Z\\d-]+[a-zA-Z\\d]\\.\\S{2,}|https?://(?:www\\.|(?!www))[a-zA-Z\\d]+\\.\\S{2,}|www\\.[a-zA-Z\\d]+\\.\\S{2,})".toRegex() + val clip = clipboard.primaryClip!!.getItemAt(0).text + if (regex.containsMatchIn(clip.toString())) { + linkYouCopied.visibility = VISIBLE + val textView = linkYouCopied.findViewById(R.id.suggestion_text) + textView.text = getString(R.string.link_you_copied) + textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_language, 0, 0, 0) + val mb = linkYouCopied.findViewById(R.id.set_search_query_button) + mb.visibility = INVISIBLE + + textView.setOnClickListener { + searchView.setText(clip.toString()) + initSearch(searchView) + } + }else{ + linkYouCopied.visibility = GONE + } + } + } searchView.editText.addTextChangedListener { searchSuggestionsLinearLayout!!.removeAllViews() + searchHistoryLinearLayout!!.removeAllViews() + + searchSuggestionsLinearLayout!!.visibility = GONE + searchHistoryLinearLayout!!.visibility = GONE + linkYouCopied!!.visibility = GONE + lifecycleScope.launch { val suggestions = withContext(Dispatchers.IO){ if (it!!.isEmpty()) { @@ -233,25 +255,65 @@ class HomeFragment : Fragment(), HomeAdapter.OnItemClickListener, View.OnClickLi } } - for (i in suggestions.indices) { - val v = LayoutInflater.from(fragmentContext) - .inflate(R.layout.search_suggestion_item, null) - val textView = v.findViewById(R.id.suggestion_text) - textView.text = suggestions[i] - Handler(Looper.getMainLooper()).post { - searchSuggestionsLinearLayout!!.addView( - v - ) + if (it!!.isEmpty()){ + for (i in suggestions.indices) { + val v = LayoutInflater.from(fragmentContext) + .inflate(R.layout.search_suggestion_item, null) + val textView = v.findViewById(R.id.suggestion_text) + textView.text = suggestions[i] + textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_refresh, 0, 0, 0) + Handler(Looper.getMainLooper()).post { + searchHistoryLinearLayout!!.addView( + v + ) + } + textView.setOnClickListener { + searchView.setText(textView.text) + initSearch(searchView) + } + textView.setOnLongClickListener { + val deleteDialog = MaterialAlertDialogBuilder(requireContext()) + deleteDialog.setTitle(getString(R.string.you_are_going_to_delete) + " \"" + textView.text + "\"!") + deleteDialog.setNegativeButton(getString(R.string.cancel)) { dialogInterface: DialogInterface, _: Int -> dialogInterface.cancel() } + deleteDialog.setPositiveButton(getString(R.string.ok)) { _: DialogInterface?, _: Int -> + searchHistoryLinearLayout!!.removeView(v) + resultViewModel.removeSearchQueryFromHistory(textView.text.toString()) + } + deleteDialog.show() + true + } + + val mb = v.findViewById(R.id.set_search_query_button) + mb.setOnClickListener { + searchView.setText(textView.text) + } } - textView.setOnClickListener { - searchView.setText(textView.text) - initSearch(searchView) - } - val mb = v.findViewById(R.id.set_search_query_button) - mb.setOnClickListener { - searchView.setText(textView.text) + searchHistoryLinearLayout!!.visibility = VISIBLE + linkYouCopied!!.visibility = VISIBLE + }else{ + for (i in suggestions.indices) { + val v = LayoutInflater.from(fragmentContext) + .inflate(R.layout.search_suggestion_item, null) + val textView = v.findViewById(R.id.suggestion_text) + textView.text = suggestions[i] + Handler(Looper.getMainLooper()).post { + searchSuggestionsLinearLayout!!.addView( + v + ) + } + textView.setOnClickListener { + searchView.setText(textView.text) + initSearch(searchView) + } + val mb = v.findViewById(R.id.set_search_query_button) + mb.setOnClickListener { + searchView.setText(textView.text) + } } + searchSuggestionsLinearLayout!!.visibility = VISIBLE } + + } false } @@ -279,7 +341,7 @@ class HomeFragment : Fragment(), HomeAdapter.OnItemClickListener, View.OnClickLi } } - private fun initSearch(searchView: com.google.android.material.search.SearchView){ + private fun initSearch(searchView: SearchView){ val inputQuery = searchView.text!!.trim { it <= ' '} if (inputQuery.isEmpty()) return searchBar!!.text = searchView.text @@ -352,10 +414,12 @@ class HomeFragment : Fragment(), HomeAdapter.OnItemClickListener, View.OnClickLi downloadFabs!!.visibility = VISIBLE } else { downloadFabs!!.visibility = GONE - if (resultsList!![1]!!.playlistTitle.isNotEmpty() && resultsList!![1]!!.playlistTitle != getString(R.string.trendingPlaylist)){ - downloadAllFabCoordinator!!.visibility = VISIBLE - }else{ - downloadAllFabCoordinator!!.visibility = GONE + if(resultsList!!.size > 1){ + if (resultsList!![1]!!.playlistTitle.isNotEmpty() && resultsList!![1]!!.playlistTitle != getString(R.string.trendingPlaylist)){ + downloadAllFabCoordinator!!.visibility = VISIBLE + }else{ + downloadAllFabCoordinator!!.visibility = GONE + } } } } diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index 97543463..2e576636 100644 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -13,21 +13,65 @@ android:hint="@string/search_hint" app:layout_anchor="@id/search_bar"> - + - + + + + + + + + + + + + + + + + + + + + + + - - + + Download worker is running. Try again later yt-dlp Version Free space + Link you copied \ No newline at end of file