[英]How to add data to Firestore in android using MVP Architecture?
so currently I'm trying to use MVP Architecture for my latest project in android.所以目前我正在尝试将 MVP 架构用于我在 android 中的最新项目。 I use firestore as its server to store all the data.
我使用 firestore 作为其服务器来存储所有数据。 The problem is when I try to input data from my activity which contains the logic to store the data to firestore, it worked, but when I do it with MVP Architecture, the save button didn't store the data to firestore collection.
问题是,当我尝试从包含将数据存储到 Firestore 的逻辑的活动中输入数据时,它可以工作,但是当我使用 MVP Architecture 进行操作时,保存按钮没有将数据存储到 Firestore 集合中。 How is this possible?
这怎么可能? because the query I use in both cases is the same.
因为我在这两种情况下使用的查询是相同的。
Here's my Activity with MVP Architecture:这是我的 MVP 架构活动:
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId){
R.id.nav_save -> {
val stringTitle = edtTitle.text.toString()
val stringContent = edtContent.text.toString()
if (stringTitle.isEmpty() && stringContent.isEmpty()) {
Toast.makeText(this, "your note shouldn't be empty!", Toast.LENGTH_LONG).show()
return false
}
progressBar.visibility = View.VISIBLE
mPresenter.performAdd(stringTitle, stringContent,this)
return true
}
else -> super.onOptionsItemSelected(item)
}
}
This one is my mPresenter:这是我的 mPresenter:
class AddPresenter: AddContract.Presenter, AddContract.onAddListener {
private val mAddView: AddContract.View? = null
private var mAddInteractor: AddInteractor? = null
override fun performAdd(title: String, content: String, context: Context) {
mAddInteractor?.Add(title, content, context)
}
}
This one is my Interactor:这是我的交互器:
class AddInteractor(onAddListener: AddContract.onAddListener) : AddContract.Interactor {
val firebaseUser = FirebaseAuth.getInstance().currentUser!!
val documentReference: DocumentReference = FirebaseFirestore.getInstance().collection("mainCollection").document(firebaseUser.uid).collection("subCollection").document()
private var mOnAddListener: AddContract.onAddListener
init {
this.mOnAddListener = onAddListener
}
override fun Add(title: String, content: String, context: Context) {
var note: HashMap<String, Any> = HashMap<String, Any>()
note.put("title", title)
note.put("content", content)
documentReference.set(note).addOnSuccessListener {
Toast.makeText(context, "Successfully saved! ", Toast.LENGTH_SHORT).show()
}.addOnFailureListener {
Toast.makeText(context, "Error save, caused by : " + it.toString(), Toast.LENGTH_LONG).show()
}
}
}
This one is my Contract:这是我的合同:
interface AddContract {
interface View{
}
interface Interactor{
fun Add(title: String, content: String, context: Context)
}
interface Presenter{
fun performAdd(title: String, content: String, context: Context)
}
interface onAddListener{
}
}
I found the solution.我找到了解决方案。 Basically, my activity didn't know which interactor it should be called, so instead of initializing the interactor and the view with a null value, I decided to reformat the structure like this:
基本上,我的活动不知道应该调用哪个交互器,因此我决定重新格式化结构,而不是使用 null 值初始化交互器和视图:
class AddPresenter(addView: AddNoteContract.View): AddContract.Presenter, AddContract.onAddListener {
private val mAddView: AddContract.View
private val mAddInteractor: AddInteractor
init {
this.mAddView = addView
mAddInteractor = AddInteractor(this)
}
override fun performAdd(title: String, content: String, context: Context) {
mAddInteractor?.Add(title, content, context)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.