Kotlin云头条技术点剖析(项目复习02)——用户协议页面

SplashActivity中的showTermServiceAgreementDialog() 也就是同意协议页面

没有同意用户协议 同意协议页面出现在当前页面供用户选择

kotlin 复制代码
private fun showTermServiceAgreementDialog() {
        TemServiceDialogFragment.show(supportFragmentManager) {
            DefaultPreferenceUtil.getInstance(this).setAcceptTermsServiceAgreement()
            requestPermission()
        }
    }

DefaultPreferenceUtil(偏好设置工具类)是否登录了,是否显示引导界面,用户Id

kotlin 复制代码
/**
 * 偏好设置工具类
 * 是否登录了,是否显示引导界面,用户Id
 */
class DefaultPreferenceUtil(context: Context) {
    private var context: Context
    private val preference: SharedPreferences

    init {
        //保存上下文
        this.context = context.applicationContext
        //这样写有内存泄漏
        //因为当前工具类不会马上释放
        //如果当前工具类引用了界面实例
        //当界面关闭后
        //因为界面对应在这里还有引用
        //所以会导致界面对象不会被释放
        //this.context = context
        //获取系统默认偏好设置,在设置界面保存的值就可以这样获取
        preference = PreferenceManager.getDefaultSharedPreferences(this.context)

        //自定义名称
//        preference = this.context.getSharedPreferences(NAME, Context.MODE_PRIVATE)
    }
    //region 用户协议
    /**
     * 设置同意了用户协议
     */
    fun setAcceptTermsServiceAgreement() {
        putBoolean(TERMS_SERVICE, true)
    }
    /**
     * 获取是否同意了用户条款
     *
     * @return
     */
    val isAcceptTermsServiceAgreement: Boolean
        get() = preference.getBoolean(TERMS_SERVICE, false)
    //endregion
    /**
     * 移动网络下是否播放
     *
     * @return 默认不能播放
     */
    val isMobileNetworkPlay: Boolean
        get() = preference.getBoolean(KEY_MOBILE_NETWORK_PLAY, false)
    //region 辅助方法
    private fun putBoolean(key: String, value: Boolean) {
        preference.edit().putBoolean(key, value).apply()
    }
    //endregion
    companion object {
        /**
         * 偏好设置文件名称
         */
        private const val NAME = "Yun_News"
        private const val TERMS_SERVICE = "TERMS_SERVICE"
        private const val KEY_MOBILE_NETWORK_PLAY = "mobile_network_play"
        private var instance: DefaultPreferenceUtil? = null
        /**
         * 获取偏好设置单例
         *
         * @param context
         * @return
         */
        @Synchronized
        fun getInstance(context: Context): DefaultPreferenceUtil {
            if (instance == null) {
                instance = DefaultPreferenceUtil(context)
            }
            return instance!!
        }
    }
}

BaseDialogFragment 父类 与BaseActivity作用一样

kotlin 复制代码
abstract class BaseDialogFragment:DialogFragment() {
    //找控件
    protected open fun initViews(){}
    //设置数据
    protected open fun initDatum(){}
    //设置监听器
    protected open fun initListeners(){}
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        //获取view
        val view = getLayoutView(inflater, container, savedInstanceState)
        return view
    }
    //重写子类
    open abstract fun getLayoutView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View?
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViews()
        initDatum()
        initListeners()
    }
}

TemServiceDialogFragment

kotlin 复制代码
class TemServiceDialogFragment:BaseViewModelDialogFragment<FragmentDialogTermServiceBinding>() {
    private lateinit var onAgreementClickListener: View.OnClickListener


    @SuppressLint("SuspiciousIndentation")
    override fun initViews() {
        super.initViews()
        //点击窗外不能被关闭
        isCancelable = false

        //设置文本中超链接的颜色
        SuperTextUtil.setLinkColor(binding.content1,getColor(requireContext(),R.color.link))


    }

    override fun initDatum() {
        super.initDatum()
        //z在fragment中的数据函数中实现对有特殊格式的文字进行解析
        val content1 = Html.fromHtml(getString(R.string.term_service_privacy_content))
        binding.content1.text = content1
    }

    //回调调用这个接口
    override fun initListeners() {
        super.initListeners()
        binding.primary.setOnClickListener {
            dismiss()//点击关闭按钮 避免内存泄漏
            onAgreementClickListener.onClick(it)
        }
        binding.disagree.setOnClickListener {
            dismiss()
            //杀死当前应用
            SuperProcessUtil.killApp()
        }
    }
    //onResume的意思是一直循环 让窗口扩展到一定的长和宽
    override fun onResume() {
        super.onResume()
        val params: ViewGroup.LayoutParams = dialog!!.window!!.attributes
        params.width = ((ScreenUtil.getScreenWith(requireContext()) * 0.9).toInt())
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT
        dialog!!.window!!.attributes = params as WindowManager.LayoutParams
    }


//companion object 是 Kotlin 中实现类级别静态成员的方式
    companion object {
        // 显示对话框
        @SuppressLint("SuspiciousIndentation")
        fun show(supportFragmentManager: FragmentManager,onAgreementClickListener:View.OnClickListener) {
         val dialogFragment = TemServiceDialogFragment()
            dialogFragment.onAgreementClickListener= onAgreementClickListener
            dialogFragment.show(supportFragmentManager,"TemServiceDialogFragment")
        }
    }
}

SuperTextUtil(设置富文本,超链接颜色)

kotlin 复制代码
object SuperTextUtil {

    //设置富文本,超链接颜色
    fun setLinkColor(view:TextView,color:Int){
        //设置后才可以点击
        view.setMovementMethod(LinkMovementMethod.getInstance())
        //链接的颜色
        view.setLinkTextColor(color)
    }
}

SuperProcessUtil (杀死当前应用)

kotlin 复制代码
object SuperProcessUtil {
    //杀死当前应用
    fun killApp(){
        Process.killProcess(Process.myPid())
    }
}

BaseViewModelDialogFragment 作用跟BaseViewActivity一模一样 为了减少生成setconview 减少重复的代码 提高可读性

kotlin 复制代码
abstract class BaseViewModelDialogFragment<VB : ViewBinding> : BaseCommonDialogFragment() {
    private var _binding: VB? = null
    protected val binding get() = _binding!!

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        _binding = ReflectUtil.newViewBinding(layoutInflater, this.javaClass)
    }

    override fun getLayoutView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return binding.root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}
相关推荐
xufengzhu1 小时前
Python库PyMySQL的使用指南
开发语言·python·pip
z落落9 小时前
C# 泛型方法(原理、类型推断、多泛型参数)+泛型效率(普通类型 VS Object装箱 VS 泛型)
开发语言·c#
L_09079 小时前
【C++】异常
开发语言·c++
世辰辰辰10 小时前
批量修改图片/文本名子
开发语言·python·批量修改文件名
z落落12 小时前
C# 四种特殊类:抽象类、密封类、静态类、部分类
开发语言·c#
VidDown12 小时前
Webhook 调试器:让第三方回调“原形毕露”
java·开发语言·javascript·编辑器·postman
装不满的克莱因瓶12 小时前
基于 OpenResty 扩展开发实现动态服务注册与发现能力
java·开发语言·架构·openresty
weixin_5231853213 小时前
Java基础知识总结(四):引用数据类型与参数传递机制
java·开发语言·python
Nayxxu13 小时前
Claude API 生产稳定性设计:超时、降级、备用模型和告警怎么做
开发语言·php
王cb13 小时前
WinRT Server and Client c#
开发语言·c#