BaseViewModel.ets 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { LogHelper } from '../log/LogHelper';
  2. /**
  3. * ViewModel 基类
  4. *
  5. * 提供统一的 ViewModel 功能,用于管理页面业务逻辑和状态
  6. *
  7. * HarmonyOS 推荐使用方式:
  8. * - ViewModel 中使用普通属性管理状态
  9. * - 页面中使用 @State viewModel,让 @State 自动响应 ViewModel 属性的变化
  10. * - 不需要使用 StateFlow 或手动 observe
  11. */
  12. /**
  13. * UI 状态基类
  14. *
  15. * 定义通用的 UI 状态,所有 ViewModel 的状态类应继承此类
  16. */
  17. export class UiState {
  18. loading: boolean = false;
  19. error: string | null = null;
  20. constructor(loading: boolean = false, error: string | null = null) {
  21. this.loading = loading;
  22. this.error = error;
  23. }
  24. /**
  25. * 创建加载状态
  26. */
  27. static loading(): UiState {
  28. return new UiState(true, null);
  29. }
  30. /**
  31. * 创建错误状态
  32. */
  33. static error(message: string): UiState {
  34. return new UiState(false, message);
  35. }
  36. /**
  37. * 创建成功状态
  38. */
  39. static success(): UiState {
  40. return new UiState(false, null);
  41. }
  42. }
  43. /**
  44. * ViewModel 基类
  45. *
  46. * 提供统一的 ViewModel 功能,与 Android 版本保持一致
  47. *
  48. * 使用方式:
  49. * ```typescript
  50. * class LoginViewModel extends BaseViewModel {
  51. * @State loginState: LoginState = LoginState.Idle;
  52. *
  53. * async login(mobile: string, password: string) {
  54. * this.loginState = LoginState.Loading;
  55. * try {
  56. * const result = await authRepository.login(mobile, password);
  57. * if (result.isSuccess) {
  58. * this.loginState = LoginState.Success(result.getOrThrow());
  59. * } else {
  60. * this.loginState = LoginState.Error(result.exceptionOrNull()?.message || '登录失败');
  61. * }
  62. * } catch (error) {
  63. * this.loginState = LoginState.Error((error as Error).message);
  64. * }
  65. * }
  66. * }
  67. * ```
  68. */
  69. export abstract class BaseViewModel {
  70. protected readonly tag: string;
  71. constructor() {
  72. this.tag = this.constructor.name;
  73. }
  74. /**
  75. * ViewModel 初始化(可选)
  76. *
  77. * 在页面创建时调用
  78. */
  79. onInit(): void {
  80. // 子类可重写
  81. }
  82. /**
  83. * ViewModel 销毁(可选)
  84. *
  85. * 在页面销毁时调用,用于清理资源
  86. */
  87. onDestroy(): void {
  88. // 子类可重写
  89. }
  90. /**
  91. * 显示加载状态
  92. */
  93. protected setLoading(state: UiState, loading: boolean): void {
  94. if (state) {
  95. state.loading = loading;
  96. }
  97. }
  98. /**
  99. * 显示错误状态
  100. */
  101. protected setError(state: UiState, error: string): void {
  102. if (state) {
  103. state.error = error;
  104. state.loading = false;
  105. }
  106. }
  107. /**
  108. * 清除错误状态
  109. */
  110. protected clearError(state: UiState): void {
  111. if (state) {
  112. state.error = null;
  113. }
  114. }
  115. }