showToast.uts 4.6 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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
import { getUniActivity } from "io.dcloud.uts.android";
import Gravity from 'android.view.Gravity';
import Toast from "android.widget.Toast";
import { WaitingView } from "./WaitingView.uts"
import { ShowToastOptions, ShowLoadingOptions, ShowToastFail,ShowToastSuccess, ShowLoadingFail,ShowLoadingSuccess } from '../interface.uts'


type ToastStyle = {
	title : string,
	icon ?: string,
	image ?: string,
	mask ?: boolean,
	duration ?: number,
	position ?: string,
	style ?: string,
	success ?: (res : UTSJSONObject) => void,
	fail ?: (res : UTSJSONObject) => void,
	complete ?: (res : UTSJSONObject) => void
}

let timeout : number | null = null
let toast : WaitingView | null = null
let toastType : string | null = null

export function showToastImpl(style : ShowToastOptions) {
	makeToast(style, 'toast', 'showToast')
	
}

export function hideToastImpl() {
	closeToast("toast")
}

export function showLoadingImpl(option : ShowLoadingOptions) {
	makeLoading(option, 'loading', 'showLoading')
}

export function hideLoadingImpl() {
	closeToast("loading")
}

function closeToast(type : string | null) {
	if (type != null && type !== toastType) {
		return
	}
	if (timeout != null && (timeout as number) > 0) {
		clearTimeout(timeout as number)
		timeout = null
	}
	if (toast != null) {
		(toast as WaitingView).close()
		toast = null
	}
	if (toast != null) {
		(toast as WaitingView).close()
		toast = null
	}
	if (androidToast != null) {
		androidToast!.cancel()
		androidToast = null
	}
	toastType = null
}

function makeLoading(style : ShowLoadingOptions, type : string, errMsg : string) {
	// 关闭之前的展示框
	closeToast(null)
	if (style.title == null) {
		// 没有title 打回报错信息即可
		let res = new UniError("uni-prompt",1001,"showLoading:title is null");
		style.fail?.(res)
		style.complete?.(res)

	} else {
		toastType = type

		let options = {};
		let icon = "success"

		options = {
			name: style.title,
			modal: style.mask,
			back: 'transmit',
			padding: '10',
			size: '16' // 固定字体大小
		}

		options["width"] = "140"
		options["height"] = "112"

		const alert = new WaitingView(getUniActivity(), options)
		toast = alert
		alert.showWaiting()

		const res : ShowLoadingSuccess = {
		}
		style.success?.(res)
		style.complete?.(res)
	}
}

let androidToast:Toast| null = null


function makeToast(style : ShowToastOptions, type : string, errMsg : string) {
	closeToast(null)
	if (style.title == null || style.title.length == 0) {

		let res = new UniError("uni-prompt",1001,"showLoading:title is null");
		style.fail?.(res)
		style.complete?.(res)

	} else {
		
		toastType = type
		if (["top", "center", "bottom"].indexOf(style.position) >= 0) {
			androidToast = Toast.makeText(getUniActivity(), style.title, Toast.LENGTH_SHORT);
			switch (style.position) {
				case "top": {
					// 修复bottom/top 与 前端api位置不一致的问题
					androidToast!.setGravity(Gravity.TOP, androidToast!.getXOffset(), androidToast!.getYOffset())
					break
				}
				case "center": {
					androidToast!.setGravity(Gravity.CENTER, 0, 0)
					break
				}
				case "bottom": {
					androidToast!.setGravity(Gravity.BOTTOM, androidToast!.getXOffset(), androidToast!.getYOffset())
					break
				}
			}
			androidToast!.show()
			const res : ShowToastSuccess = {
			}
			
			style.success?.(res)
			style.complete?.(res)
		} else {
			let options = {};
			let icon = style.icon
			if (icon == null || ["success", "loading", "error", "none"].indexOf(icon) < 0) {
				icon = "success"
			}
			options = {
				name: style.title,
				modal: style.mask,
				back: 'transmit',
				padding: '10',
				size: '16' // 固定字体大小
			}
			if ((style.image == null || style.image == '') && icon == "none") {
				options["loading"] = {
					display: "none"
				}
			} else {
				options["width"] = "140"
				options["height"] = "112"
			}
			if (style.image != null && style.image != '') {
				options["loading"] = {
					display: "block",
					height: "55",
					icon: style.image
				}
			} else {
				if (['success', 'error'].indexOf(icon) >= 0) {
					options["loading"] = {
						display: 'block',
						height: '36',
						icon: icon == "success" ? "successIcon" : "errorIcon"
					}
				}
			}

			const alert = new WaitingView(getUniActivity(), options)
			toast = alert
			alert.showWaiting()
			let duration : number | null = style.duration
			if (duration == null || duration <= 0) {
				duration = 1500
			}
			if (type !== "loading") {
				timeout = setTimeout(() => {
					alert.close()
				}, duration)
			}
			
			const res : ShowToastSuccess = {
			}
			
			style.success?.(res)
			style.complete?.(res)
		}
	}
}