YTToast.ets 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { BusinessError } from '@kit.BasicServicesKit';
  2. import { ComponentContent, promptAction } from '@kit.ArkUI';
  3. import { UIContext } from '@ohos.arkui.UIContext';
  4. import { BasicType, YTLog } from '../../../../Index';
  5. import { yTDoubleConfirm } from '../components/YtDoubleConfirm';
  6. import { AgreePrivacy } from '../components/AgreePrivacy';
  7. interface InitOption {
  8. context: UIContext;
  9. options: promptAction.BaseDialogOptions;
  10. }
  11. class Params<T> implements BasicType<T> {
  12. src?: ResourceStr
  13. acSrc?: ResourceStr
  14. text?: string
  15. index?: number
  16. number?: number | undefined
  17. message?: string
  18. click?: () => void
  19. constructor(item: BasicType<undefined>) {
  20. this.src = item.src;
  21. this.text = item.text;
  22. this.index = item.index;
  23. this.number = item.number;
  24. this.click = item.click;
  25. this.message = item.message;
  26. this.acSrc = item.acSrc;
  27. }
  28. }
  29. export class YTToast {
  30. private declare ctx: UIContext;
  31. private declare contentNode: ComponentContent<Object> | null;
  32. private declare options: promptAction.BaseDialogOptions;
  33. private maskColor: string = '#80000000'
  34. init(initOption: InitOption) {
  35. this.ctx = initOption.context;
  36. initOption.options.maskColor = this.maskColor
  37. this.options = initOption.options;
  38. }
  39. setContext(context: UIContext) {
  40. this.ctx = context;
  41. }
  42. setContentNode(node: ComponentContent<Object>) {
  43. this.contentNode = node;
  44. }
  45. setOptions(options: promptAction.BaseDialogOptions) {
  46. this.options = options;
  47. }
  48. openToast(builder: WrappedBuilder<[
  49. BasicType<undefined>
  50. ]>, item: BasicType<undefined>) {
  51. this.contentNode =
  52. new ComponentContent(this.ctx, builder, new Params<undefined>(item));
  53. this.ctx.getPromptAction()
  54. .openCustomDialog(this.contentNode, this.options)
  55. .then(() => {
  56. console.info('OpenCustomDialog complete.')
  57. })
  58. .catch((error: BusinessError) => {
  59. let message = (error as BusinessError).message;
  60. let code = (error as BusinessError).code;
  61. console.error(`OpenCustomDialog args error code is ${code}, message is ${message}`);
  62. })
  63. }
  64. doubleConfirm(item: BasicType<undefined>) {
  65. this.openToast(wrapBuilder(yTDoubleConfirm), item)
  66. }
  67. agreePrivacy(item: BasicType<undefined>) {
  68. this.openToast(wrapBuilder(AgreePrivacy), item)
  69. }
  70. hide() {
  71. try {
  72. if (this.contentNode !== null) {
  73. this.ctx.getPromptAction()
  74. .closeCustomDialog(this.contentNode)
  75. .then(() => {
  76. this.contentNode = null
  77. console.info('CloseCustomDialog complete.')
  78. })
  79. .catch((error: BusinessError) => {
  80. let message = (error as BusinessError).message;
  81. let code = (error as BusinessError).code;
  82. console.error(`CloseCustomDialog args error code is ${code}, message is ${message}`);
  83. })
  84. }
  85. } catch (e) {
  86. YTLog.warn(e)
  87. }
  88. }
  89. updateDialog(options: promptAction.BaseDialogOptions) {
  90. if (this.contentNode !== null) {
  91. this.ctx.getPromptAction()
  92. .updateCustomDialog(this.contentNode, options)
  93. .then(() => {
  94. console.info('UpdateCustomDialog complete.')
  95. })
  96. .catch((error: BusinessError) => {
  97. let message = (error as BusinessError).message;
  98. let code = (error as BusinessError).code;
  99. console.error(`UpdateCustomDialog args error code is ${code}, message is ${message}`);
  100. })
  101. }
  102. }
  103. }
  104. export const yTToast = new YTToast()