showToast.uts 6.5 KB
Newer Older
DCloud-yyl's avatar
DCloud-yyl 已提交
1

2 3 4
import Gravity from 'android.view.Gravity';
import Toast from "android.widget.Toast";
import { WaitingView } from "./WaitingView.uts"
DCloud-yyl's avatar
DCloud-yyl 已提交
5 6 7 8 9 10 11 12
import { ShowToastOptions, ShowLoadingOptions, ShowToastSuccess, ShowLoadingSuccess } from '../interface.uts'
import { PromptErrorImpl } from "../unierror.uts"

// #ifdef UNI-APP-X
import getCurrentPages from 'io.dcloud.uniapp.framework.getCurrentPages';
import onReady from 'io.dcloud.uniapp.framework.onReady';
import onUnload from 'io.dcloud.uniapp.framework.onUnload';
// #endif
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


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 打回报错信息即可
DCloud-yyl's avatar
DCloud-yyl 已提交
77
		let res = new PromptErrorImpl(1001, "showLoading:title is null");
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
		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"

DCloud-yyl's avatar
DCloud-yyl 已提交
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
		
		
		// #ifndef UNI-APP-X
			/**
			 * uni-app
			 */
			const alert = new WaitingView(UTSAndroid.getTopPageActivity(), options)
			toast = alert
			alert?.showWaiting();
			
			UTSAndroid.onAppActivityDestroy(function(){
				toast?.close()
				toast = null
			})
			
		// #endif
		// #ifdef UNI-APP-X
			/**
			 * uni-app x
			 * 需要特殊处理生命周期 
			 */
			const pages = getCurrentPages();
			if (pages.length > 0) {
			    const page = pages[pages.length - 1]
			    const instance = page.$
			    if (page.$isReady) {
					const alert = new WaitingView(UTSAndroid.getTopPageActivity(), options)
					toast = alert
			        alert?.showWaiting();
			    } else {
			        onReady(() => {
						const alert = new WaitingView(UTSAndroid.getTopPageActivity(), options)
						toast = alert
			            alert?.showWaiting();
			        }, instance)
			    }
			    
				onUnload(() => {
					toast?.close()
					toast = null
				}, instance)
			}
		// #endif
		
		
143 144 145 146 147 148 149 150 151 152 153 154 155
		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) {
DCloud-yyl's avatar
DCloud-yyl 已提交
156
		let res = new PromptErrorImpl(1001, "showLoading:title is null");
157 158 159 160 161 162 163
		style.fail?.(res)
		style.complete?.(res)

	} else {
		
		toastType = type
		if (["top", "center", "bottom"].indexOf(style.position) >= 0) {
DCloud-yyl's avatar
DCloud-yyl 已提交
164
			androidToast = Toast.makeText(UTSAndroid.getTopPageActivity(), style.title, Toast.LENGTH_SHORT);
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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
			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"
					}
				}
			}

DCloud-yyl's avatar
DCloud-yyl 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
			
			
			// #ifndef UNI-APP-X
				/**
				 * uni-app
				 */
				const alert = new WaitingView(UTSAndroid.getTopPageActivity(), options)
				toast = alert
				alert?.showWaiting();
				
				UTSAndroid.onAppActivityDestroy(function(){
					toast?.close()
					toast = null
				})
				
			// #endif
			// #ifdef UNI-APP-X
				/**
				 * uni-app x
				 * 需要特殊处理生命周期 
				 */
				const pages = getCurrentPages();
				if (pages.length > 0) {
				    const page = pages[pages.length - 1]
				    const instance = page.$
				    if (page.$isReady) {
						const alert = new WaitingView(UTSAndroid.getTopPageActivity(), options)
						toast = alert
				        alert?.showWaiting();
				    } else {
				        onReady(() => {
							const alert = new WaitingView(UTSAndroid.getTopPageActivity(), options)
							toast = alert
				            alert?.showWaiting();
				        }, instance)
				    }
				    
					onUnload(() => {
						toast?.close()
						toast = null
					}, instance)
				}
			// #endif
			
			
268 269 270 271 272 273
			let duration : number | null = style.duration
			if (duration == null || duration <= 0) {
				duration = 1500
			}
			if (type !== "loading") {
				timeout = setTimeout(() => {
DCloud-yyl's avatar
DCloud-yyl 已提交
274
					toast?.close()
275 276 277 278 279 280 281 282 283 284 285 286
				}, duration)
			}
			
			const res : ShowToastSuccess = {
			}
			
			style.success?.(res)
			style.complete?.(res)
		}
	}
}