Android: Convert CheatListFragment to Kotlin

This commit is contained in:
Charles Lombardo 2023-03-01 13:39:07 -05:00
parent d8c1381155
commit 3bcc4a0a59
2 changed files with 74 additions and 82 deletions

View File

@ -1,82 +0,0 @@
// SPDX-License-Identifier: GPL-2.0-or-later
package org.dolphinemu.dolphinemu.features.cheats.ui;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.ColorInt;
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.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.google.android.material.color.MaterialColors;
import com.google.android.material.divider.MaterialDividerItemDecoration;
import com.google.android.material.elevation.ElevationOverlayProvider;
import org.dolphinemu.dolphinemu.R;
import org.dolphinemu.dolphinemu.databinding.FragmentCheatListBinding;
import org.dolphinemu.dolphinemu.features.cheats.model.CheatsViewModel;
public class CheatListFragment extends Fragment
{
private FragmentCheatListBinding mBinding;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState)
{
mBinding = FragmentCheatListBinding.inflate(inflater, container, false);
return mBinding.getRoot();
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
{
CheatsActivity activity = (CheatsActivity) requireActivity();
CheatsViewModel viewModel = new ViewModelProvider(activity).get(CheatsViewModel.class);
mBinding.cheatList.setAdapter(new CheatsAdapter(activity, viewModel));
mBinding.cheatList.setLayoutManager(new LinearLayoutManager(activity));
MaterialDividerItemDecoration divider =
new MaterialDividerItemDecoration(requireActivity(), LinearLayoutManager.VERTICAL);
divider.setLastItemDecorated(false);
mBinding.cheatList.addItemDecoration(divider);
@ColorInt int color =
new ElevationOverlayProvider(mBinding.cheatsWarning.getContext()).compositeOverlay(
MaterialColors.getColor(mBinding.cheatsWarning, R.attr.colorSurface),
getResources().getDimensionPixelSize(R.dimen.elevated_app_bar));
mBinding.cheatsWarning.setBackgroundColor(color);
mBinding.gfxModsWarning.setBackgroundColor(color);
setInsets();
}
@Override
public void onDestroyView()
{
super.onDestroyView();
mBinding = null;
}
private void setInsets()
{
ViewCompat.setOnApplyWindowInsetsListener(mBinding.cheatList, (v, windowInsets) ->
{
Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(0, 0, 0,
insets.bottom + getResources().getDimensionPixelSize(R.dimen.spacing_xtralarge));
return windowInsets;
});
}
}

View File

@ -0,0 +1,74 @@
// SPDX-License-Identifier: GPL-2.0-or-later
package org.dolphinemu.dolphinemu.features.cheats.ui
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.ColorInt
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.color.MaterialColors
import com.google.android.material.divider.MaterialDividerItemDecoration
import com.google.android.material.elevation.ElevationOverlayProvider
import org.dolphinemu.dolphinemu.R
import org.dolphinemu.dolphinemu.databinding.FragmentCheatListBinding
import org.dolphinemu.dolphinemu.features.cheats.model.CheatsViewModel
class CheatListFragment : Fragment() {
private var _binding: FragmentCheatListBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentCheatListBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val activity = requireActivity() as CheatsActivity
val viewModel = ViewModelProvider(activity)[CheatsViewModel::class.java]
binding.cheatList.adapter = CheatsAdapter(activity, viewModel)
binding.cheatList.layoutManager = LinearLayoutManager(activity)
val divider = MaterialDividerItemDecoration(requireActivity(), LinearLayoutManager.VERTICAL)
divider.isLastItemDecorated = false
binding.cheatList.addItemDecoration(divider)
@ColorInt val color =
ElevationOverlayProvider(binding.cheatsWarning.context).compositeOverlay(
MaterialColors.getColor(binding.cheatsWarning, R.attr.colorSurface),
resources.getDimensionPixelSize(R.dimen.elevated_app_bar).toFloat()
)
binding.cheatsWarning.setBackgroundColor(color)
binding.gfxModsWarning.setBackgroundColor(color)
setInsets()
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
private fun setInsets() {
ViewCompat.setOnApplyWindowInsetsListener(binding.cheatList) { v: View, windowInsets: WindowInsetsCompat ->
val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(
0,
0,
0,
insets.bottom + resources.getDimensionPixelSize(R.dimen.spacing_xtralarge)
)
windowInsets
}
}
}