Commit 53ae3ce5 by Paktalin

Words can be deleted by long press

parent 0b4e4889
......@@ -9,9 +9,16 @@ import android.view.ViewGroup
import android.widget.TextView
import com.paktalin.vocabularynotebook.activities.WordItemInfoActivity
class VocabularyAdapter(private val wordItems: List<WordItem>,
class VocabularyAdapter(private val wordItems: MutableList<WordItem>,
private val context: Context) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() {
private lateinit var recyclerView: RecyclerView
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView)
this.recyclerView = recyclerView
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.word_item, parent, false)
......@@ -23,6 +30,7 @@ class VocabularyAdapter(private val wordItems: List<WordItem>,
holder.tvWord.text = wordItem.pojo.word
holder.tvTranslation.text = wordItem.pojo.translation
holder.itemView.setOnClickListener { openWordItemInfo(wordItem) }
holder.itemView.setOnLongClickListener { deleteWordItem(position);true }
}
override fun getItemCount(): Int {
......@@ -40,5 +48,13 @@ class VocabularyAdapter(private val wordItems: List<WordItem>,
context.startActivity(intentWordItemInfo)
}
private fun deleteWordItem(position: Int) {
wordItems[position].delete()
wordItems.removeAt(position)
recyclerView.removeViewAt(position)
this.notifyItemRemoved(position)
this.notifyItemRangeChanged(position, wordItems.size)
}
companion object { private val TAG = "VN/" + VocabularyAdapter::class.java.simpleName }
}
\ No newline at end of file
package com.paktalin.vocabularynotebook;
import android.support.annotation.NonNull;
import android.util.Log;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;
import java.io.Serializable;
public class WordItem implements Serializable {
private static final String TAG = "VN/" + WordItem.class.getSimpleName();
public static class WordItemPojo implements Serializable{
private String word, translation;
......@@ -30,13 +40,31 @@ public class WordItem implements Serializable {
}
private String id;
private String vocabularyId;
private WordItemPojo pojo;
public WordItem(String word, String translation, String id) {
public WordItem(String word, String translation, String id, String vocabularyId) {
this.pojo = new WordItemPojo(word, translation);
this.vocabularyId = vocabularyId;
this.id = id;
}
public void delete() {
FirebaseFirestore.getInstance().collection("vocabularies").document(vocabularyId)
.collection("words").document(id).delete()
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.i(TAG, "Successfully deleted word with id " + id);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "deleteWordWithId " + id + ":failure", e.fillInStackTrace());
}
});
}
public WordItemPojo getPojo() {
return pojo;
}
......
......@@ -87,8 +87,7 @@ class VocabularyFragment : Fragment() {
for (ref in documents) {
val word = ref.get("word").toString()
val translation = ref.get("translation").toString()
val wordItemId = ref.id
wordItems.add(WordItem(word, translation, wordItemId))
wordItems.add(WordItem(word, translation, ref.id, vocabulary.id))
}
val adapter = VocabularyAdapter(wordItems, activity!!)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment