【问题标题】:How to add data to Firestore in android using MVP Architecture?如何使用 MVP 架构将数据添加到 Android 中的 Firestore?
【发布时间】:2021-06-06 23:15:25
【问题描述】:

所以目前我正在尝试将 MVP 架构用于我在 android 中的最新项目。我使用 firestore 作为其服务器来存储所有数据。问题是当我尝试从包含将数据存储到 Firestore 的逻辑的活动中输入数据时,它可以工作,但是当我使用 MVP Architecture 进行操作时,保存按钮没有将数据存储到 firestore 集合。这怎么可能?因为我在这两种情况下使用的查询是相同的。

这是我的 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)
        }
    }

这是我的 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)
    }
}

这是我的交互者:

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()
        }
    }
}

这是我的合同:

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{
    }
}

【问题讨论】:

  • 在您的演示者中,当您调用mAddInteractor?.Add(title, content, context) 方法时,您确定您的mAddInteractor 不为空并且正在调用Add() 方法吗?
  • @Ryan 如果我使用 toast 检查我的输入,它不为空,但我不知道为什么交互器不工作
  • @Ryan 有什么方法可以检查我是否正确调用它?
  • 很高兴看到你解决了它:)

标签: android kotlin google-cloud-firestore firebase-authentication


【解决方案1】:

我找到了解决方案。基本上,我的 Activity 不知道应该调用哪个交互器,所以我决定重新格式化这个结构,而不是用空值初始化交互器和视图:

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)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    相关资源
    最近更新 更多