convert ScheduledTootAdapter to Kotlin
This commit is contained in:
parent
ce2ee660ae
commit
1471042fee
|
@ -9,6 +9,7 @@ import androidx.appcompat.widget.Toolbar
|
||||||
import androidx.lifecycle.Lifecycle
|
import androidx.lifecycle.Lifecycle
|
||||||
import androidx.recyclerview.widget.DividerItemDecoration
|
import androidx.recyclerview.widget.DividerItemDecoration
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
|
import com.keylesspalace.tusky.adapter.ScheduledTootAction
|
||||||
import com.keylesspalace.tusky.adapter.ScheduledTootAdapter
|
import com.keylesspalace.tusky.adapter.ScheduledTootAdapter
|
||||||
import com.keylesspalace.tusky.appstore.EventHub
|
import com.keylesspalace.tusky.appstore.EventHub
|
||||||
import com.keylesspalace.tusky.appstore.StatusScheduledEvent
|
import com.keylesspalace.tusky.appstore.StatusScheduledEvent
|
||||||
|
@ -29,7 +30,7 @@ import retrofit2.Response
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
|
||||||
class ScheduledTootActivity : BaseActivity(), ScheduledTootAdapter.ScheduledTootAction, Injectable {
|
class ScheduledTootActivity : BaseActivity(), ScheduledTootAction, Injectable {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
|
|
|
@ -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 <http://www.gnu.org/licenses>. */
|
|
||||||
|
|
||||||
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<ScheduledStatus> 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<ScheduledStatus> 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);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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 <http://www.gnu.org/licenses>. */
|
||||||
|
|
||||||
|
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<ScheduledTootAdapter.TootViewHolder>() {
|
||||||
|
|
||||||
|
private var items: MutableList<ScheduledStatus> = 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<ScheduledStatus>) {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue