YTBindSheet.ets 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { BasicType } from '../models';
  2. import { ComponentContent } from '@kit.ArkUI';
  3. import { Params } from './YTToast';
  4. import { BusinessError } from '@ohos.base';
  5. interface InitOption {
  6. context: UIContext;
  7. options: SheetOptions
  8. }
  9. export class YTBindSheet {
  10. private static declare instance: YTBindSheet
  11. private declare ctx: UIContext;
  12. private declare contentNode: ComponentContent<Object> | null;
  13. private declare options: SheetOptions
  14. private maskColor: string = '#80000000'
  15. static getInstance() {
  16. if (!YTBindSheet.instance) {
  17. YTBindSheet.instance = new YTBindSheet()
  18. }
  19. return YTBindSheet.instance
  20. }
  21. init(initOption: InitOption) {
  22. this.ctx = initOption.context;
  23. if (!initOption.options.maskColor) {
  24. initOption.options.maskColor = this.maskColor
  25. }
  26. this.options = initOption.options;
  27. }
  28. setContext(context: UIContext) {
  29. this.ctx = context;
  30. }
  31. setContentNode(node: ComponentContent<Object>) {
  32. this.contentNode = node;
  33. }
  34. setOptions(options: SheetOptions) {
  35. const keys = Object.keys(options)
  36. keys.forEach((key) => {
  37. if (options[key]) {
  38. this.options[key] = options[key]
  39. }
  40. })
  41. }
  42. openBindSheet(builder: WrappedBuilder<[
  43. BasicType
  44. ]>, item: BasicType) {
  45. this.contentNode =
  46. new ComponentContent(this.ctx, builder, new Params<undefined>(item));
  47. this.ctx.openBindSheet(this.contentNode, this.options)
  48. .then(() => {
  49. console.info('openBindSheet success');
  50. })
  51. .catch((err: BusinessError) => {
  52. console.error('openBindSheet error: ' + err.code + ' ' + err.message);
  53. })
  54. }
  55. hide() {
  56. this.ctx.closeBindSheet(this.contentNode)
  57. .then(() => {
  58. console.info('closeBindSheet success');
  59. })
  60. .catch((err: BusinessError) => {
  61. console.error('closeBindSheet error: ' + err.code + ' ' + err.message);
  62. })
  63. }
  64. //刷新数据
  65. updateBindSheet(item: BasicType) {
  66. if (this.contentNode !== null) {
  67. this.contentNode.update(item)
  68. }
  69. }
  70. }
  71. export const yTBindSheet = YTBindSheet.getInstance()