showActionSheet.uts 7.0 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
import AlertDialog from 'android.app.AlertDialog';
import Activity from 'android.app.Activity';
import Gravity from 'android.view.Gravity';
import { UTSAndroid } from "io.dcloud.uts";

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

import R from 'io.dcloud.uts.image.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';



// import { ShowActionSheetOptions, ShowActionSheetSuccess,ShowActionSheetFail} from '../interface.uts'


export type ShowActionSheetOptions = {
	/**
	 * 菜单标题
	 */
	title : string | null,
	/**
	 * 警示文案(同菜单标题, app无效)
	 */
	alertText : string | null,
	/**
	 * 按钮的文字数组
	 */
	itemList : string[],
	/**
	 * 按钮的文字颜色,字符串格式(iOS默认为系统控件颜色)
	 */
	itemColor : string | null,
	/**
	 * 接口调用成功的回调函数
	 */
	success : ((result : ShowActionSheetSuccess) => void) | null,
	/**
	 * 接口调用失败的回调函数
	 */
	fail : ((result : ShowActionSheetFail) => void) | null,
	/**
	 * 接口调用结束的回调函数(调用成功、失败都会执行)
	 */
	complete : ((result : any) => void) | null
}

export type ShowActionSheetSuccess = {
	tapIndex : number
}
export type ShowActionSheetFail = {
	errCode : number,
	errMsg : string,
	errSubject : string
}

let uniActionSheet : UniActionSheet | null = null

/**
 * 弹出功能入口
 */
export function actionSheetImpl(activity : Activity, style : ShowActionSheetOptions) {
	if (uniActionSheet != null) {
		uniActionSheet?.dismiss()
	}
	uniActionSheet = new UniActionSheet(activity, style)
	uniActionSheet?.show()
}

export function closeActionSheet() {
	if (uniActionSheet != null) {
		uniActionSheet!.dismiss()
	}
}


/**
 * UniActionSheet 
 * 功能实现
 */
export class UniActionSheet {

	/**
	 * 宿主activity
	 */
	hostActivity : Activity
	/**
	 * 宿主样式
	 */
	hostStyle : ShowActionSheetOptions
	/**
	 * 标题组件
	 */
	title : AppCompatTextView
	/**
	 * 取消按钮
	 */
	cancel : AppCompatTextView
	/**
	 * 列表 recycler 
	 */
	myRecyclerView : RecyclerView
	/**
	 * 列表 recycler Adapter
	 */
	recyclerviewAdapter : ItemAdapter
	/**
	 * title 组件
	 */
	lineTitle : LinearLayoutCompat
	/**
	 * 内容组件
	 */
	lineContent : LinearLayoutCompat
	/**
	 * 取消组件
	 */
	lineCancel : LinearLayoutCompat
	/**
	 * 构建后的dialog
	 */
	alertDialog : AlertDialog

	constructor(activity : Activity, style : ShowActionSheetOptions) {
		this.hostActivity = activity
		this.hostStyle = style

		let adb = AlertDialog.Builder(this.hostActivity);

		let v : View
		if (UTSAndroid.getAppDarkMode()) {
DCloud-yyl's avatar
DCloud-yyl 已提交
141
			v = LayoutInflater.from(this.hostActivity).inflate(R.layout.uni_media_action_sheet_night, null)
142
		} else {
DCloud-yyl's avatar
DCloud-yyl 已提交
143
			v = LayoutInflater.from(this.hostActivity).inflate(R.layout.uni_media_action_sheet, null)
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
		}


		this.title = v.findViewById(R.id.tvTitle)
		this.cancel = v.findViewById(R.id.tvCancelAction)
		this.lineTitle = v.findViewById(R.id.line_title)
		this.lineContent = v.findViewById(R.id.line_content)
		this.lineCancel = v.findViewById(R.id.line_cancel)
		this.myRecyclerView = v.findViewById(R.id.myRecyclerview)
		this.myRecyclerView.layoutManager = LinearLayoutManager(this.hostActivity)


		if (style.title != null) {
			this.lineTitle.setVisibility(View.VISIBLE)
			this.title.setText(style.title)
		}

		// 构造最终的展示组件
		adb.setView(v)
		this.alertDialog = adb.create()
		this.cancel.setOnClickListener(new CancelClickListener(this.alertDialog, this.hostStyle));

		this.recyclerviewAdapter = new ItemAdapter(this.alertDialog, style)
		this.myRecyclerView.adapter = this.recyclerviewAdapter
		this.recyclerviewAdapter.notifyDataSetChanged()
	}

	/**
	 * 弹出actionsheet
	 */
	show() {
DCloud-yyl's avatar
DCloud-yyl 已提交
175
		// this.alertDialog.window!.attributes.windowAnimations = R.style.DialogAnimations_SmileWindow
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 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 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
		this.alertDialog.setCancelable(true)
		this.alertDialog.window!.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM)
		this.alertDialog.window!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
		this.alertDialog.show()
	}


	dismiss() {
		this.alertDialog.dismiss()
	}

}

/**
 * 列表选项点击事件监听
 */
class ItemClickListener extends View.OnClickListener {

	host : AlertDialog
	hostStyle : ShowActionSheetOptions
	hostIndex : number

	constructor(dialog : AlertDialog, style : ShowActionSheetOptions, index : number) {
		super()
		this.host = dialog
		this.hostStyle = style
		this.hostIndex = index
	}

	override onClick(v ?: View) : void {

		var res : ShowActionSheetSuccess = {
			tapIndex: this.hostIndex
		}

		this.hostStyle.success?.(res)
		this.hostStyle.complete?.(res)

		this.host.dismiss()

	}
}

/**
 * 取消按钮点击事件
 */
class CancelClickListener extends View.OnClickListener {

	host : AlertDialog
	hostStyle : ShowActionSheetOptions

	constructor(dialog : AlertDialog, style : ShowActionSheetOptions) {
		super()
		this.host = dialog
		this.hostStyle = style
	}

	override onClick(v ?: View) : void {

		const res : ShowActionSheetFail = {
			errCode: 0,
			errSubject: 'uni-prompt',
			errMsg: 'showActionSheet:fail cancel'
		}
		this.hostStyle.fail?.(res)
		this.hostStyle.complete?.(res)

		this.host.dismiss()

	}
}


/**
 * 列表数据适配器
 */
class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder>{


	mItemList : Array<String>
	hostStyle : ShowActionSheetOptions
	hostDialog : AlertDialog

	constructor(dialog : AlertDialog, 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 onCreateViewHolder(
		parent : ViewGroup,
		viewType : Int
	) : ViewHolder {

		let view : View
		if (UTSAndroid.getAppDarkMode()) {
			view =
DCloud-yyl's avatar
DCloud-yyl 已提交
286
				LayoutInflater.from(parent.context).inflate(R.layout.uni_media_recyclerview_layout_night, parent, false)
287 288
		} else {
			view =
DCloud-yyl's avatar
DCloud-yyl 已提交
289
				LayoutInflater.from(parent.context).inflate(R.layout.uni_media_recyclerview_layout, parent, false)
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
		}

		return ViewHolder(view)
	}

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

	override getItemCount() : Int {
		return mItemList.size
	}

}