From 1471042fee6121ef471f0fde35fb6bbf7a932285 Mon Sep 17 00:00:00 2001 From: Conny Duck Date: Sun, 22 Dec 2019 11:42:13 +0100 Subject: [PATCH] convert ScheduledTootAdapter to Kotlin --- .../tusky/ScheduledTootActivity.kt | 3 +- .../tusky/adapter/ScheduledTootAdapter.java | 125 ------------------ .../tusky/adapter/ScheduledTootAdapter.kt | 87 ++++++++++++ 3 files changed, 89 insertions(+), 126 deletions(-) delete mode 100644 app/src/main/java/com/keylesspalace/tusky/adapter/ScheduledTootAdapter.java create mode 100644 app/src/main/java/com/keylesspalace/tusky/adapter/ScheduledTootAdapter.kt diff --git a/app/src/main/java/com/keylesspalace/tusky/ScheduledTootActivity.kt b/app/src/main/java/com/keylesspalace/tusky/ScheduledTootActivity.kt index 5467a3b87..3399eea0b 100644 --- a/app/src/main/java/com/keylesspalace/tusky/ScheduledTootActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/ScheduledTootActivity.kt @@ -9,6 +9,7 @@ import androidx.appcompat.widget.Toolbar import androidx.lifecycle.Lifecycle import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager +import com.keylesspalace.tusky.adapter.ScheduledTootAction import com.keylesspalace.tusky.adapter.ScheduledTootAdapter import com.keylesspalace.tusky.appstore.EventHub import com.keylesspalace.tusky.appstore.StatusScheduledEvent @@ -29,7 +30,7 @@ import retrofit2.Response import javax.inject.Inject -class ScheduledTootActivity : BaseActivity(), ScheduledTootAdapter.ScheduledTootAction, Injectable { +class ScheduledTootActivity : BaseActivity(), ScheduledTootAction, Injectable { companion object { @JvmStatic diff --git a/app/src/main/java/com/keylesspalace/tusky/adapter/ScheduledTootAdapter.java b/app/src/main/java/com/keylesspalace/tusky/adapter/ScheduledTootAdapter.java deleted file mode 100644 index 6698dd371..000000000 --- a/app/src/main/java/com/keylesspalace/tusky/adapter/ScheduledTootAdapter.java +++ /dev/null @@ -1,125 +0,0 @@ -/* Copyright 2019 kyori19 - * - * This file is a part of Tusky. - * - * This program is free software; you can redistribute it and/or modify it under the terms of the - * GNU General Public License as published by the Free Software Foundation; either version 3 of the - * License, or (at your option) any later version. - * - * Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even - * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General - * Public License for more details. - * - * You should have received a copy of the GNU General Public License along with Tusky; if not, - * see . */ - -package com.keylesspalace.tusky.adapter; - -import android.content.Context; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ImageButton; -import android.widget.TextView; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.recyclerview.widget.RecyclerView; - -import com.keylesspalace.tusky.R; -import com.keylesspalace.tusky.entity.ScheduledStatus; - -import java.util.ArrayList; -import java.util.List; - -public class ScheduledTootAdapter extends RecyclerView.Adapter { - private List list; - private ScheduledTootAction handler; - - public ScheduledTootAdapter(Context context) { - super(); - list = new ArrayList<>(); - handler = (ScheduledTootAction) context; - } - - @NonNull - @Override - public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { - View view = LayoutInflater.from(parent.getContext()) - .inflate(R.layout.item_scheduled_toot, parent, false); - return new TootViewHolder(view); - } - - @Override - public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) { - TootViewHolder holder = (TootViewHolder) viewHolder; - holder.bind(getItem(position)); - } - - @Override - public int getItemCount() { - return list.size(); - } - - public void setItems(List newToot) { - list = new ArrayList<>(); - list.addAll(newToot); - } - - @Nullable - public ScheduledStatus removeItem(int position) { - if (position < 0 || position >= list.size()) { - return null; - } - ScheduledStatus toot = list.remove(position); - notifyItemRemoved(position); - return toot; - } - - private ScheduledStatus getItem(int position) { - if (position >= 0 && position < list.size()) { - return list.get(position); - } - return null; - } - - public interface ScheduledTootAction { - void edit(int position, ScheduledStatus item); - - void delete(int position, ScheduledStatus item); - } - - private class TootViewHolder extends RecyclerView.ViewHolder { - View view; - TextView text; - ImageButton edit; - ImageButton delete; - - TootViewHolder(View view) { - super(view); - this.view = view; - this.text = view.findViewById(R.id.text); - this.edit = view.findViewById(R.id.edit); - this.delete = view.findViewById(R.id.delete); - } - - void bind(final ScheduledStatus item) { - edit.setEnabled(true); - delete.setEnabled(true); - - if (item != null) { - text.setText(item.getParams().getText()); - - edit.setOnClickListener(v -> { - v.setEnabled(false); - handler.edit(getAdapterPosition(), item); - }); - - delete.setOnClickListener(v -> { - v.setEnabled(false); - handler.delete(getAdapterPosition(), item); - }); - } - } - } -} diff --git a/app/src/main/java/com/keylesspalace/tusky/adapter/ScheduledTootAdapter.kt b/app/src/main/java/com/keylesspalace/tusky/adapter/ScheduledTootAdapter.kt new file mode 100644 index 000000000..85e81d000 --- /dev/null +++ b/app/src/main/java/com/keylesspalace/tusky/adapter/ScheduledTootAdapter.kt @@ -0,0 +1,87 @@ +/* Copyright 2019 Tusky Contributors + * + * This file is a part of Tusky. + * + * This program is free software; you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation; either version 3 of the + * License, or (at your option) any later version. + * + * Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even + * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. + * + * You should have received a copy of the GNU General Public License along with Tusky; if not, + * see . */ + +package com.keylesspalace.tusky.adapter + +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.ImageButton +import android.widget.TextView +import androidx.recyclerview.widget.RecyclerView +import com.keylesspalace.tusky.R +import com.keylesspalace.tusky.entity.ScheduledStatus + +interface ScheduledTootAction { + fun edit(position: Int, item: ScheduledStatus?) + fun delete(position: Int, item: ScheduledStatus?) +} + +class ScheduledTootAdapter( + val listener: ScheduledTootAction +) : RecyclerView.Adapter() { + + private var items: MutableList = mutableListOf() + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TootViewHolder { + val view = LayoutInflater.from(parent.context) + .inflate(R.layout.item_scheduled_toot, parent, false) + return TootViewHolder(view) + } + + override fun onBindViewHolder(viewHolder: TootViewHolder, position: Int) { + viewHolder.bind(items[position]) + } + + override fun getItemCount() = items.size + + fun setItems(newItems: List) { + items = newItems.toMutableList() + notifyDataSetChanged() + } + + fun removeItem(position: Int): ScheduledStatus? { + if (position < 0 || position >= items.size) { + return null + } + val toot = items.removeAt(position) + notifyItemRemoved(position) + return toot + } + + inner class TootViewHolder(view: View) : RecyclerView.ViewHolder(view) { + + private val text: TextView = view.findViewById(R.id.text) + private val edit: ImageButton = view.findViewById(R.id.edit) + private val delete: ImageButton = view.findViewById(R.id.delete) + + fun bind(item: ScheduledStatus) { + edit.isEnabled = true + delete.isEnabled = true + text.text = item.params.text + edit.setOnClickListener { v: View -> + v.isEnabled = false + listener.edit(adapterPosition, item) + } + delete.setOnClickListener { v: View -> + v.isEnabled = false + listener.delete(adapterPosition, item) + } + + } + + } + +} \ No newline at end of file