ToolsHelper.ets 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import promptAction from '@ohos.promptAction';
  2. import { Resource } from 'GlobalResource';
  3. import { BusinessError } from '@kit.BasicServicesKit';
  4. import { HashMap } from '@kit.ArkTS';
  5. export interface Btn {
  6. text?: string | Resource;
  7. color?: string | Resource;
  8. onClick: () => void
  9. }
  10. export interface AlertOptions {
  11. title?: string
  12. msg?: string
  13. action: Btn
  14. }
  15. export interface ConfirmOptions {
  16. title?: string
  17. msg?: string
  18. confirm: Btn
  19. cancel: Btn
  20. }
  21. export interface ListOptions<T> {
  22. title?: string
  23. cancel?: Btn
  24. values: Array<T>
  25. onSelected: (index: number, value: T) => void
  26. onError?: (msg: string) => void
  27. }
  28. interface ListItem {
  29. content: string
  30. }
  31. @Builder
  32. function customDialogBuilder<T>(option: ListOptions<T>, dialogId: number) {
  33. Column() {
  34. Text(option.title)
  35. .fontSize(13)
  36. .textAlign(TextAlign.Center)
  37. .width('60%')
  38. .maxLines(2)
  39. .ellipsisMode(EllipsisMode.END)
  40. .textOverflow({
  41. overflow: TextOverflow.Ellipsis
  42. })
  43. .visibility(option.title ? Visibility.Visible : Visibility.None)
  44. List({ space: 20, initialIndex: 0 }) {
  45. ForEach(option.values, (item: T, index: number) => {
  46. ListItem() {
  47. Text(typeof item === "string" ? item : (item as ListItem).content)
  48. .width('100%')
  49. .fontSize(16)
  50. .textAlign(TextAlign.Center)
  51. .onClick(() => {
  52. if (ToolsHelper.mapDialog.get(dialogId)) {
  53. promptAction.closeCustomDialog(ToolsHelper.mapDialog.get(dialogId))
  54. ToolsHelper.mapDialog.remove(dialogId)
  55. }
  56. option.onSelected(index, item)
  57. })
  58. }
  59. }, (item: string) => item)
  60. }
  61. .listDirection(Axis.Vertical) // 排列方向
  62. .scrollBar(BarState.Off)
  63. .friction(0.6)
  64. .divider({ strokeWidth: 1, color: 0xEEEEEE, startMargin: 20, endMargin: 20 }) // 每行之间的分界线
  65. .edgeEffect(EdgeEffect.Spring) // 边缘效果设置为Spring
  66. .width('100%')
  67. .height(option.values.length < 8 ? `${option.values.length / 16 * 100}%` : '50%')
  68. .margin({ top: 20 })
  69. }.padding({ top: 20, bottom: 20, left: 20, right: 20 })
  70. }
  71. /**
  72. * 常用方法
  73. */
  74. export class ToolsHelper {
  75. /**
  76. * 弹出Toast
  77. * @param msg
  78. */
  79. static showMessage(msg: string) {
  80. console.info(msg);
  81. promptAction.showToast({
  82. message: msg,
  83. duration: 1500
  84. });
  85. }
  86. /**kio9
  87. * 弹出Alert弹窗
  88. * @param options
  89. */
  90. static showAlertDialog(options: AlertOptions) {
  91. try {
  92. promptAction.showDialog({
  93. alignment: 1,
  94. title: options.title,
  95. message: options.msg,
  96. buttons: [{
  97. text: options.action.text ?? "确定",
  98. color: options.action.color ?? "#000000",
  99. }]
  100. })
  101. .then(() => {
  102. options.action.onClick()
  103. })
  104. .catch((err: Error) => {
  105. ToolsHelper.showMessage(err.message)
  106. })
  107. } catch (error) {
  108. let message = (error as BusinessError).message
  109. ToolsHelper.showMessage(message)
  110. }
  111. }
  112. /**
  113. * 弹出Confirm弹窗
  114. * @param options
  115. */
  116. static showConfirmDialog(options: ConfirmOptions) {
  117. try {
  118. promptAction.showDialog({
  119. alignment: 1,
  120. title: options.title,
  121. message: options.msg,
  122. buttons: [{
  123. text: options.confirm.text ?? "确定",
  124. color: options.confirm.color ?? "#000000",
  125. }, {
  126. text: options.cancel.text ?? "取消",
  127. color: options.cancel.color ?? "#666666",
  128. }]
  129. })
  130. .then((data) => {
  131. if (data.index === 0) {
  132. options.confirm.onClick()
  133. } else {
  134. options.cancel.onClick()
  135. }
  136. })
  137. .catch((err: Error) => {
  138. ToolsHelper.showMessage(err.message)
  139. })
  140. } catch (error) {
  141. let message = (error as BusinessError).message
  142. ToolsHelper.showMessage(message)
  143. }
  144. }
  145. public static mapDialog = new HashMap<number, number>()
  146. /**
  147. * 弹出List弹窗
  148. * @param options values 如果是非string列表的话,需要存在content字段
  149. */
  150. static showListDialog<T = string>(options: ListOptions<T>, p: object) {
  151. let isSuccess: Array<number> = []
  152. options.values.forEach((item, index) => {
  153. if (typeof item !== 'string') {
  154. if (!(item as ListItem).content) {
  155. isSuccess.push(index)
  156. }
  157. }
  158. })
  159. if (isSuccess.length > 0) {
  160. options.onError && options.onError(`第(${isSuccess.join("、")})个数据中,没有content字段。`)
  161. } else {
  162. const dialogTag = new Date().getTime()
  163. promptAction.openCustomDialog({
  164. alignment: 1,
  165. builder: customDialogBuilder.bind(p, options, dialogTag)
  166. }).then((dialogId: number) => {
  167. ToolsHelper.mapDialog.set(dialogTag, dialogId)
  168. })
  169. }
  170. }
  171. /**
  172. * 弹出自定义弹窗
  173. * @param alignment 弹窗在竖直方向上的对齐方式
  174. */
  175. static showCustomDialog(b: CustomBuilder, alignment?: DialogAlignment) {
  176. const dialogTag = new Date().getTime()
  177. promptAction.openCustomDialog({
  178. alignment: alignment ?? DialogAlignment.Center,
  179. builder: b
  180. }).then((dialogId: number) => {
  181. ToolsHelper.mapDialog.set(dialogTag, dialogId)
  182. }).catch((error: Error) => {
  183. console.log('>>>>>', JSON.stringify(error))
  184. })
  185. }
  186. }