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()) { v = LayoutInflater.from(this.hostActivity).inflate(R.layout.action_sheet_night, null) } else { v = LayoutInflater.from(this.hostActivity).inflate(R.layout.action_sheet, null) } 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() { this.alertDialog.window!.attributes.windowAnimations = R.style.DialogAnimations_SmileWindow 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{ mItemList : Array 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 = LayoutInflater.from(parent.context).inflate(R.layout.recyclerview_layout_night, parent, false) } else { view = LayoutInflater.from(parent.context).inflate(R.layout.recyclerview_layout, parent, false) } 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 } }