2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-28 04:37:40 +00:00

Migrate list fragments to Kotlin

This commit is contained in:
TPJ Schikhof 2024-10-04 19:48:41 +02:00 committed by Philip Cohn-Cort
parent 7210fd8425
commit 8fea00a843
5 changed files with 85 additions and 109 deletions

View File

@ -95,7 +95,7 @@ class AboutFragment : Fragment() {
setupInfoButton(aboutData.websiteURL, binding!!.websiteButton) setupInfoButton(aboutData.websiteURL, binding!!.websiteButton)
// Update authors // Update authors
binding!!.authorsList.adapter = ListAdapter(this.context, aboutData.authors.map { AboutPersonEntryItem(it) }, false) binding!!.authorsList.adapter = ListAdapter(this.requireContext(), aboutData.authors.map { AboutPersonEntryItem(it) }, false)
if (aboutData.authorsFooterText != null) { if (aboutData.authorsFooterText != null) {
binding!!.authorsFooterText.text = context?.getString(aboutData.authorsFooterText!!) binding!!.authorsFooterText.text = context?.getString(aboutData.authorsFooterText!!)
} }

View File

@ -3,34 +3,28 @@
* *
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/ */
package org.kde.kdeconnect.UserInterface.List
package org.kde.kdeconnect.UserInterface.List; import android.graphics.drawable.Drawable
import android.view.LayoutInflater
import android.view.View
import org.kde.kdeconnect_tp.databinding.ListCardEntryBinding
import android.graphics.drawable.Drawable; open class EntryItemWithIcon : ListAdapter.Item {
import android.view.LayoutInflater; protected val title: String
import android.view.View; protected val icon: Drawable
import androidx.annotation.NonNull; constructor(title: String, icon: Drawable) {
this.title = title
import org.kde.kdeconnect_tp.databinding.ListCardEntryBinding; this.icon = icon
public class EntryItemWithIcon implements ListAdapter.Item {
protected final String title;
protected final Drawable icon;
public EntryItemWithIcon(String title, Drawable icon) {
this.title = title;
this.icon = icon;
} }
@NonNull override fun inflateView(layoutInflater: LayoutInflater): View {
@Override val binding = ListCardEntryBinding.inflate(layoutInflater)
public View inflateView(@NonNull LayoutInflater layoutInflater) {
final ListCardEntryBinding binding = ListCardEntryBinding.inflate(layoutInflater);
binding.listItemEntryTitle.setText(title); binding.listItemEntryTitle.text = title
binding.listItemEntryIcon.setImageDrawable(icon); binding.listItemEntryIcon.setImageDrawable(icon)
return binding.getRoot(); return binding.root
} }
} }

View File

@ -3,47 +3,34 @@
* *
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/ */
package org.kde.kdeconnect.UserInterface.List
package org.kde.kdeconnect.UserInterface.List; import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.content.Context; class ListAdapter : ArrayAdapter<ListAdapter.Item> {
import android.view.LayoutInflater; private val items: List<Item>
import android.view.View; private val isEnabled: Boolean
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import androidx.annotation.NonNull; @JvmOverloads
constructor(context: Context, items: List<Item>, isEnabled: Boolean = true) : super(context, 0, items) {
import java.util.List; this.items = items
this.isEnabled = isEnabled
public class ListAdapter extends ArrayAdapter<ListAdapter.Item> {
public interface Item {
@NonNull
View inflateView(@NonNull LayoutInflater layoutInflater);
} }
private final List<? extends Item> items; interface Item {
private final boolean isEnabled; fun inflateView(layoutInflater: LayoutInflater): View
public ListAdapter(Context context, List<? extends Item> items) {
this(context, items, true);
} }
public ListAdapter(Context context, List<? extends Item> items, boolean isEnabled) { override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
super(context, 0, (List<Item>) items); val i = items[position]
this.items = items; return i.inflateView(LayoutInflater.from(parent.context))
this.isEnabled = isEnabled;
} }
@NonNull override fun isEnabled(position: Int): Boolean {
@Override return isEnabled
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
final Item i = items.get(position);
return i.inflateView(LayoutInflater.from(parent.getContext()));
}
@Override
public boolean isEnabled(int position) {
return isEnabled;
} }
} }

View File

@ -3,42 +3,37 @@
* *
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/ */
package org.kde.kdeconnect.UserInterface.List
package org.kde.kdeconnect.UserInterface.List; import android.view.LayoutInflater
import android.view.View
import org.kde.kdeconnect_tp.databinding.ListItemCategoryBinding
import android.view.LayoutInflater; class SectionItem : ListAdapter.Item {
import android.view.View; private val title: String
import androidx.annotation.NonNull; constructor(title: String) {
this.title = title
import org.kde.kdeconnect_tp.databinding.ListItemCategoryBinding;
public class SectionItem implements ListAdapter.Item {
private final String title;
public boolean isEmpty;
public SectionItem(String title) {
this.title = title;
this.isEmpty = true;
} }
@NonNull @JvmField
@Override var isEmpty: Boolean = true
public View inflateView(@NonNull LayoutInflater layoutInflater) {
final ListItemCategoryBinding binding = ListItemCategoryBinding.inflate(layoutInflater); override fun inflateView(layoutInflater: LayoutInflater): View {
val binding = ListItemCategoryBinding.inflate(layoutInflater)
// Make it not selectable // Make it not selectable
binding.getRoot().setOnClickListener(null); binding.root.setOnClickListener(null)
binding.getRoot().setOnLongClickListener(null); binding.root.setOnLongClickListener(null)
binding.getRoot().setFocusable(false); binding.root.isFocusable = false
binding.getRoot().setClickable(false); binding.root.isClickable = false
binding.listItemCategoryText.setText(title); binding.listItemCategoryText.text = title
if (isEmpty) { if (isEmpty) {
binding.listItemCategoryEmptyPlaceholder.setVisibility(View.VISIBLE); binding.listItemCategoryEmptyPlaceholder.visibility = View.VISIBLE
} }
return binding.getRoot(); return binding.root
} }
} }

View File

@ -3,43 +3,43 @@
* *
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/ */
package org.kde.kdeconnect.UserInterface.List
package org.kde.kdeconnect.UserInterface.List; import android.R
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import android.util.TypedValue; class SmallEntryItem : ListAdapter.Item {
import android.view.LayoutInflater; private val title: String
import android.view.View; private val clickListener: View.OnClickListener?
import android.widget.TextView;
import androidx.annotation.NonNull; constructor(title: String, clickListener: View.OnClickListener?) {
this.title = title
public class SmallEntryItem implements ListAdapter.Item { this.clickListener = clickListener
private final String title;
private final View.OnClickListener clickListener;
SmallEntryItem(String title, View.OnClickListener clickListener) {
this.title = title;
this.clickListener = clickListener;
} }
@NonNull override fun inflateView(layoutInflater: LayoutInflater): View {
@Override val v = layoutInflater.inflate(R.layout.simple_list_item_1, null)
public View inflateView(@NonNull LayoutInflater layoutInflater) { val padding = (28 * layoutInflater.context.resources.displayMetrics.density).toInt()
View v = layoutInflater.inflate(android.R.layout.simple_list_item_1, null); v.setPadding(padding, 0, padding, 0)
final int padding = (int) (28 * layoutInflater.getContext().getResources().getDisplayMetrics().density);
v.setPadding(padding, 0, padding, 0);
TextView titleView = v.findViewById(android.R.id.text1); val titleView = v.findViewById<TextView>(R.id.text1)
if (titleView != null) { if (titleView != null) {
titleView.setText(title); titleView.text = title
if (clickListener != null) { if (clickListener != null) {
titleView.setOnClickListener(clickListener); titleView.setOnClickListener(clickListener)
TypedValue outValue = new TypedValue(); val outValue = TypedValue()
layoutInflater.getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true); layoutInflater.context.theme.resolveAttribute(
v.setBackgroundResource(outValue.resourceId); R.attr.selectableItemBackground,
outValue,
true
)
v.setBackgroundResource(outValue.resourceId)
} }
} }
return v; return v
} }
} }