cleanup and fixes

This commit is contained in:
Bnyro 2022-05-29 10:32:44 +02:00
parent ff2e103586
commit 0556af2cdd
4 changed files with 33 additions and 27 deletions

View File

@ -50,9 +50,11 @@ class CreatePlaylistDialog : DialogFragment() {
val playlistName = view.findViewById<TextInputEditText>(R.id.playlist_name)
val createPlaylistBtn = view.findViewById<Button>(R.id.create_new_playlist)
createPlaylistBtn.setOnClickListener {
var listName = playlistName.text.toString()
// avoid creating the same playlist multiple times by spamming the button
createPlaylistBtn.setOnClickListener(null)
val listName = playlistName.text.toString()
if (listName != "") {
createPlaylist("$listName")
createPlaylist(listName)
} else {
Toast.makeText(context, R.string.emptyPlaylistName, Toast.LENGTH_LONG).show()
}
@ -83,7 +85,7 @@ class CreatePlaylistDialog : DialogFragment() {
Toast.makeText(context, getString(R.string.unknown_error), Toast.LENGTH_SHORT)
.show()
}
}.invokeOnCompletion {
// tell the Subscription Activity to fetch the playlists again
setFragmentResult("fetchPlaylists", bundleOf("" to ""))
dismiss()
}

View File

@ -23,8 +23,8 @@ class Library : Fragment() {
private val TAG = "LibraryFragment"
lateinit var token: String
lateinit var playlistRecyclerView: RecyclerView
lateinit var refreshLayout: SwipeRefreshLayout
private lateinit var playlistRecyclerView: RecyclerView
private lateinit var refreshLayout: SwipeRefreshLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {

View File

@ -62,13 +62,14 @@ class SearchFragment : Fragment() {
searchRecView = view.findViewById<RecyclerView>(R.id.search_recycler)
val autoTextView = view.findViewById<AutoCompleteTextView>(R.id.autoCompleteTextView)
val historyRecycler = view.findViewById<RecyclerView>(R.id.history_recycler)
val filterImageView = view.findViewById<ImageView>(R.id.filterMenu_imageView)
var tempSelectedItem = 0
val sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(requireContext())
filterImageView.setOnClickListener {
val filterOptions = arrayOf(
getString(R.string.all),
@ -119,10 +120,10 @@ class SearchFragment : Fragment() {
historyRecycler.layoutManager = LinearLayoutManager(view.context)
var historylist = getHistory()
if (historylist.isNotEmpty()) {
val historyList = getHistory()
if (historyList.isNotEmpty()) {
historyRecycler.adapter =
SearchHistoryAdapter(requireContext(), historylist, autoTextView)
SearchHistoryAdapter(requireContext(), historyList, autoTextView)
}
searchRecView.layoutManager = GridLayoutManager(view.context, 1)
@ -155,13 +156,6 @@ class SearchFragment : Fragment() {
GlobalScope.launch {
fetchSuggestions(s.toString(), autoTextView)
delay(1000)
val sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(requireContext())
if (sharedPreferences.getBoolean(
"search_history_toggle",
true
)
) addtohistory(s.toString())
fetchSearch(s.toString())
}
}
@ -171,10 +165,10 @@ class SearchFragment : Fragment() {
if (s!!.isEmpty()) {
searchRecView.visibility = GONE
historyRecycler.visibility = VISIBLE
var historylist = getHistory()
if (historylist.isNotEmpty()) {
val historyList = getHistory()
if (historyList.isNotEmpty()) {
historyRecycler.adapter =
SearchHistoryAdapter(requireContext(), historylist, autoTextView)
SearchHistoryAdapter(requireContext(), historyList, autoTextView)
}
}
}
@ -184,6 +178,14 @@ class SearchFragment : Fragment() {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
hideKeyboard()
autoTextView.dismissDropDown()
if (sharedPreferences.getBoolean(
"search_history_toggle",
true
)
) {
val newString = autoTextView.text.toString()
addToHistory(newString)
}
return@OnEditorActionListener true
}
false
@ -276,12 +278,12 @@ class SearchFragment : Fragment() {
hideKeyboard()
}
private fun addtohistory(query: String) {
private fun addToHistory(query: String) {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
var historyList = getHistory()
if (historyList.size != 0 && query == historyList.get(historyList.size - 1)) {
if (historyList.isNotEmpty() && query == historyList[historyList.size - 1]) {
return
} else if (query == "") {
return
@ -293,19 +295,19 @@ class SearchFragment : Fragment() {
historyList = historyList.takeLast(10)
}
var set: Set<String> = HashSet(historyList)
val set: Set<String> = HashSet(historyList)
sharedPreferences.edit().putStringSet("search_history", set)
.apply()
}
private fun getHistory(): List<String> {
try {
return try {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
val set: Set<String> = sharedPreferences.getStringSet("search_history", HashSet())!!
return set.toList()
set.toList()
} catch (e: Exception) {
return emptyList()
emptyList()
}
}
}

View File

@ -41,7 +41,7 @@ fun updateLanguage(context: Context) {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
val languageName = sharedPreferences.getString("language", "sys")
if (languageName != "") {
var locale = if (languageName != "sys" && "$languageName".length < 3) {
val locale = if (languageName != "sys" && "$languageName".length < 3) {
Locale(languageName)
} else if ("$languageName".length > 3) {
Locale(languageName?.substring(0, 2), languageName?.substring(4, 6))
@ -51,7 +51,9 @@ fun updateLanguage(context: Context) {
val res = context.resources
val dm = res.displayMetrics
val conf = res.configuration
// Change App Language
conf.setLocale(locale)
// Change API Language
Locale.setDefault(locale)
res.updateConfiguration(conf, dm)
}