showModal.uts 8.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
import Dialog from 'android.app.Dialog';
import Context from 'android.content.Context';
import R from 'io.dcloud.uts.prompt.R';
import TextView from 'android.widget.TextView';
import EditText from 'android.widget.EditText';
import View from 'android.view.View';
import Color from 'android.graphics.Color';


import { ShowModalOptions,ShowModalSuccess,ShowModalFail} from '../interface.uts'
import TextUtils from 'android.text.TextUtils';

DCloud-yyl's avatar
DCloud-yyl 已提交
13 14 15 16 17 18
// #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

19 20 21 22 23 24 25 26 27 28 29 30 31 32

class UTSDialog extends Dialog implements View.OnClickListener {
	
	tv_title:TextView
	tv_content:TextView
	et_content:EditText
	tvCancelAction:TextView
	tvSureAction:TextView
	view_split_line_v:View

	constructor(context:Context){
		super(context);
		
		if(UTSAndroid.getAppDarkMode()){
DCloud-yyl's avatar
DCloud-yyl 已提交
33
			this.setContentView(R.layout.uni_prompt_modal_dialog_night);
34
		}else{
DCloud-yyl's avatar
DCloud-yyl 已提交
35
			this.setContentView(R.layout.uni_prompt_modal_dialog);
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
		}
		
		this.getWindow()?.setBackgroundDrawableResource(android.R.color.transparent);
		
		this.tv_title = this.findViewById<TextView>(R.id.tv_title)
		this.tv_content = this.findViewById<TextView>(R.id.tv_content)
		this.et_content = this.findViewById<EditText>(R.id.et_content)
		this.tvCancelAction= this.findViewById<TextView>(R.id.tvCancelAction)
		this.tvSureAction= this.findViewById<TextView>(R.id.tvSureAction)
		this.view_split_line_v = findViewById(R.id.view_split_line_v)
		
	}
	
	hostStyle:ShowModalOptions|null = null
	
DCloud-yyl's avatar
DCloud-yyl 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
	override dismiss():void {
		/**
		 * 对齐微信,dismiss中的调用都是 cancel
		 */
		const res: ShowModalSuccess = {
			confirm: false,
			cancel: true,
		}
		// 为了确保complete只调用一次,执行后手动置空
		this.hostStyle?.complete?.(res)
		if(this.hostStyle != null){
			this.hostStyle!.complete = null
		}
		
		super.dismiss()
		
	}
	
69 70 71 72 73 74 75 76 77 78
	initStyle(style:ShowModalOptions){
		this.hostStyle = style
		this.setCanceledOnTouchOutside(false)
		
		if(style.showCancel != null && style.showCancel == true){
			this.tvCancelAction.setVisibility(View.VISIBLE)
			this.view_split_line_v.setVisibility(View.VISIBLE)
			this.tvSureAction.setVisibility(View.VISIBLE)
			
			if(UTSAndroid.getAppDarkMode()){
DCloud-yyl's avatar
DCloud-yyl 已提交
79
				this.tvSureAction.setBackgroundResource(R.drawable.uni_prompt_dialog_button_select_right_night)
80
			}else{
DCloud-yyl's avatar
DCloud-yyl 已提交
81
				this.tvSureAction.setBackgroundResource(R.drawable.uni_prompt_dialog_button_select_right)
82 83 84 85 86 87 88
			}
		}else{
			this.tvCancelAction.setVisibility(View.GONE)
			this.view_split_line_v.setVisibility(View.GONE)
			this.tvSureAction.setVisibility(View.VISIBLE)
			
			if(UTSAndroid.getAppDarkMode()){
DCloud-yyl's avatar
DCloud-yyl 已提交
89
				this.tvSureAction.setBackgroundResource(R.drawable.uni_prompt_dialog_button_select_total_night)
90
			}else{
DCloud-yyl's avatar
DCloud-yyl 已提交
91
				this.tvSureAction.setBackgroundResource(R.drawable.uni_prompt_dialog_button_select_total)
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
			}
			
		}
		
		if(style.editable != null && style.editable == true){
			// 编辑模式
			this.et_content.setVisibility(View.VISIBLE)
			this.tv_content.setVisibility(View.GONE)
			
			if(!TextUtils.isEmpty(style.content)){
				
				this.et_content.setFocusable(true);
				this.et_content.setFocusableInTouchMode(true);
				this.et_content.requestFocus();
				this.et_content.setText(style.content)
				this.et_content.setSelection(this.et_content.getText().length);
			}
			
			if(style.placeholderText != null){
				this.et_content.setHint(style.placeholderText)
			}
			
			if(!TextUtils.isEmpty(style.title)){
				this.tv_title.setVisibility(View.VISIBLE)
				this.tv_title.setText(style.title)
			}else{
				this.tv_title.setVisibility(View.GONE)
			}
		
		}else{
			// 非编辑模式
			this.et_content.setVisibility(View.GONE)
			
			
			if(!TextUtils.isEmpty(style.title)){
				// title 不为空
				this.tv_title.setVisibility(View.VISIBLE)
				this.tv_title.setText(style.title)
			}else{
				this.tv_title.setVisibility(View.GONE)
			}
			
			if(!TextUtils.isEmpty(style.content)){
				// content 不为空
				this.tv_content.setVisibility(View.VISIBLE)
				this.tv_content.setText(style.content)
				if(TextUtils.isEmpty(style.title)){
					// 如果此时title 为空,需要修改文本颜色
					if(UTSAndroid.getAppDarkMode()){
DCloud-yyl's avatar
DCloud-yyl 已提交
141
						this.tv_content.setTextColor(UTSAndroid.getAppContext()!.getResources().getColor(R.color.uni_prompt_dialog_title_text_night))
142
					}else{
DCloud-yyl's avatar
DCloud-yyl 已提交
143
						this.tv_content.setTextColor(UTSAndroid.getAppContext()!.getResources().getColor(R.color.uni_prompt_dialog_title_text))
144 145 146 147 148 149 150 151 152
					}
				}
			}else{
				// 判断title 是否为空,如果不为空,则将title填充到content,并且修改文本颜色
				if(!TextUtils.isEmpty(style.title)){
					this.tv_title.setVisibility(View.GONE)
					this.tv_content.setVisibility(View.VISIBLE)
					this.tv_content.setText(style.title)
					if(UTSAndroid.getAppDarkMode()){
DCloud-yyl's avatar
DCloud-yyl 已提交
153
						this.tv_content.setTextColor(UTSAndroid.getAppContext()!.getResources().getColor(R.color.uni_prompt_dialog_title_text_night))
154
					}else{
DCloud-yyl's avatar
DCloud-yyl 已提交
155
						this.tv_content.setTextColor(UTSAndroid.getAppContext()!.getResources().getColor(R.color.uni_prompt_dialog_title_text))
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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
					}
				}else{
					this.tv_content.setVisibility(View.GONE)
				}
			}
			
			
		}
		
		if(style.cancelText != null){
			this.tvCancelAction.setText(style.cancelText)
		}
		if(style.cancelColor != null){
			try{
				this.tvCancelAction.setTextColor(Color.parseColor(style.cancelColor))
			}catch(e){
				// 传入的格式异常,捕获
			}
			
		}
		
		if(style.confirmText != null){
			this.tvSureAction.setText(style.confirmText)
		}
		if(style.confirmColor != null){
			try{
				this.tvSureAction.setTextColor(Color.parseColor(style.confirmColor))
			}catch(e){
				// 传入的格式异常,捕获
			}
		}
		
		this.tvCancelAction.setOnClickListener(this)
		this.tvSureAction.setOnClickListener(this)
		
	}
	
	override onClick(view: View) {
		
		const res: ShowModalSuccess = {
			confirm: true,
			cancel: false,
		}
		
		if(view.getId() == R.id.tvCancelAction){
			// 取消
			res.confirm = false
			res.cancel = true
		}else if(view.getId() == R.id.tvSureAction){
			// 确定
			let inputContent = this.et_content.getEditableText().toString()
			if(TextUtils.isEmpty(inputContent)){
				res.content = null
			}
			res.content = inputContent
		}
		
		this.hostStyle?.success?.(res)
DCloud-yyl's avatar
DCloud-yyl 已提交
214
		// 为了确保complete只调用一次,执行后手动置空
215
		this.hostStyle?.complete?.(res)
DCloud-yyl's avatar
DCloud-yyl 已提交
216 217 218
		if(this.hostStyle != null){
			this.hostStyle!.complete = null
		}
219 220 221 222 223 224 225 226 227 228 229
		this.dismiss()
	}
}

let utsDialog:UTSDialog|null = null
/**
 * 弹出对话框的功能入口
 */
export function showModalImpl(style:ShowModalOptions){
	if(utsDialog != null){
		utsDialog?.dismiss()
DCloud-yyl's avatar
DCloud-yyl 已提交
230
		utsDialog = null
231 232
	}
	
DCloud-yyl's avatar
DCloud-yyl 已提交
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 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
	
	
	// #ifndef UNI-APP-X
		/**
		 * uni-app
		 */
		if(UTSAndroid.getTopPageActivity() == null){
			return 
		}
		if(UTSAndroid.getTopPageActivity()!.isFinishing()){
			return 
		}
		utsDialog = new UTSDialog(UTSAndroid.getTopPageActivity()!);
		utsDialog?.initStyle(style)
		utsDialog?.show();
		
		UTSAndroid.onAppActivityDestroy(function(){
			utsDialog?.dismiss()
			utsDialog = 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) {
				if(UTSAndroid.getTopPageActivity() == null){
					return 
				}
				if(UTSAndroid.getTopPageActivity()!.isFinishing()){
					return 
				}
				utsDialog = new UTSDialog(UTSAndroid.getTopPageActivity()!);
				utsDialog?.initStyle(style)
		        utsDialog?.show();
		    } else {
		        onReady(() => {
					if(UTSAndroid.getTopPageActivity() == null){
						return 
					}
					if(UTSAndroid.getTopPageActivity()!.isFinishing()){
						return 
					}
					utsDialog = new UTSDialog(UTSAndroid.getTopPageActivity()!);
					utsDialog?.initStyle(style)
		            utsDialog?.show();
		        }, instance)
		    }
		    
			onUnload(() => {
				utsDialog?.dismiss()
				utsDialog = null
			}, instance)
			
		    
		}
	// #endif
	
	
298 299
	
}