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) } } }