2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-31 14:15:14 +00:00

Replace StubTextPlugin.kt with PluginItem.kt

This commit is contained in:
Dmitry Yudin
2023-04-12 01:12:50 +02:00
parent b7b9a3ad66
commit 851fb58712
11 changed files with 60 additions and 124 deletions

View File

@@ -4,7 +4,7 @@ import com.android.build.gradle.api.ApplicationVariant
import com.github.jk1.license.render.TextReportRenderer
buildscript {
ext.kotlin_version = '1.8.10'
ext.kotlin_version = '1.8.0'
dependencies {
classpath 'com.android.tools.build:gradle:7.4.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

View File

@@ -2,10 +2,8 @@
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:maxWidth="400dp"
android:layout_height="wrap_content"
android:background="?selectableItemBackground"
android:paddingHorizontal="@dimen/activity_horizontal_margin"
android:paddingVertical="@dimen/activity_vertical_margin"
android:padding="@dimen/view_default_padding"
tools:background="@android:color/darker_gray"
tools:text="@tools:sample/lorem"/>

View File

@@ -6,7 +6,7 @@
android:layout_height="wrap_content"
android:layout_margin="4dp"
style="@style/KdeConnectCardStyle.Filled"
app:contentPadding="12dp"
app:contentPadding="@dimen/view_default_padding"
tools:layout_width="240dp">
<LinearLayout

View File

@@ -2,6 +2,7 @@
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="view_default_padding">16dp</dimen>
<dimen name="key_height">48dip</dimen>
<dimen name="fab_margin">16dp</dimen>
<dimen name="fab_elevation">6dp</dimen>

View File

@@ -1,15 +0,0 @@
package org.kde.kdeconnect.Plugins
class StubTextPlugin(private val description: String) : Plugin() {
override fun getDisplayName() = description
override fun getDescription() = description
override fun getSupportedPacketTypes(): Array<String> {
throw UnsupportedOperationException("StubTextPlugin is used only with displayName and description")
}
override fun getOutgoingPacketTypes(): Array<String> {
throw UnsupportedOperationException("StubTextPlugin is used only with displayName and description")
}
}

View File

@@ -25,8 +25,8 @@ import org.kde.kdeconnect.Device.PluginsChangedListener
import org.kde.kdeconnect.Helpers.SecurityHelpers.SslHelper
import org.kde.kdeconnect.Plugins.BatteryPlugin.BatteryPlugin
import org.kde.kdeconnect.Plugins.Plugin
import org.kde.kdeconnect.Plugins.StubTextPlugin
import org.kde.kdeconnect.UserInterface.List.PluginAdapter
import org.kde.kdeconnect.UserInterface.List.PluginItem
import org.kde.kdeconnect_tp.R
import org.kde.kdeconnect_tp.databinding.ActivityDeviceBinding
import org.kde.kdeconnect_tp.databinding.ViewPairErrorBinding
@@ -45,8 +45,8 @@ class DeviceFragment : Fragment() {
private val mActivity: MainActivity? by lazy { activity as MainActivity? }
//TODO use LinkedHashMap and delete irrelevant records when plugins changed
private val pluginListItems: ArrayList<Pair<Plugin, (() -> Unit)?>> = ArrayList()
private val permissionListItems: ArrayList<Pair<Plugin, (() -> Unit)?>> = ArrayList()
private val pluginListItems: ArrayList<PluginItem> = ArrayList()
private val permissionListItems: ArrayList<PluginItem> = ArrayList()
/**
* Top-level ViewBinding for this fragment.
@@ -259,9 +259,13 @@ class DeviceFragment : Fragment() {
if (paired && !reachable) {
requireErrorBinding().errorMessageContainer.visibility = View.VISIBLE
requireErrorBinding().notReachableMessage.visibility = View.VISIBLE
requireDeviceBinding().buttonsList.visibility = View.GONE
requireDeviceBinding().pluginsList.visibility = View.GONE
} else {
requireErrorBinding().errorMessageContainer.visibility = View.GONE
requireErrorBinding().notReachableMessage.visibility = View.GONE
requireDeviceBinding().buttonsList.visibility = View.VISIBLE
requireDeviceBinding().pluginsList.visibility = View.VISIBLE
}
try {
if (paired && reachable) {
@@ -275,7 +279,9 @@ class DeviceFragment : Fragment() {
//Fill enabled plugins ArrayList
for (p in plugins) {
if (!p.hasMainActivity(context) || p.displayInContextMenu()) continue
pluginListItems.add(p to { p.startMainActivity(mActivity) })
pluginListItems.add(
PluginItem(requireContext(), p, { p.startMainActivity(mActivity) })
)
}
//Fill permissionListItems with permissions plugins
@@ -358,10 +364,18 @@ class DeviceFragment : Fragment() {
) {
if (plugins.isEmpty()) return
val device = device ?: return
permissionListItems.add(StubTextPlugin(requireContext().getString(headerText)) to null)
permissionListItems.add(
PluginItem(
context = requireContext(),
header = requireContext().getString(headerText),
textStyleRes = R.style.TextAppearance_Material3_BodyMedium,
)
)
for (plugin in plugins.values) {
if (device.isPluginEnabled(plugin.pluginKey)) {
permissionListItems.add(plugin to { action(plugin) })
permissionListItems.add(
PluginItem(requireContext(), plugin, action, R.style.TextAppearance_Material3_LabelLarge)
)
}
}
}

View File

@@ -1,19 +0,0 @@
/*
* SPDX-FileCopyrightText: 2018 Nicolas Fella <nicolas.fella@gmx.de>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
package org.kde.kdeconnect.UserInterface.List;
import org.kde.kdeconnect.Plugins.Plugin;
public class FailedPluginListItem extends SmallEntryItem {
public interface Action {
void action(Plugin plugin);
}
public FailedPluginListItem(Plugin plugin, Action action) {
super(plugin.getDisplayName(), (view) -> action.action(plugin));
}
}

View File

@@ -8,8 +8,6 @@ import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import org.kde.kdeconnect.Plugins.Plugin
import org.kde.kdeconnect.Plugins.StubTextPlugin
import org.kde.kdeconnect_tp.R
/**
@@ -20,30 +18,24 @@ import org.kde.kdeconnect_tp.R
* Any other TextView layout
*/
class PluginAdapter(
private val pluginList: ArrayList<Pair<Plugin, (() -> Unit)?>>,
private val layout: Int,
private val pluginList: ArrayList<PluginItem>,
private val layoutRes: Int,
) : RecyclerView.Adapter<PluginAdapter.PluginViewHolder>() {
override fun onCreateViewHolder(viewGroup: ViewGroup, type: Int) =
PluginViewHolder(LayoutInflater.from(viewGroup.context).inflate(layout, viewGroup, false))
PluginViewHolder(LayoutInflater.from(viewGroup.context).inflate(layoutRes, viewGroup, false))
override fun getItemCount() = pluginList.size
@TargetApi(Build.VERSION_CODES.M)
override fun onBindViewHolder(holder: PluginViewHolder, position: Int) {
pluginList[position].let { (plugin, action) ->
holder.pluginTitle.text = plugin.displayName
pluginList[position].let { plugin ->
holder.pluginTitle.text = plugin.header
holder.pluginIcon?.setImageDrawable(plugin.icon)
//Set regular text for unclickable StubTextPlugin and bold for supposedly clickable TextView items
when {
plugin is StubTextPlugin ->
holder.pluginTitle.setTextAppearance(R.style.TextAppearance_Material3_BodyMedium)
holder.itemView is TextView ->
holder.pluginTitle.setTextAppearance(R.style.TextAppearance_Material3_LabelLarge)
}
plugin.textStyleRes?.let { holder.pluginTitle.setTextAppearance(it) }
action?.let { holder.itemView.setOnClickListener { action.invoke() } }
plugin.action?.let { action -> holder.itemView.setOnClickListener { action.invoke() } }
}
}

View File

@@ -1,31 +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.Plugins.Plugin;
public class PluginItem extends EntryItemWithIcon {
private final View.OnClickListener clickListener;
public PluginItem(Plugin p, View.OnClickListener clickListener) {
super(p.getActionName(), p.getIcon());
this.clickListener = clickListener;
}
@NonNull
@Override
public View inflateView(@NonNull LayoutInflater layoutInflater) {
final View root = super.inflateView(layoutInflater);
root.setOnClickListener(clickListener);
return root;
}
}

View File

@@ -0,0 +1,29 @@
package org.kde.kdeconnect.UserInterface.List
import android.content.Context
import android.graphics.drawable.Drawable
import org.kde.kdeconnect.Plugins.Plugin
class PluginItem(
val context: Context,
val header: String,
val textStyleRes: Int? = null,
) {
var action: (() -> Unit)? = null
var icon: Drawable? = null
constructor(
context: Context,
plugin: Plugin,
action: (Plugin) -> Unit,
textStyleRes: Int? = null,
) : this(
context = context,
header = plugin.displayName,
textStyleRes = textStyleRes,
) {
this.action = { action(plugin) }
this.icon = plugin.icon
}
}

View File

@@ -1,33 +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 android.widget.TextView;
import androidx.annotation.NonNull;
import org.kde.kdeconnect_tp.databinding.ListItemPluginHeaderBinding;
public class PluginListHeaderItem implements ListAdapter.Item {
private final int text;
public PluginListHeaderItem(int text) {
this.text = text;
}
@NonNull
@Override
public View inflateView(@NonNull LayoutInflater layoutInflater) {
TextView textView = ListItemPluginHeaderBinding.inflate(layoutInflater).getRoot();
textView.setText(text);
textView.setOnClickListener(null);
textView.setOnLongClickListener(null);
return textView;
}
}