Android: Convert PlatformGamesFragment to Kotlin

This commit is contained in:
Charles Lombardo 2023-06-03 20:41:17 -04:00
parent 0915bfbb30
commit 3e8d6b8aa2
2 changed files with 122 additions and 147 deletions

View File

@ -1,147 +0,0 @@
// SPDX-License-Identifier: GPL-2.0-or-later
package org.dolphinemu.dolphinemu.ui.platform;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.google.android.material.color.MaterialColors;
import org.dolphinemu.dolphinemu.R;
import org.dolphinemu.dolphinemu.adapters.GameAdapter;
import org.dolphinemu.dolphinemu.databinding.FragmentGridBinding;
import org.dolphinemu.dolphinemu.layout.AutofitGridLayoutManager;
import org.dolphinemu.dolphinemu.services.GameFileCacheManager;
public final class PlatformGamesFragment extends Fragment implements PlatformGamesView
{
private static final String ARG_PLATFORM = "platform";
private SwipeRefreshLayout mSwipeRefresh;
private SwipeRefreshLayout.OnRefreshListener mOnRefreshListener;
private FragmentGridBinding mBinding;
public static PlatformGamesFragment newInstance(Platform platform)
{
PlatformGamesFragment fragment = new PlatformGamesFragment();
Bundle args = new Bundle();
args.putSerializable(ARG_PLATFORM, platform);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@NonNull
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
mBinding = FragmentGridBinding.inflate(inflater, container, false);
return mBinding.getRoot();
}
@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState)
{
mSwipeRefresh = mBinding.swipeRefresh;
GameAdapter adapter = new GameAdapter(requireActivity());
adapter.setStateRestorationPolicy(
RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY);
mBinding.gridGames.setAdapter(adapter);
mBinding.gridGames.setLayoutManager(new AutofitGridLayoutManager(requireContext(),
getResources().getDimensionPixelSize(R.dimen.card_width)));
// Set theme color to the refresh animation's background
mSwipeRefresh.setProgressBackgroundColorSchemeColor(
MaterialColors.getColor(mSwipeRefresh, R.attr.colorPrimary));
mSwipeRefresh.setColorSchemeColors(
MaterialColors.getColor(mSwipeRefresh, R.attr.colorOnPrimary));
mSwipeRefresh.setOnRefreshListener(mOnRefreshListener);
setInsets();
setRefreshing(GameFileCacheManager.isLoadingOrRescanning());
showGames();
}
@Override
public void onDestroyView()
{
super.onDestroyView();
mBinding = null;
}
@Override
public void onItemClick(String gameId)
{
// No-op for now
}
@Override
public void showGames()
{
if (mBinding == null)
return;
if (mBinding.gridGames.getAdapter() != null)
{
Platform platform = (Platform) getArguments().getSerializable(ARG_PLATFORM);
((GameAdapter) mBinding.gridGames.getAdapter()).swapDataSet(
GameFileCacheManager.getGameFilesForPlatform(platform));
}
}
@Override
public void refetchMetadata()
{
((GameAdapter) mBinding.gridGames.getAdapter()).refetchMetadata();
}
public void setOnRefreshListener(@Nullable SwipeRefreshLayout.OnRefreshListener listener)
{
mOnRefreshListener = listener;
if (mSwipeRefresh != null)
{
mSwipeRefresh.setOnRefreshListener(listener);
}
}
public void setRefreshing(boolean refreshing)
{
mBinding.swipeRefresh.setRefreshing(refreshing);
}
private void setInsets()
{
ViewCompat.setOnApplyWindowInsetsListener(mBinding.gridGames, (v, windowInsets) ->
{
Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(0, 0, 0,
insets.bottom + getResources().getDimensionPixelSize(R.dimen.spacing_list) +
getResources().getDimensionPixelSize(R.dimen.spacing_fab));
return windowInsets;
});
}
}

View File

@ -0,0 +1,122 @@
// SPDX-License-Identifier: GPL-2.0-or-later
package org.dolphinemu.dolphinemu.ui.platform
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout.OnRefreshListener
import com.google.android.material.color.MaterialColors
import org.dolphinemu.dolphinemu.R
import org.dolphinemu.dolphinemu.adapters.GameAdapter
import org.dolphinemu.dolphinemu.databinding.FragmentGridBinding
import org.dolphinemu.dolphinemu.layout.AutofitGridLayoutManager
import org.dolphinemu.dolphinemu.services.GameFileCacheManager
class PlatformGamesFragment : Fragment(), PlatformGamesView {
private var swipeRefresh: SwipeRefreshLayout? = null
private var onRefreshListener: OnRefreshListener? = null
private var _binding: FragmentGridBinding? = null
private val binding: FragmentGridBinding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentGridBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
swipeRefresh = binding.swipeRefresh
val gameAdapter = GameAdapter(requireActivity())
gameAdapter.stateRestorationPolicy =
RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY
binding.gridGames.apply {
adapter = gameAdapter
layoutManager = AutofitGridLayoutManager(
requireContext(),
resources.getDimensionPixelSize(R.dimen.card_width)
)
}
// Set theme color to the refresh animation's background
binding.swipeRefresh.apply {
setProgressBackgroundColorSchemeColor(
MaterialColors.getColor(swipeRefresh!!, R.attr.colorPrimary)
)
setColorSchemeColors(MaterialColors.getColor(swipeRefresh!!, R.attr.colorOnPrimary))
setOnRefreshListener(onRefreshListener)
}
setInsets()
setRefreshing(GameFileCacheManager.isLoadingOrRescanning())
showGames()
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
override fun showGames() {
if (_binding == null)
return
if (binding.gridGames.adapter != null) {
val platform = requireArguments().getSerializable(ARG_PLATFORM) as Platform
(binding.gridGames.adapter as GameAdapter?)!!.swapDataSet(
GameFileCacheManager.getGameFilesForPlatform(platform)
)
}
}
override fun refetchMetadata() {
(binding.gridGames.adapter as GameAdapter).refetchMetadata()
}
fun setOnRefreshListener(listener: OnRefreshListener?) {
onRefreshListener = listener
swipeRefresh?.setOnRefreshListener(listener)
}
override fun setRefreshing(refreshing: Boolean) {
binding.swipeRefresh.isRefreshing = refreshing
}
private fun setInsets() {
ViewCompat.setOnApplyWindowInsetsListener(binding.gridGames) { v: View, windowInsets: WindowInsetsCompat ->
val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(
0,
0,
0,
insets.bottom + resources.getDimensionPixelSize(R.dimen.spacing_list)
+ resources.getDimensionPixelSize(R.dimen.spacing_fab)
)
windowInsets
}
}
companion object {
private const val ARG_PLATFORM = "platform"
@JvmStatic
fun newInstance(platform: Platform?): PlatformGamesFragment {
val fragment = PlatformGamesFragment()
val args = Bundle()
args.putSerializable(ARG_PLATFORM, platform)
fragment.arguments = args
return fragment
}
}
}