using Android.Views; using AndroidX.Fragment.App; using System.Application.UI.Internals; using Fragment = AndroidX.Fragment.App.Fragment; namespace System.Application.UI.Fragments { /// /// 片段(Fragment) /// public interface IFragment : View.IOnClickListener { /// FragmentActivity? Activity { get; } Fragment Fragment { get; } View View { get; set; } /// /// 当前 指定布局资源 /// int? LayoutResource { get; } void OnCreateView(View view); /// /// 控件的点击事件 /// 根据返回值决定是否不调用 中的 /// 默认返回值 /// /// /// new bool OnClick(View view); /// /// 是否在显示当前 Fragment 时启用亮色状态栏 /// 默认值: /// 亮色: /// 暗色: /// bool? IsLightStatusBar { get; } protected static void ChangeStatusBar(IFragment f) { var isLightStatusBar = f.IsLightStatusBar; if (isLightStatusBar.HasValue) { var activity = f.Activity; if (activity != null) { if (isLightStatusBar.Value && DarkModeUtil.IsDarkModeQ) { isLightStatusBar = false; } StatusBarUtil.SetWindowLightStatusBar(activity, isLightStatusBar.Value); } } } protected static View? OnCreateView(IFragment f, LayoutInflater inflater, ViewGroup container) { if (!f.LayoutResource.HasValue) return null; var view = inflater.Inflate(f.LayoutResource.Value, container, false); if (view == null) throw new ArgumentOutOfRangeException(nameof(view), f.LayoutResource.Value, null); f.View = view; if (f is IToolbar toolbar) { toolbar.BindingToolbar(); } f.OnCreateView(view); return view; } protected static void OnClick(IFragment f, View? view) { if (view != null) { var stopCall = f.OnClick(view); if (!stopCall && f.Activity is View.IOnClickListener listener) { listener.OnClick(view); } } } protected static T? FindViewById(IFragment f, int id) where T : View => f.View.FindViewById(id); protected static View? FindViewById(IFragment f, int id) => f.View.FindViewById(id); } partial class BaseFragment : IFragment { Fragment IFragment.Fragment => this; int? IFragment.LayoutResource => LayoutResource; void IFragment.OnCreateView(View view) => OnCreateView(view); bool IFragment.OnClick(View view) => OnClick(view); bool? IFragment.IsLightStatusBar => IsLightStatusBar; } partial class BaseDialogFragment : IFragment { Fragment IFragment.Fragment => this; int? IFragment.LayoutResource => LayoutResource; void IFragment.OnCreateView(View view) => OnCreateView(view); bool IFragment.OnClick(View view) => OnClick(view); bool? IFragment.IsLightStatusBar => IsLightStatusBar; } partial class BaseBottomSheetDialogFragment : IFragment { Fragment IFragment.Fragment => this; int? IFragment.LayoutResource => LayoutResource; void IFragment.OnCreateView(View view) => OnCreateView(view); bool IFragment.OnClick(View view) => OnClick(view); bool? IFragment.IsLightStatusBar => IsLightStatusBar; } }