YTToast.ets 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 static declare instance: YTToast
  31. private declare ctx: UIContext;
  32. private declare contentNode: ComponentContent<Object> | null;
  33. private declare options: promptAction.BaseDialogOptions;
  34. private maskColor: string = '#80000000'
  35. static getInstance() {
  36. if (!YTToast.instance) {
  37. YTToast.instance = new YTToast()
  38. }
  39. return YTToast.instance
  40. }
  41. init(initOption: InitOption) {
  42. this.ctx = initOption.context;
  43. initOption.options.maskColor = this.maskColor
  44. this.options = initOption.options;
  45. }
  46. setContext(context: UIContext) {
  47. this.ctx = context;
  48. }
  49. setContentNode(node: ComponentContent<Object>) {
  50. this.contentNode = node;
  51. }
  52. setOptions(options: promptAction.BaseDialogOptions) {
  53. this.options = options;
  54. }
  55. openToast(builder: WrappedBuilder<[
  56. BasicType<undefined>
  57. ]>, item: BasicType<undefined>) {
  58. this.contentNode =
  59. new ComponentContent(this.ctx, builder, new Params<undefined>(item));
  60. this.ctx.getPromptAction()
  61. .openCustomDialog(this.contentNode, this.options)
  62. .then(() => {
  63. console.info('OpenCustomDialog complete.')
  64. })
  65. .catch((error: BusinessError) => {
  66. let message = (error as BusinessError).message;
  67. let code = (error as BusinessError).code;
  68. console.error(`OpenCustomDialog args error code is ${code}, message is ${message}`);
  69. })
  70. }
  71. doubleConfirm(item: BasicType<undefined>) {
  72. this.openToast(wrapBuilder(yTDoubleConfirm), item)
  73. }
  74. agreePrivacy(item: BasicType<undefined>) {
  75. this.openToast(wrapBuilder(AgreePrivacy), item)
  76. }
  77. hide() {
  78. try {
  79. if (this.contentNode !== null) {
  80. this.ctx.getPromptAction()
  81. .closeCustomDialog(this.contentNode)
  82. .then(() => {
  83. this.contentNode = null
  84. console.info('CloseCustomDialog complete.')
  85. })
  86. .catch((error: BusinessError) => {
  87. let message = (error as BusinessError).message;
  88. let code = (error as BusinessError).code;
  89. console.error(`CloseCustomDialog args error code is ${code}, message is ${message}`);
  90. })
  91. }
  92. } catch (e) {
  93. YTLog.warn(e)
  94. }
  95. }
  96. updateDialog(options: promptAction.BaseDialogOptions) {
  97. if (this.contentNode !== null) {
  98. this.ctx.getPromptAction()
  99. .updateCustomDialog(this.contentNode, options)
  100. .then(() => {
  101. console.info('UpdateCustomDialog complete.')
  102. })
  103. .catch((error: BusinessError) => {
  104. let message = (error as BusinessError).message;
  105. let code = (error as BusinessError).code;
  106. console.error(`UpdateCustomDialog args error code is ${code}, message is ${message}`);
  107. })
  108. }
  109. }
  110. }