showActionSheet.uts 3.7 KB
Newer Older
DCloud-yyl's avatar
DCloud-yyl 已提交
1
import { UIPopoverArrowDirection , UIDevice , UIUserInterfaceIdiom, CGRect, UIInterfaceOrientationMask } from "UIKit"
2 3 4 5
import { UTSiOS } from "DCloudUTSFoundation"
import { DispatchQueue } from 'Dispatch';
import { DCActionSheetController, DCAlertAction, DCAlertActionStyle } from "DCloudAlertController" assert { type: "implementationOnly" };
import { ShowActionSheetOptions, ShowActionSheetSuccess, ShowActionSheetFail } from '../interface.uts'
DCloud-yyl's avatar
DCloud-yyl 已提交
6 7 8 9 10
import { PromptErrorImpl } from "../unierror.uts"
// #ifdef UNI-APP-X
import { UniSDKEngine } from "DCloudUniappRuntime"
// #endif

11 12 13 14

export function actionSheet(options : ShowActionSheetOptions) {
	const itemList = options.itemList!
	if (itemList.length < 1) {
DCloud-yyl's avatar
DCloud-yyl 已提交
15
		let res = new PromptErrorImpl(1001, "showActionSheet:fail parameter error: parameter.itemList should have at least 1 item");
16 17 18
		options.fail?.(res)
		options.complete?.(res)
		return
DCloud-yyl's avatar
DCloud-yyl 已提交
19 20 21 22 23 24 25 26 27 28
	}
	
	/**
	 * 元素个数不能超过6个
	 */
	if(options.itemList.count > 6){
		let res = new PromptErrorImpl(1001, "showActionSheet:fail parameter error: itemList should not be large than 6");
		options.fail?.(res)
		options.complete?.(res)
		return ;
29 30 31 32 33
	}

	// uts方法默认会在子线程中执行,涉及 UI 操作必须在主线程中运行,通过 DispatchQueue.main.async 方法可将代码在主线程中运行
	DispatchQueue.main.async(execute = () : void => {
		// 初始化actionSheet的实例
DCloud-yyl's avatar
DCloud-yyl 已提交
34 35 36 37 38 39 40
		
		let supportedInterfaceOrientations : UIInterfaceOrientationMask | null = null
		// #ifdef UNI-APP-X
		supportedInterfaceOrientations = UniSDKEngine.shared.getAppManager()?.getCurrentApp()?.appConfig.supportedInterfaceOrientations
		// #endif
		
		let actionSheet = DCActionSheetController.init(noticeTitle = options.title ?? "", supportedInterfaceOrientations = supportedInterfaceOrientations)
41 42 43 44 45 46 47
		 
		// 创建普通按钮
		itemList.forEach((item : string, index : number) => {
			
			let action = DCAlertAction.init(title = item, style = DCAlertActionStyle.defaultStyle, handler = (action: DCAlertAction): void => {
				// 点击按钮的回调方法
				const res: ShowActionSheetSuccess = {
DCloud-yyl's avatar
DCloud-yyl 已提交
48
					tapIndex: index,
49 50 51 52 53 54 55 56 57 58 59 60
				}
				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 => {
DCloud-yyl's avatar
DCloud-yyl 已提交
61 62
				// 点击按钮的回调方法
				let res = new PromptErrorImpl(1, "showActionSheet:fail cancel");
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
				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()
	})
}