在MVVM + Repostiory + LiveData设置中获取HashMap <Model1,List <Model2 >>?

时间:2019-09-14 16:21:47

标签: kotlin hashmap repository-pattern android-livedata android-mvvm

因此,我正在开发这个愚蠢的小应用程序,用于练习MVVM和存储库模式。目前,我有两个模型课程。它们是CategorySubCategory,我为它们定义了以下数据类:

@Entity(tableName = "categories")
data class Category(
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    val id: Int,
    @ColumnInfo(name = "name")
    val name: String
) {
}

还有

/**
 * One to many Relationship from Category to SubCategory
 */
@Entity(
    tableName = "sub_categories", foreignKeys = arrayOf(
        ForeignKey(
            entity = Category::class,
            parentColumns = arrayOf("id"),
            childColumns = arrayOf("category_id")
        )
    )
)
data class SubCategory(
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    val id: Int,
    @ColumnInfo(name = "name")
    val name: String,
    @ColumnInfo(name = "category_id")
    val categoryId: Int
) {
}

如您所见,我已经对资源进行了建模,以便我们需要传递categoryId才能获取与Category相关的SubCategories。

现在,我对这种MVVM,LiveData和存储库模式还很陌生。

我的问题是我正在使用ExpandableListView填充类别下的SubCategories,而Adapter则需要HashMap<Category, List<SubCategory>才能显示可扩展列表视图。

所以我的问题是如何使用HashMap<Category, List<SubCategory>的方法从数据库中获取db->dao->repository->viewmodel,以及adpater到哪里去。

我想创建一个像CategorySubCategoryRespository这样的独立存储库,通过该存储库,我可以执行以下操作对您没有帮助?

class CategorySubCategoryRepository(
        private val categoryDao: CategoryDao,
        private val subCategoryDao: SubCategoryDao
    ) {
    val allCategoriesSubCategories: LiveData<HashMap<Category, List<SubCategory>>>
        get() {
            val hashMap: HashMap<Category, List<SubCategory>> = hashMapOf()
            for (category in categoryDao.getList()) {
                hashMap[category] = subCategoryDao.getSubCategoriesListForCategory(category.id)
            }
            return hashMap
        }
    }
}

PS:我想我想尽可能使用LiveData

1 个答案:

答案 0 :(得分:0)

所以我最终要做的是,在CategorySubcategoryRepositoryCategoryDao中,我的SubcategoryDao构造了哈希图,如下所示:

class CategorySubCategoryRepository(
    private val categoryDao: CategoryDao,
    private val subCategoryDao: SubCategoryDao
) {

    fun getHashMap(): LiveData<HashMap<Category, List<SubCategory>>> {
        val data = MutableLiveData<HashMap<Category, List<SubCategory>>>()
        val hashMap: HashMap<Category, List<SubCategory>> = hashMapOf()
        Executors.newSingleThreadExecutor().execute {
            for (category in categoryDao.getList()) {
                hashMap[category] = subCategoryDao.getSubCategoriesListForCategory(category.id)
            }
        }
        data.value = hashMap
        return data
    }

}

然后我在视图模型的init{}块中使用了此代码,

hashMap = categorySubCategoryRepository.getHashMap()

然后我在片段的onCreateView中将其观察为:

        myViewModel.hashMap.observe(this, Observer {
            adapter.setCategoryList(it.keys.toList())
            adapter.setCategorySubCategoriesMap(it)
            elv_categories.setAdapter(adapter) 

            adapter.notifyDataSetChanged()
        })

如果这不是正确的选择,请发表评论。我这样做只是为了提高自己的技能,很想听听是否有更好的方法来解决问题,或者我的方法完全荒谬。

编辑:

根据@SanlokLee的评论。 getHashMap函数已更改为:

fun getHashMap(): LiveData<HashMap<Category, List<SubCategory>>> {
        val data = MutableLiveData<HashMap<Category, List<SubCategory>>>()
        Executors.newSingleThreadExecutor().execute {
            val hashMap: HashMap<Category, List<SubCategory>> = hashMapOf()
            for (category in categoryDao.getList()) {
                hashMap[category] = subCategoryDao.getSubCategoriesListForCategory(category.id)
            }
            data.postValue(hashMap)
        }
        return data
    }