showActionSheet.uts 2.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
import { UIPopoverArrowDirection , UIDevice , UIUserInterfaceIdiom, CGRect } from "UIKit"
import { UTSiOS } from "DCloudUTSFoundation"
import { DispatchQueue } from 'Dispatch';
import { DCActionSheetController, DCAlertAction, DCAlertActionStyle } from "DCloudAlertController" assert { type: "implementationOnly" };
import { ShowActionSheetOptions, ShowActionSheetSuccess, ShowActionSheetFail } from '../interface.uts'


export function actionSheet(options : ShowActionSheetOptions) {
	const itemList = options.itemList!
	if (itemList.length < 1) {
		let res = new UniError("uni-prompt",-3,"showActionSheet:fail cancel");
		options.fail?.(res)
		options.complete?.(res)
		return
	}

	// uts方法默认会在子线程中执行,涉及 UI 操作必须在主线程中运行,通过 DispatchQueue.main.async 方法可将代码在主线程中运行
	DispatchQueue.main.async(execute = () : void => {
		// 初始化actionSheet的实例
		let actionSheet = DCActionSheetController.init(noticeTitle = options.title ?? "")
		 
		// 创建普通按钮
		itemList.forEach((item : string, index : number) => {
			
			let action = DCAlertAction.init(title = item, style = DCAlertActionStyle.defaultStyle, handler = (action: DCAlertAction): void => {
				// 点击按钮的回调方法
				const res: ShowActionSheetSuccess = {
					tapIndex: index + 1,
				}
				options.success?.(res)
				options.complete?.(res)   
			})
		
			if (options.itemColor != null) {
				const color = UTSiOS.colorWithString(options.itemColor!)
				action.titleColor = color
			}
			actionSheet.addAction(action)
		})
		let cancelAction = DCAlertAction.init(title = "取消", style = DCAlertActionStyle.cancelStyle, handler = (action: DCAlertAction): void => {
				// 点击按钮的回调方法
				let res = new UniError("uni-prompt",1002,"showActionSheet:fail cancel");
				options.fail?.(res)
				options.complete?.(res)
		})
		if (options.itemColor != null) {
			const color = UTSiOS.colorWithString(options.itemColor!)
			cancelAction.titleColor = color
		}
		actionSheet.addAction(cancelAction)
		
		// 判断是否设置popover
		const isPad = UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad
		if (options.popover != null && isPad == true) {
			let popoverController = actionSheet.popoverPresentationController
			if (popoverController != null) {
				let sourceView = UTSiOS.getCurrentViewController().view
				if (sourceView != null) {
					let sourceRect = CGRect(x = options.popover!.left.toDouble(), y = options.popover!.top.toDouble(), width = options.popover!.width.toDouble(), height = options.popover!.height.toDouble())
					if (options.popover!.height == -1) {
						sourceRect = CGRect(x = sourceView!.bounds.midX, y = sourceView!.bounds.maxY, width = 1.0, height = 1.0)
					}
					popoverController!.sourceView = sourceView
					popoverController!.sourceRect = sourceRect
					popoverController!.permittedArrowDirections = UIPopoverArrowDirection.any
				}
			}
		}
		actionSheet.show()
	})
}