2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-22 09:58:08 +00:00

Port device list items to Kotlin

This commit is contained in:
Albert Vaca Cintora 2025-05-26 14:10:18 +02:00
parent 5bc41367ed
commit c0a41034fa
No known key found for this signature in database
10 changed files with 90 additions and 131 deletions

View File

@ -166,7 +166,7 @@ public class SendKeystrokesToHostActivity extends BaseActivity<ActivitySendkeyst
for (Device d : devices) { for (Device d : devices) {
if (d.isReachable() && d.isPaired()) { if (d.isReachable() && d.isPaired()) {
devicesList.add(d); devicesList.add(d);
items.add(new DeviceItem(d, null)); items.add(new DeviceItem(d));
section.isEmpty = false; section.isEmpty = false;
} }
} }

View File

@ -49,7 +49,7 @@ class RunCommandWidgetConfigActivity : AppCompatActivity() {
val pairedDevices = KdeConnect.getInstance().devices.values.stream().filter(Device::isPaired).collect(Collectors.toList()) val pairedDevices = KdeConnect.getInstance().devices.values.stream().filter(Device::isPaired).collect(Collectors.toList())
val list = ListAdapter(this, pairedDevices.map { DeviceItem(it, null) }) val list = ListAdapter(this, pairedDevices.map { DeviceItem(it) })
binding.runCommandsDeviceList.adapter = list binding.runCommandsDeviceList.adapter = list
binding.runCommandsDeviceList.setOnItemClickListener { _, _, position, _ -> binding.runCommandsDeviceList.setOnItemClickListener { _, _, position, _ ->
val deviceId = pairedDevices[position].deviceId val deviceId = pairedDevices[position].deviceId

View File

@ -111,9 +111,9 @@ public class ShareActivity extends BaseActivity<ActivityShareBinding> {
if (d.isPaired() && (intentHasUrl || d.isReachable())) { if (d.isPaired() && (intentHasUrl || d.isReachable())) {
devicesList.add(d); devicesList.add(d);
if (!d.isReachable()) { if (!d.isReachable()) {
items.add(new UnreachableDeviceItem(d, null)); items.add(new UnreachableDeviceItem(d));
} else { } else {
items.add(new DeviceItem(d, null)); items.add(new DeviceItem(d));
} }
section.isEmpty = false; section.isEmpty = false;
} }

View File

@ -1,52 +0,0 @@
/*
* SPDX-FileCopyrightText: 2014 Albert Vaca Cintora <albertvaka@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
package org.kde.kdeconnect.UserInterface.List;
import android.view.LayoutInflater;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.kde.kdeconnect.Device;
import org.kde.kdeconnect_tp.databinding.ListItemDeviceEntryBinding;
public class DeviceItem implements ListAdapter.Item {
public interface Callback {
void pairingClicked(Device d);
}
private final @Nullable Callback callback;
protected final @NonNull Device device;
protected ListItemDeviceEntryBinding binding;
public DeviceItem(@NonNull Device device, @Nullable Callback callback) {
this.device = device;
this.callback = callback;
}
public @NonNull Device getDevice() {
return this.device;
}
@NonNull
@Override
public View inflateView(@NonNull LayoutInflater layoutInflater) {
binding = ListItemDeviceEntryBinding.inflate(layoutInflater);
binding.listItemEntryIcon.setImageDrawable(device.getIcon());
binding.listItemEntryTitle.setText(device.getName());
if (callback != null) {
binding.getRoot().setOnClickListener(v1 -> callback.pairingClicked(device));
}
return binding.getRoot();
}
}

View File

@ -0,0 +1,36 @@
/*
* SPDX-FileCopyrightText: 2025 Albert Vaca Cintora <albertvaka@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
package org.kde.kdeconnect.UserInterface.List
import android.view.LayoutInflater
import android.view.View
import org.kde.kdeconnect.Device
import org.kde.kdeconnect_tp.databinding.ListItemDeviceEntryBinding
open class DeviceItem(
val device: Device,
private val callback: ((d: Device) -> Unit)?
) : ListAdapter.Item
{
constructor(device: Device) : this(device, null)
protected lateinit var binding: ListItemDeviceEntryBinding
override fun inflateView(layoutInflater: LayoutInflater): View {
binding = ListItemDeviceEntryBinding.inflate(layoutInflater)
binding.listItemEntryIcon.setImageDrawable(device.icon)
binding.listItemEntryTitle.text = device.name
if (callback != null) {
binding.getRoot().setOnClickListener(View.OnClickListener { v1: View? ->
callback(device)
})
}
return binding.getRoot()
}
}

View File

@ -1,36 +0,0 @@
/*
* SPDX-FileCopyrightText: 2014 Albert Vaca Cintora <albertvaka@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
package org.kde.kdeconnect.UserInterface.List;
import android.view.LayoutInflater;
import android.view.View;
import androidx.annotation.NonNull;
import org.kde.kdeconnect.Device;
import org.kde.kdeconnect_tp.R;
public class PairingDeviceItem extends DeviceItem {
public PairingDeviceItem(Device device, Callback callback) {
super(device, callback);
}
@NonNull
@Override
public View inflateView(@NonNull LayoutInflater layoutInflater) {
View ret = super.inflateView(layoutInflater);
if (device.compareProtocolVersion() > 0) {
binding.listItemEntrySummary.setText(R.string.protocol_version_newer);
binding.listItemEntrySummary.setVisibility(View.VISIBLE);
} else {
binding.listItemEntrySummary.setVisibility(View.GONE);
}
return ret;
}
}

View File

@ -0,0 +1,25 @@
/*
* SPDX-FileCopyrightText: 2025 Albert Vaca Cintora <albertvaka@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
package org.kde.kdeconnect.UserInterface.List
import android.view.LayoutInflater
import android.view.View
import org.kde.kdeconnect.Device
import org.kde.kdeconnect_tp.R
class PairingDeviceItem(
device: Device,
callback: ((d: Device) -> Unit)?
) : DeviceItem(device, callback) {
override fun inflateView(layoutInflater: LayoutInflater): View {
return super.inflateView(layoutInflater).also {
if (device.compareProtocolVersion() > 0) {
binding.listItemEntrySummary.setText(R.string.protocol_version_newer)
binding.listItemEntrySummary.visibility = View.VISIBLE
}
}
}
}

View File

@ -1,34 +0,0 @@
/*
* SPDX-FileCopyrightText: 2014 Albert Vaca Cintora <albertvaka@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
package org.kde.kdeconnect.UserInterface.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import androidx.annotation.NonNull;
import org.kde.kdeconnect.Device;
import org.kde.kdeconnect_tp.R;
public class UnreachableDeviceItem extends DeviceItem {
public UnreachableDeviceItem(Device device, Callback callback) {
super(device, callback);
}
@NonNull
@Override
public View inflateView(@NonNull LayoutInflater layoutInflater) {
View ret = super.inflateView(layoutInflater);
binding.listItemEntryTitle.setText(device.getName());
binding.listItemEntrySummary.setText(R.string.runcommand_notreachable);
binding.listItemEntrySummary.setVisibility(View.VISIBLE);
return ret;
}
}

View File

@ -0,0 +1,20 @@
/*
* SPDX-FileCopyrightText: 2025 Albert Vaca Cintora <albertvaka@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
package org.kde.kdeconnect.UserInterface.List
import android.view.LayoutInflater
import android.view.View
import org.kde.kdeconnect.Device
import org.kde.kdeconnect_tp.R
class UnreachableDeviceItem(device: Device) : DeviceItem(device) {
override fun inflateView(layoutInflater: LayoutInflater): View {
return super.inflateView(layoutInflater).also {
binding.listItemEntrySummary.setText(R.string.runcommand_notreachable)
binding.listItemEntrySummary.visibility = View.VISIBLE
}
}
}

View File

@ -45,7 +45,7 @@ import org.kde.kdeconnect_tp.databinding.PairingExplanationTextNoWifiBinding
/** /**
* The view that the user will see when there are no devices paired, or when you choose "add a new device" from the sidebar. * The view that the user will see when there are no devices paired, or when you choose "add a new device" from the sidebar.
*/ */
class PairingFragment : BaseFragment<DevicesListBinding>(), PairingDeviceItem.Callback { class PairingFragment : BaseFragment<DevicesListBinding>() {
private var _textBinding: PairingExplanationTextBinding? = null private var _textBinding: PairingExplanationTextBinding? = null
private var _duplicateNamesBinding: PairingExplanationDuplicateNamesBinding? = null private var _duplicateNamesBinding: PairingExplanationDuplicateNamesBinding? = null
@ -212,7 +212,7 @@ class PairingFragment : BaseFragment<DevicesListBinding>(), PairingDeviceItem.Ca
val devices: Collection<Device> = KdeConnect.getInstance().devices.values val devices: Collection<Device> = KdeConnect.getInstance().devices.values
for (device in devices) { for (device in devices) {
if (device.isReachable && device.isPaired) { if (device.isReachable && device.isPaired) {
items.add(PairingDeviceItem(device, this@PairingFragment)) items.add(PairingDeviceItem(device, ::deviceClicked))
connectedSection.isEmpty = false connectedSection.isEmpty = false
} }
} }
@ -224,7 +224,7 @@ class PairingFragment : BaseFragment<DevicesListBinding>(), PairingDeviceItem.Ca
items.add(availableSection) items.add(availableSection)
for (device in devices) { for (device in devices) {
if (device.isReachable && !device.isPaired) { if (device.isReachable && !device.isPaired) {
items.add(PairingDeviceItem(device, this@PairingFragment)) items.add(PairingDeviceItem(device, ::deviceClicked))
availableSection.isEmpty = false availableSection.isEmpty = false
} }
} }
@ -236,7 +236,7 @@ class PairingFragment : BaseFragment<DevicesListBinding>(), PairingDeviceItem.Ca
items.add(rememberedSection) items.add(rememberedSection)
for (device in devices) { for (device in devices) {
if (!device.isReachable && device.isPaired) { if (!device.isReachable && device.isPaired) {
items.add(PairingDeviceItem(device, this@PairingFragment)) items.add(PairingDeviceItem(device, ::deviceClicked))
rememberedSection.isEmpty = false rememberedSection.isEmpty = false
} }
} }
@ -310,7 +310,7 @@ class PairingFragment : BaseFragment<DevicesListBinding>(), PairingDeviceItem.Ca
super.onStop() super.onStop()
} }
override fun pairingClicked(device: Device) { fun deviceClicked(device: Device) {
mainActivity.onDeviceSelected(device.deviceId, !device.isPaired || !device.isReachable) mainActivity.onDeviceSelected(device.deviceId, !device.isPaired || !device.isReachable)
} }