简体   繁体   English

如何使用 MVP 架构将数据添加到 android 中的 Firestore?

[英]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.

相关问题 如何在Android中的MVP架构中的两位演示者之间共享数据? - How to share data between two presenters in MVP architecture in Android? 如何在Android中使用图像时实现适当的MVP架构? - How to implement proper MVP architecture whilst using images in Android? Android MVP架构中如何分离Service和Activity? - How to separate Service and Activity in Android MVP architecture? 具有MVP的Android架构组件 - Android architecture components with MVP Android JAVA中MVP模式中的Firestore数据提取序列 - Firestore data extraction sequence in a MVP pattern in Android JAVA Android MVP和Lifecycle体系结构组件 - Android MVP and Lifecycle architecture component 如何测试在Android MVP Clean Architecture中注入存储库中的sharedpreference - How to test sharedpreference that inject in repository in Android MVP Clean Architecture Android MVP-Architecture如何使用SQLiteHelper在模型中进行数据库调用 - Android MVP-Architecture How to make Database Calls in the Model with SQLiteHelper Android MVP体系结构和领域-如何避免在MVP层之间传递上下文? - Android MVP Architecture and Realm - How to Avoid Passing Context among the MVP layers? Chopper 在 Flutter 中使用 MVP 架构 - Chopper using MVP Architecture in flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM