showActionSheet.uts 9.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
import Dialog from 'android.app.Dialog';
import Activity from 'android.app.Activity';
import Gravity from 'android.view.Gravity';

import ViewGroup from 'android.view.ViewGroup';
import View from 'android.view.View';
import Color from 'android.graphics.Color';

import R from 'io.dcloud.uts.prompt.R';
import TextView from 'android.widget.TextView';
import LayoutInflater from 'android.view.LayoutInflater';

import AppCompatTextView from 'androidx.appcompat.widget.AppCompatTextView';
import LinearLayoutCompat from 'androidx.appcompat.widget.LinearLayoutCompat';

import LinearLayoutManager from 'androidx.recyclerview.widget.LinearLayoutManager';
import RecyclerView from 'androidx.recyclerview.widget.RecyclerView';
DCloud-yyl's avatar
DCloud-yyl 已提交
18
import WindowManager from 'android.view.WindowManager';
19

DCloud-yyl's avatar
DCloud-yyl 已提交
20
import { ShowActionSheetOptions, ShowActionSheetSuccess} from '../interface.uts'
21

DCloud-yyl's avatar
DCloud-yyl 已提交
22 23 24 25 26
// #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
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


let uniActionSheet:UniActionSheet|null = null

/**
 * 弹出功能入口
 */
export function actionSheetImpl(style: ShowActionSheetOptions) {
	
	/**
	 * 参数校验,itemList 不能为空
	 */
	if(style.itemList == null || style.itemList.length < 1){

		let res = new UniError("uni-prompt",-1,"showActionSheet:fail parameter error: parameter.itemList should have at least 1 item");
		
		style.fail?.(res)
		style.complete?.(res)
		return ;
	}
	
	
	/**
	 * 元素个数不能超过6个
	 */
	if(style.itemList.size > 6){
		let res = new UniError("uni-prompt",-2,"showActionSheet:fail parameter error: itemList should not be large than 6");
		
		style.fail?.(res)
		style.complete?.(res)
		return ;
	}
	
	
	if(uniActionSheet != null){
		uniActionSheet?.dismiss()
DCloud-yyl's avatar
DCloud-yyl 已提交
63
		uniActionSheet = null
64 65
	}
	
DCloud-yyl's avatar
DCloud-yyl 已提交
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
	
	// #ifndef UNI-APP-X
		/**
		 * uni-app
		 */
		if(UTSAndroid.getTopPageActivity() == null){
			return 
		}
		if(UTSAndroid.getTopPageActivity()!.isFinishing()){
			return 
		}
		uniActionSheet = new UniActionSheet(UTSAndroid.getTopPageActivity()!, style)
		uniActionSheet?.show()
		UTSAndroid.onAppActivityDestroy(function(){
			uniActionSheet?.dismiss()
			uniActionSheet = 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 
				}
				uniActionSheet = new UniActionSheet(UTSAndroid.getTopPageActivity()!, style)
		        uniActionSheet?.show()
		    } else {
		        onReady(() => {
					if(UTSAndroid.getTopPageActivity() == null){
						return 
					}
					if(UTSAndroid.getTopPageActivity()!.isFinishing()){
						return 
					}
					uniActionSheet = new UniActionSheet(UTSAndroid.getTopPageActivity()!, style)
		            uniActionSheet?.show()
		        }, instance)
		    }
		    
			onUnload(() => {
				uniActionSheet?.dismiss()
				uniActionSheet = null
			}, instance)
			
		}
	// #endif
	
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
	
}


/**
 * UniActionSheet 
 * 功能实现
 */
export class UniActionSheet extends Dialog{
	
	/**
	 * 宿主activity
	 */
	hostActivity: Activity
	/**
	 * 宿主样式
	 */
	hostStyle:ShowActionSheetOptions
	/**
	 * 标题组件
	 */
	title: AppCompatTextView
	/**
	 * 取消按钮
	 */
	cancel: AppCompatTextView
	/**
	 * 列表 recycler 
	 */
	myRecyclerView: RecyclerView
	
	/**
	 * title 组件
	 */
	lineTitle: LinearLayoutCompat
	/**
	 * 内容组件
	 */
	lineContent: LinearLayoutCompat
	/**
	 * 取消组件
	 */
	lineCancel: LinearLayoutCompat

DCloud-yyl's avatar
DCloud-yyl 已提交
168 169 170 171 172 173 174 175 176 177 178 179

	override dismiss():void {
		let res = new UniError("uni-prompt",1002,"showActionSheet:fail cancel");
		this.hostStyle.fail?.(res)
		this.hostStyle.fail = null
		// 为了确保complete只调用一次,执行后手动置空
		this.hostStyle.complete?.(res)
		this.hostStyle.complete = null
		
		super.dismiss()
	}
	
180 181 182
	
	constructor(activity: Activity, style: ShowActionSheetOptions) {
		
DCloud-yyl's avatar
DCloud-yyl 已提交
183
		super(activity,R.style.uni_prompt_ActionsheetDialog);
184 185 186 187 188 189 190 191
		
		this.hostActivity = activity
		this.hostStyle = style
		
		/**
		 * 适配暗黑模式,配置不同的布局文件
		 */
		if(UTSAndroid.getAppDarkMode()){
DCloud-yyl's avatar
DCloud-yyl 已提交
192
			setContentView(R.layout.uni_prompt_uts_action_sheet_night)
193
		}else{
DCloud-yyl's avatar
DCloud-yyl 已提交
194
			setContentView(R.layout.uni_prompt_uts_action_sheet)
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
		}
		
		
		this.title = findViewById(R.id.tvTitle)
		this.cancel = findViewById(R.id.tvCancelAction)
		this.lineTitle = findViewById(R.id.line_title)
		this.lineContent = findViewById(R.id.line_content)
		this.lineCancel = findViewById(R.id.line_cancel)
		this.myRecyclerView = findViewById(R.id.myRecyclerview)
		this.myRecyclerView.layoutManager = LinearLayoutManager(this.hostActivity)
		
		
		this.cancel.setOnClickListener(new CancelClickListener(this,this.hostStyle));
		let recyclerviewAdapter = new ItemAdapter(this,this.hostStyle)
		this.myRecyclerView.adapter = recyclerviewAdapter
		
		if(!style.title.isNullOrBlank()){
			this.lineTitle.setVisibility(View.VISIBLE)
			this.title.setText(style.title)
		}
		
		if(this.window != null){
			this.window!.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT)
			this.window!.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
			if(this.window!.attributes != null){
DCloud-yyl's avatar
DCloud-yyl 已提交
220
				this.window!.attributes!.windowAnimations = R.style.uni_prompt_DialogAnimations_slideWindow;
221 222 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
			}
		}
		
	}
	

}

/**
 * 列表选项点击事件监听
 */
class ItemClickListener extends View.OnClickListener{
	
	host:Dialog
	hostStyle:ShowActionSheetOptions
	hostIndex:number
	
	constructor(dialog:Dialog,style: ShowActionSheetOptions,index:number){
		super()
		this.host = dialog
		this.hostStyle = style
		this.hostIndex = index
	}
	
    override onClick(v?: View):void{
		
		const res : ShowActionSheetSuccess = {
			tapIndex: this.hostIndex
		}
		
		this.hostStyle.success?.(res)
DCloud-yyl's avatar
DCloud-yyl 已提交
252
		this.hostStyle.success = null
253
		this.hostStyle.complete?.(res)
DCloud-yyl's avatar
DCloud-yyl 已提交
254 255 256
		this.hostStyle.complete = null
		// 点击触发成功后,去掉fail
		this.hostStyle.fail = null
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
		this.host.dismiss()
		
    }
}

/**
 * 取消按钮点击事件
 */
class CancelClickListener extends View.OnClickListener{
	
	host:Dialog
	hostStyle:ShowActionSheetOptions
	
	constructor(dialog:Dialog,style: ShowActionSheetOptions){
		super()
		this.host = dialog
		this.hostStyle = style
	}
	
    override onClick(v?: View):void{
		
		let res = new UniError("uni-prompt",1002,"showActionSheet:fail cancel");
		
		this.hostStyle.fail?.(res)
DCloud-yyl's avatar
DCloud-yyl 已提交
281
		this.hostStyle.fail = null
282
		this.hostStyle.complete?.(res)
DCloud-yyl's avatar
DCloud-yyl 已提交
283
		this.hostStyle.complete = null
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
		this.host.dismiss()
		
    }
}


/**
 * 列表数据适配器
 */
class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder>{
	
	
	mItemList: Array<String>
	hostStyle:ShowActionSheetOptions
	hostDialog:Dialog
	
	constructor(dialog:Dialog ,style: ShowActionSheetOptions){
		super()
		this.hostDialog = dialog
		this.mItemList = style.itemList
		this.hostStyle = style
	}
	
	/**
	 * view 持有容器
	 */
	ViewHolder = class extends RecyclerView.ViewHolder {
		
		itemName:TextView;
		constructor(view:View) {
			super(view);
			itemName = view.findViewById(R.id.tvName) as TextView;
		}
	}
	
	
	override getItemViewType(position: Int): Int {
		if(hostStyle.title.isNullOrBlank()){
			// 无标题状态
			if(position == 0){
				return 1001
			}
			return 1002
		}else{
			// 有标题状态
			return 1002
		}
	}


    override onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {
		
		let view: View
		if(viewType == 1002){
			if(UTSAndroid.getAppDarkMode()){
				view =
DCloud-yyl's avatar
DCloud-yyl 已提交
343
				    LayoutInflater.from(parent.context).inflate(R.layout.uni_prompt_ac_recyclerview_layout_night, parent, false)
344 345
			}else{
				view =
DCloud-yyl's avatar
DCloud-yyl 已提交
346
				    LayoutInflater.from(parent.context).inflate(R.layout.uni_prompt_ac_recyclerview_layout, parent, false)
347 348 349 350
			}
		}else{
			if(UTSAndroid.getAppDarkMode()){
				view =
DCloud-yyl's avatar
DCloud-yyl 已提交
351
				    LayoutInflater.from(parent.context).inflate(R.layout.uni_prompt_ac_recyclerview_layout_top_night, parent, false)
352 353
			}else{
				view =
DCloud-yyl's avatar
DCloud-yyl 已提交
354
				    LayoutInflater.from(parent.context).inflate(R.layout.uni_prompt_ac_recyclerview_layout_top, parent, false)
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
			}
		}
		
        
        return ViewHolder(view)
    }

    override onBindViewHolder(holder: ViewHolder, position: Int) {
        let perContent: String = mItemList[position]
        holder.itemName.setText(perContent)
		if(this.hostStyle.itemColor != null && isValidColor(this.hostStyle.itemColor)){
			 holder.itemName.setTextColor(Color.parseColor(this.hostStyle.itemColor))
		}
		holder.itemName.setOnClickListener(new ItemClickListener(this.hostDialog,this.hostStyle,position));
    }

    override getItemCount(): Int {
        return mItemList.size
    }
	
}
/**
 * 工具方法,判断一个color 字符串是否合法
 */
function isValidColor(colorStr?:string):boolean{
	if(colorStr == null){
		return false
	}
	if(colorStr.length != 7){
		return false
	}
	if(!colorStr.startsWith("#")){
		return false
	}
	return true
}