简体   繁体   English

MVVM 架构 Android

[英]MVVM Architecture Android

I have one Activity and i have created one View-model for it.我有一个 Activity 并为它创建了一个视图模型。 I have created different classes like UiUtil( show, hide view, hide key board ), Network layer, Data Base layer, AppUtil( for common functionality like Collection check, String validation, Date Conversion etc)我创建了不同的类,如 UiUtil(显示、隐藏视图、隐藏键盘)、网络层、数据库层、AppUtil(用于集合检查、字符串验证、日期转换等常见功能)

My question is, In MVVM design pattern is Activity can use these utility classes directly or it needs to use these classes via View-model, if it via view model then in the view-model i have to write a method that just call utility classes method.我的问题是,在 MVVM 设计模式中,Activity 可以直接使用这些实用程序类,或者它需要通过视图模型使用这些类,如果它通过视图 model 那么在视图模型中我必须编写一个只调用实用程序类的方法方法。 like below TimeDateManager is utility class used in view-model class HomeViewModel: BaseViewModel() { fun prepareTimeAmPmToDisplay(context: Context, alarm: Alarm): String { return TimeDateManager.prepareTimeAmPmToDisplay(context, alarm) } }如下所示 TimeDateManager 是实用程序 class 用于视图模型class HomeViewModel: BaseViewModel() { fun prepareTimeAmPmToDisplay(context: Context, alarm: Alarm): String { return TimeDateManager.prepareTimeAmPmToDisplay(context, alarm) } }

Architectures are not obligatory, they are recommendational, thus you can change their usage in quite wide range.架构不是强制性的,它们是推荐性的,因此您可以在相当广泛的范围内更改它们的使用。 The only stopper should be a common sense(if it is present of course).唯一的障碍应该是常识(当然,如果它存在的话)。

In this particular case the usage of utility class inside an Activity maybe ok, based on your ViewModel construction and its way of communication with View (read Activity ).在这种特殊情况下,根据您的ViewModel构造及其与View的通信方式(读取Activity ),在Activity中使用实用程序 class 可能没问题。

For example if you have some LiveData that sends some kind of event(for ex. data loaded from backend or alarm trigger) inside your ViewModel and your View listens to it, I think it is ok to use util classes inside an Observer in Activity .例如,如果您有一些LiveDataViewModel中发送某种事件(例如从后端加载的数据或警报触发器)并且您的View监听它,我认为可以在ActivityObserver中使用 util 类。 Especially if this utils method doesn't depend on any ViewModel or Repository data.特别是如果此 utils 方法不依赖于任何ViewModelRepository数据。 The direct utils usage in Activity is not limited by this usecase, though - there are plenty of others.不过, Activity中的直接 utils 使用不受此用例的限制 - 还有很多其他用例。

I understand that this may be an unpopular opinion in modern time of "clean approach" but I believe that this "clean approach" sometimes complicates stuff where it shouldn't, thus if mixing things a bit does not brake overall architecture but rather makes some thing more readable and easy to maintain - I would go for it.我知道这在现代“干净的方法”可能是一个不受欢迎的观点,但我相信这种“干净的方法”有时会使不应该的东西复杂化,因此如果混合一些东西不会破坏整体架构,而是让一些更具可读性和易于维护的东西 - 我会为它 go 。

Hope it helps.希望能帮助到你。

My approach toward MVVM is simple, ViewModel is responsible for business logic, dealing with repositories (Network, Database, etc.) and all of the non-UI codes preparing the required data for UI, just like the documentation :我处理 MVVM 的方法很简单,ViewModel 负责业务逻辑,处理存储库(网络、数据库等)和所有为 UI 准备所需数据的非 UI 代码,就像文档一样:

A ViewModel object provides the data for a specific UI component, such as a fragment or activity, and contains data-handling business logic to communicate with the model. ViewModel object 为特定的 UI 组件(例如片段或活动)提供数据,并包含与 model 通信的数据处理业务逻辑。 For example, the ViewModel can call other components to load the data, and it can forward user requests to modify the data.例如,ViewModel 可以调用其他组件来加载数据,它可以转发用户修改数据的请求。 The ViewModel doesn't know about UI components, so it isn't affected by configuration changes, such as recreating an activity when rotating the device. ViewModel 不了解 UI 组件,因此它不受配置更改的影响,例如在旋转设备时重新创建活动。

On the other hand, ViewModels should not store a context (ApplicationContext is exceptional) and it's preferred that they do not use android APIs at all, so they become more testable (especially in the case on pure unit tests).另一方面,ViewModel 不应该存储上下文(ApplicationContext 是例外),并且它们最好根本不使用 android API,因此它们变得更加可测试(尤其是在纯单元测试的情况下)。

Also we are recommended to make use of LiveData in ViewModels and the UI has to observe the LiveData.此外,我们建议在 ViewModels 中使用 LiveData,UI 必须观察 LiveData。 For example, in onCreate of your Activity, you will call loadMainContent() method from VM, it calls getMainContent(page=1) from repository, and the repository will decide to load data from DB or network, and the result will be set on a LiveData were the View is listening for changes.例如,在你的Activity的onCreate中,你会从VM调用loadMainContent()方法,它从repository调用getMainContent(page=1) ,repository会决定从DB或者network加载数据,结果会设置为on一个 LiveData 是 View 正在监听变化。

TL;DR TL;博士

Sometimes it's even better to call these utilities from View rather than the VM.有时从 View 而不是 VM 调用这些实用程序会更好。 I'm pretty sure about your UiUtil also I think TimeDateManager is more view related rather than logic related.我很确定你的UiUtil也我认为TimeDateManager更多的是与视图相关而不是与逻辑相关。 In addition, Network and DB layers are more efficient if called through a repository (which is responsible for caching, etc.) and VM can use that repo.此外,如果通过存储库(负责缓存等)调用网络和数据库层,则效率更高,并且 VM 可以使用该存储库。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM