showModal.uts 6.1 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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
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 UTSAndroid from 'io.dcloud.uts.UTSAndroid';
import Typeface from 'android.graphics.Typeface';


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


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()){
			this.setContentView(R.layout.modal_dialog_night);
		}else{
			this.setContentView(R.layout.modal_dialog);
		}
		
		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
	
	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()){
				this.tvSureAction.setBackgroundResource(R.drawable.dialog_button_select_right_night)
			}else{
				this.tvSureAction.setBackgroundResource(R.drawable.dialog_button_select_right)
			}
		}else{
			this.tvCancelAction.setVisibility(View.GONE)
			this.view_split_line_v.setVisibility(View.GONE)
			this.tvSureAction.setVisibility(View.VISIBLE)
			
			if(UTSAndroid.getAppDarkMode()){
				this.tvSureAction.setBackgroundResource(R.drawable.dialog_button_select_total_night)
			}else{
				this.tvSureAction.setBackgroundResource(R.drawable.dialog_button_select_total)
			}
			
		}
		
		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()){
						this.tv_content.setTextColor(UTSAndroid.getAppContext()!.getResources().getColor(R.color.dialog_title_text_night))
					}else{
						this.tv_content.setTextColor(UTSAndroid.getAppContext()!.getResources().getColor(R.color.dialog_title_text))
					}
				}
			}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()){
						this.tv_content.setTextColor(UTSAndroid.getAppContext()!.getResources().getColor(R.color.dialog_title_text_night))
					}else{
						this.tv_content.setTextColor(UTSAndroid.getAppContext()!.getResources().getColor(R.color.dialog_title_text))
					}
				}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)
		this.hostStyle?.complete?.(res)
		
		this.dismiss()
	}
}

let utsDialog:UTSDialog|null = null
/**
 * 弹出对话框的功能入口
 */
export function showModalImpl(style:ShowModalOptions){
	if(utsDialog != null){
		utsDialog?.dismiss()
	}
	utsDialog = new UTSDialog(UTSAndroid.getUniActivity()!);
	utsDialog?.initStyle(style)
	utsDialog?.show();
	
	UTSAndroid.onAppActivityDestroy(function(){
		utsDialog?.dismiss()
		utsDialog = null
	})
	
}