From 2e9238a512f47f345566a9d9afc9fc1c1324d323 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 26 Jun 2022 19:21:54 +0200 Subject: [PATCH 1/3] fix minor bugs --- .../java/com/github/libretube/fragments/Library.kt | 11 +++++------ .../com/github/libretube/util/PreferenceHelper.kt | 10 +++++----- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/github/libretube/fragments/Library.kt b/app/src/main/java/com/github/libretube/fragments/Library.kt index d0dbf7736..d459a85d2 100644 --- a/app/src/main/java/com/github/libretube/fragments/Library.kt +++ b/app/src/main/java/com/github/libretube/fragments/Library.kt @@ -28,7 +28,6 @@ class Library : Fragment() { lateinit var token: String private lateinit var playlistRecyclerView: RecyclerView private lateinit var refreshLayout: SwipeRefreshLayout - private lateinit var createPlaylistButton: FloatingActionButton override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -57,10 +56,9 @@ class Library : Fragment() { fetchPlaylists(view) refreshLayout.isEnabled = true refreshLayout.setOnRefreshListener { - Log.d(TAG, "hmm") fetchPlaylists(view) } - createPlaylistButton = view.findViewById(R.id.create_playlist) + val createPlaylistButton = view.findViewById(R.id.create_playlist) createPlaylistButton.setOnClickListener { val newFragment = CreatePlaylistDialog() newFragment.show(childFragmentManager, "Create Playlist") @@ -75,10 +73,11 @@ class Library : Fragment() { } override fun onResume() { - // optimize CreatePlaylistFab bottom margin - val layoutParams = createPlaylistButton.layoutParams as ViewGroup.MarginLayoutParams + // optimize CreatePlaylistFab bottom margin if miniPlayer active + val createPlaylistButton = view?.findViewById(R.id.create_playlist) + val layoutParams = createPlaylistButton?.layoutParams as ViewGroup.MarginLayoutParams layoutParams.bottomMargin = if (isMiniPlayerVisible) 180 else 64 - createPlaylistButton.layoutParams = layoutParams + createPlaylistButton?.layoutParams = layoutParams super.onResume() } diff --git a/app/src/main/java/com/github/libretube/util/PreferenceHelper.kt b/app/src/main/java/com/github/libretube/util/PreferenceHelper.kt index 950d17d13..79f67e39f 100644 --- a/app/src/main/java/com/github/libretube/util/PreferenceHelper.kt +++ b/app/src/main/java/com/github/libretube/util/PreferenceHelper.kt @@ -56,13 +56,13 @@ object PreferenceHelper { fun clearPreferences(context: Context) { val editor = getDefaultSharedPreferencesEditor(context) editor.clear() - editor.commit() + editor.apply() } fun removePreference(context: Context, value: String?) { val editor = getDefaultSharedPreferencesEditor(context) editor.remove(value) - editor.commit() + editor.apply() } fun getToken(context: Context): String { @@ -72,7 +72,7 @@ object PreferenceHelper { fun setToken(context: Context, newValue: String) { val editor = context.getSharedPreferences("token", Context.MODE_PRIVATE).edit() - editor.putString("token", newValue) + editor.putString("token", newValue).apply() } fun getUsername(context: Context): String { @@ -82,7 +82,7 @@ object PreferenceHelper { fun setUsername(context: Context, newValue: String) { val editor = context.getSharedPreferences("username", Context.MODE_PRIVATE).edit() - editor.putString("username", newValue) + editor.putString("username", newValue).apply() } fun saveCustomInstance(context: Context, customInstance: CustomInstance) { @@ -93,7 +93,7 @@ object PreferenceHelper { customInstancesList += customInstance val json = gson.toJson(customInstancesList) - editor.putString("customInstances", json).commit() + editor.putString("customInstances", json).apply() } fun getCustomInstances(context: Context): ArrayList { From 731379e00617ba1400b6a5d38c5e9e3e8e23b98b Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 26 Jun 2022 19:34:54 +0200 Subject: [PATCH 2/3] optimize playlist creation --- .../libretube/dialogs/CreatePlaylistDialog.kt | 10 +++++-- .../com/github/libretube/fragments/Library.kt | 29 +++++++------------ .../github/libretube/util/PreferenceHelper.kt | 6 ++-- 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/com/github/libretube/dialogs/CreatePlaylistDialog.kt b/app/src/main/java/com/github/libretube/dialogs/CreatePlaylistDialog.kt index ce69b89ba..85c1f57a6 100644 --- a/app/src/main/java/com/github/libretube/dialogs/CreatePlaylistDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/CreatePlaylistDialog.kt @@ -14,6 +14,7 @@ import androidx.fragment.app.DialogFragment import androidx.fragment.app.setFragmentResult import androidx.lifecycle.lifecycleScope import com.github.libretube.R +import com.github.libretube.fragments.Library import com.github.libretube.obj.Playlists import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.RetrofitInstance @@ -87,8 +88,13 @@ class CreatePlaylistDialog : DialogFragment() { Toast.makeText(context, getString(R.string.unknown_error), Toast.LENGTH_SHORT) .show() } - // tell the Subscription Activity to fetch the playlists again - setFragmentResult("fetchPlaylists", bundleOf("" to "")) + // refresh the playlists in the library + try { + val parent = parentFragment as Library + parent.fetchPlaylists() + } catch (e: Exception) { + Log.e(TAG, e.toString()) + } dismiss() } } diff --git a/app/src/main/java/com/github/libretube/fragments/Library.kt b/app/src/main/java/com/github/libretube/fragments/Library.kt index d459a85d2..4c844066e 100644 --- a/app/src/main/java/com/github/libretube/fragments/Library.kt +++ b/app/src/main/java/com/github/libretube/fragments/Library.kt @@ -53,19 +53,16 @@ class Library : Fragment() { if (token != "") { view.findViewById(R.id.boogh2).visibility = View.GONE view.findViewById(R.id.textLike2).visibility = View.GONE - fetchPlaylists(view) + fetchPlaylists() refreshLayout.isEnabled = true refreshLayout.setOnRefreshListener { - fetchPlaylists(view) + fetchPlaylists() } val createPlaylistButton = view.findViewById(R.id.create_playlist) createPlaylistButton.setOnClickListener { val newFragment = CreatePlaylistDialog() newFragment.show(childFragmentManager, "Create Playlist") } - childFragmentManager.setFragmentResultListener("fetchPlaylists", this) { _, _ -> - fetchPlaylists(view) - } } else { refreshLayout.isEnabled = false view.findViewById(R.id.create_playlist).visibility = View.GONE @@ -81,7 +78,7 @@ class Library : Fragment() { super.onResume() } - private fun fetchPlaylists(view: View) { + fun fetchPlaylists() { fun run() { refreshLayout.isRefreshing = true lifecycleScope.launchWhenCreated { @@ -101,12 +98,8 @@ class Library : Fragment() { } if (response.isNotEmpty()) { runOnUiThread { - with(view.findViewById(R.id.boogh2)) { - visibility = View.GONE - } - with(view.findViewById(R.id.textLike2)) { - visibility = View.GONE - } + view?.findViewById(R.id.boogh2)?.visibility = View.GONE + view?.findViewById(R.id.textLike2)?.visibility = View.GONE } val playlistsAdapter = PlaylistsAdapter( response.toMutableList(), @@ -115,13 +108,13 @@ class Library : Fragment() { playlistRecyclerView.adapter = playlistsAdapter } else { runOnUiThread { - with(view.findViewById(R.id.boogh2)) { - visibility = View.VISIBLE - setImageResource(R.drawable.ic_list) + view?.findViewById(R.id.boogh2).apply { + this?.visibility = View.VISIBLE + this?.setImageResource(R.drawable.ic_list) } - with(view.findViewById(R.id.textLike2)) { - visibility = View.VISIBLE - text = getString(R.string.emptyList) + view?.findViewById(R.id.textLike2).apply { + this?.visibility = View.VISIBLE + this?.text = getString(R.string.emptyList) } } } diff --git a/app/src/main/java/com/github/libretube/util/PreferenceHelper.kt b/app/src/main/java/com/github/libretube/util/PreferenceHelper.kt index 79f67e39f..310a02578 100644 --- a/app/src/main/java/com/github/libretube/util/PreferenceHelper.kt +++ b/app/src/main/java/com/github/libretube/util/PreferenceHelper.kt @@ -55,14 +55,12 @@ object PreferenceHelper { fun clearPreferences(context: Context) { val editor = getDefaultSharedPreferencesEditor(context) - editor.clear() - editor.apply() + editor.clear().apply() } fun removePreference(context: Context, value: String?) { val editor = getDefaultSharedPreferencesEditor(context) - editor.remove(value) - editor.apply() + editor.remove(value).apply() } fun getToken(context: Context): String { From 042fab09a4519a273fe1687467cafaa9a7c73e57 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 26 Jun 2022 19:35:27 +0200 Subject: [PATCH 3/3] imports --- .../java/com/github/libretube/dialogs/CreatePlaylistDialog.kt | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/src/main/java/com/github/libretube/dialogs/CreatePlaylistDialog.kt b/app/src/main/java/com/github/libretube/dialogs/CreatePlaylistDialog.kt index 85c1f57a6..d08256704 100644 --- a/app/src/main/java/com/github/libretube/dialogs/CreatePlaylistDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/CreatePlaylistDialog.kt @@ -8,10 +8,8 @@ import android.view.View import android.widget.Button import android.widget.TextView import android.widget.Toast -import androidx.core.os.bundleOf import androidx.core.text.HtmlCompat import androidx.fragment.app.DialogFragment -import androidx.fragment.app.setFragmentResult import androidx.lifecycle.lifecycleScope import com.github.libretube.R import com.github.libretube.fragments.Library