showToast.uts 3.3 KB
Newer Older
DCloud-yyl's avatar
DCloud-yyl 已提交
1 2 3
import { UTSiOS } from "DCloudUTSFoundation"
import { MCToast, MCToastConfig } from "DCUniToast"  assert { type: "implementationOnly" };
import { UIScreen, UIImage } from 'UIKit'
4
import { ShowToastOptions, ShowLoadingOptions, ShowToastSuccess, ShowToastFail, ShowLoadingSuccess, ShowLoadingFail } from '../interface.uts'
DCloud-yyl's avatar
DCloud-yyl 已提交
5 6 7 8
import { PromptErrorImpl } from "../unierror.uts"

/* 显示toast */
export function toShowToast(options : ShowToastOptions) {
9
	if (options.title == null || options.title.length == 0) {
DCloud-yyl's avatar
DCloud-yyl 已提交
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
		let res = new PromptErrorImpl(1001, "showLoading:title is null");
		options.fail?.(res)
		options.complete?.(res)
	} else {
		//duration
		let duration = 2.5
		if (options.duration != null) {
			duration = options.duration! / 1000
		}
		//mask
		let mask = MCToast.MCToastRespond.default
		if (options.mask == true) {
			mask = MCToast.MCToastRespond.noRespond
		}
		MCToastConfig.shared.background.size = CGSize.init(width = 135, height = 120);
		//position
		if (options.position != null) {
			const interval = Float(UIScreen.main.bounds.height * 0.25)
			let centerPoint = 0.0 //正下 负上 
			if (options.position == "top") {
				centerPoint = 0 - interval
			} else if (options.position == "center") {
				centerPoint = 0.0
			} else {
				centerPoint = Number(interval)
			}
			MCToast.mc_text(options.title, offset = CGFloat(centerPoint.toFloat()), duration = CGFloat(duration.toFloat()), respond = mask)
		} else {
			MCToastConfig.shared.icon.successImage = new UIImage(named = "uni_uts_toast_success.png")
			MCToastConfig.shared.icon.failureImage = new UIImage(named = "uni_uts_toast_error.png")
			if (options.image != null) {
				const imagePath = UTSiOS.getResourcePath(options.image!)
				const image = new UIImage(contentsOfFile = imagePath)
				MCToast.showStatus(MCToast.MCToastType.success, text = options.title, iconImage = image, duration = CGFloat(duration.toFloat()), respond = mask)

			} else if (options.icon != null) {
				if (options.icon == "success") {
					MCToast.mc_success(options.title, duration = CGFloat(duration.toFloat()), respond = mask)
				} else if (options.icon == "error") {
					MCToast.mc_failure(options.title, duration = CGFloat(duration.toFloat()), respond = mask)
				} else if (options.icon == "loading") {
					MCToast.mc_loading(text = options.title, duration = CGFloat(duration.toFloat()), respond = mask)
				} else {
					MCToast.mc_text(options.title, offset = 0, duration = CGFloat(duration.toFloat()), respond = mask)
				}
			} else {
				MCToast.mc_success(options.title, duration = CGFloat(duration.toFloat()), respond = mask)
			}
		}
		const res : ShowToastSuccess = {
		}
		options.success?.(res)
		options.complete?.(res)
	}

}

/* 隐藏toast */
export function toHideToast() {
	MCToast.mc_remove()
}

/* 显示loading + text */
export function toShowLoading(options : ShowLoadingOptions) {

	let mask = MCToast.MCToastRespond.default
	if (options.mask == true) {
		mask = MCToast.MCToastRespond.noRespond
	}
	// config
	MCToastConfig.shared.background.size = CGSize.init(width = 135, height = 120);
	// show loading
	MCToast.mc_loading(text = options.title, respond = mask)
	// callback
	const res : ShowLoadingSuccess = {
	}
	options.success?.(res)
	options.complete?.(res)

}

/* 隐藏loading */
export function toHideLoading() {
	MCToast.mc_remove()
94
}