Commit 9b0bdf06 by Paktalin

Moved WordItem from java to kotlin

parent 53ae3ce5
...@@ -27,8 +27,8 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, ...@@ -27,8 +27,8 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>,
override fun onBindViewHolder(holder: ViewHolder, position: Int) { override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val wordItem = wordItems[position] val wordItem = wordItems[position]
holder.tvWord.text = wordItem.pojo.word holder.tvWord.text = wordItem.pojo!!.word
holder.tvTranslation.text = wordItem.pojo.translation holder.tvTranslation.text = wordItem.pojo!!.translation
holder.itemView.setOnClickListener { openWordItemInfo(wordItem) } holder.itemView.setOnClickListener { openWordItemInfo(wordItem) }
holder.itemView.setOnLongClickListener { deleteWordItem(position);true } holder.itemView.setOnLongClickListener { deleteWordItem(position);true }
} }
......
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;
public WordItemPojo(String word, String translation) {
this.word = word;
this.translation = translation;
}
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public String getTranslation() {
return translation;
}
public void setTranslation(String translation) {
this.translation = translation;
}
}
private String id;
private String vocabularyId;
private WordItemPojo pojo;
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;
}
public void setPojo(WordItemPojo pojo) {
this.pojo = pojo;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
package com.paktalin.vocabularynotebook
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
class WordItem(
word: String,
translation: String,
var id: String?,
private val vocabularyId: String) : Serializable {
var pojo: WordItemPojo? = null
class WordItemPojo(var word: String?,
var translation: String?) : Serializable
init {
this.pojo = WordItemPojo(word, translation)
}
fun delete() {
FirebaseFirestore.getInstance().collection("vocabularies").document(vocabularyId)
.collection("words").document(id!!).delete()
.addOnSuccessListener { Log.i(TAG, "Successfully deleted word with id $id") }
.addOnFailureListener { e -> Log.w(TAG, "deleteWordWithId $id:failure", e.fillInStackTrace()) }
}
companion object {
private val TAG = "VN/" + WordItem::class.java.simpleName
}
}
...@@ -19,8 +19,8 @@ class WordItemInfoActivity: AppCompatActivity() { ...@@ -19,8 +19,8 @@ class WordItemInfoActivity: AppCompatActivity() {
} }
private fun updateUi() { private fun updateUi() {
tvWord.text = wordItem.pojo.word tvWord.text = wordItem.pojo!!.word
tvTranslation.text = wordItem.pojo.translation tvTranslation.text = wordItem.pojo!!.translation
} }
companion object { private val TAG = "VN/" + WordItemInfoActivity::class.java.simpleName } companion object { private val TAG = "VN/" + WordItemInfoActivity::class.java.simpleName }
......
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