简体   繁体   English

如何在 MVVM 中为 Repository 提供 Drawable,干净的架构

[英]How to Providing Drawable for Repository in MVVM, Clean Architecture

i am trying to code with SOLID, and MVVM architecture.我正在尝试使用 SOLID 和 MVVM 架构进行编码。 But i am confused how to provide drawable to Repository in MVVM但我很困惑如何在 MVVM 中向 Repository 提供 drawable

interface IStringProvider {
   val mengenaliPotensiDiri: String
   val eventSpesial: String
   val pentingMengenaliDiri: String
   val mengenaliAspekDiri: String
   val mulaiRencanaKarier: String
}

class AssessmentListRepository(private val stringProvider: IStringProvider) {
   fun getAssessmentTopicMengenaliDiri(): List<AssessmentProgressItem> {
      return listOf(
         AssessmentProgressItem(
            stringProvider.kerangkaPerencanaanKuliahDanKarier,
            R.drawable.ic_home_video_open, //How should i provide it?
            R.drawable.ic_home_video_lock,
            R.drawable.ic_home_video_open,
            { AssessmentProgressItem.PROGRESS_STATUS_OPEN },
            true,
            0,
            {}
         ),
         AssessmentProgressItem(
            stringProvider.caraMengenaliDiri,
            R.drawable.ic_home_video_open,
            R.drawable.ic_home_video_lock,
            R.drawable.ic_home_video_open,
            { AssessmentProgressItem.PROGRESS_STATUS_OPEN },
            true,
            0,
            {}
         )
      )
   }
}
  1. So, how should i provide drawable to my AssessmentListRepository?那么,我应该如何向我的 AssessmentListRepository 提供 drawable 呢? shall i providing Drawable class, or i just providing integer IDs?我应该提供 Drawable class,还是只提供 integer ID?

  2. Is Drawable an AndroidComponent? Drawable 是 Android 组件吗?

interesting question.有趣的问题。 Hope I understood you correctly, because I don't understand why your repository needs drawables?希望我理解正确,因为我不明白为什么您的存储库需要可绘制对象? What are going to do with those drawables?这些drawables要做什么? Return they back to VM?将它们返回给 VM?

Actually, I'm not sure that repository has to provide Drawable in general.实际上,我不确定存储库是否必须提供 Drawable。 And R.java is definitely Android specific class, so we can say that your Repository class depends on Android specific class. And R.java is definitely Android specific class, so we can say that your Repository class depends on Android specific class. I think better way do something like this:我认为更好的方法是这样做:

  1. Create independent class which will describe data you needs for your screen.创建独立的 class 将描述您的屏幕所需的数据。 Instead of drawables you can use enum/sealed classes to describe some data/states.您可以使用枚举/密封类来描述一些数据/状态,而不是可绘制对象。
  2. Repository will return that class (without drawables or similar resources)存储库将返回 class (没有可绘制或类似资源)
  3. ViewModel will ask data from repository and analyze that data. ViewModel 将从存储库中询问数据并分析该数据。
  4. ViewModel will change UI-related properties based on data from repository. ViewModel 将根据存储库中的数据更改与 UI 相关的属性。
  5. As View is observer on UI-related properties of ViewModel, View will change UI-state automatically.由于 View 是 ViewModel 的 UI 相关属性的观察者,因此 View 会自动更改 UI 状态。 Drawable and other resources should be used on this stage.在这个阶段应该使用 Drawable 和其他资源。

Cheers干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在没有 DI 并遵循干净架构的情况下在 MVVM 的存储库中传递上下文? - How to pass context in Repository in MVVM without DI and following the clean architecture? 如何在 android 中为 MVVM Clean 架构创建用例 - How to create UseCase for MVVM Clean architecture in android 干净的架构存储库 - Clean Architecture Repository 带有实时数据的 Android MVVM 清洁架构 - Android MVVM clean architecture with livedata Android MVVM 应用架构:如何将 FirebaseFirestore 设置为存储库 - Android MVVM App Architecture: How to set up FirebaseFirestore as Repository 如何测试在Android MVP Clean Architecture中注入存储库中的sharedpreference - How to test sharedpreference that inject in repository in Android MVP Clean Architecture 功能与 MVVM 相似的 2 个片段的干净架构? - Clean architecture for 2 fragments that are similar in function with MVVM? 在 Clean architecture MVVM 中,将对象转换为适配器? - In Clean architecture MVVM where transform objects to adapter? Android 中具有干净架构的 mvvm 和没有干净架构的 mvvm 有什么区别? - What is difference between mvvm with clean architecture and mvvm without clean architecture in Android? 存储库 MVVM 架构中的堆栈溢出错误 - Stack Overflow error in Repository MVVM architecture
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM