11package com.yenaly.han1meviewer.ui.fragment.video
22
33import android.app.Dialog
4+ import android.os.Build
45import android.os.Bundle
56import android.view.Gravity
67import android.view.LayoutInflater
8+ import android.view.View
9+ import android.view.ViewGroup
10+ import android.view.WindowManager
711import androidx.annotation.OptIn
812import androidx.fragment.app.viewModels
913import androidx.lifecycle.lifecycleScope
1014import androidx.recyclerview.widget.LinearLayoutManager
1115import com.google.android.material.badge.BadgeDrawable
1216import com.google.android.material.badge.BadgeUtils
1317import com.google.android.material.badge.ExperimentalBadgeUtils
18+ import com.google.android.material.bottomsheet.BottomSheetBehavior
19+ import com.google.android.material.bottomsheet.BottomSheetDialog
1420import com.yenaly.han1meviewer.COMMENT_ID
1521import com.yenaly.han1meviewer.R
1622import com.yenaly.han1meviewer.databinding.PopUpFragmentChildCommentBinding
@@ -19,7 +25,6 @@ import com.yenaly.han1meviewer.ui.adapter.VideoCommentRvAdapter
1925import com.yenaly.han1meviewer.ui.viewmodel.CommentViewModel
2026import com.yenaly.han1meviewer.util.setGravity
2127import com.yenaly.yenaly_libs.base.YenalyBottomSheetDialogFragment
22- import com.yenaly.yenaly_libs.utils.appScreenHeight
2328import com.yenaly.yenaly_libs.utils.arguments
2429import com.yenaly.yenaly_libs.utils.dp
2530import com.yenaly.yenaly_libs.utils.showShortToast
@@ -47,10 +52,10 @@ class ChildCommentPopupFragment :
4752 override fun initData (savedInstanceState : Bundle ? , dialog : Dialog ) {
4853 if (commentId == null ) dialog.dismiss()
4954
50- binding.root.minimumHeight = appScreenHeight / 2
55+ // binding.root.minimumHeight = appScreenHeight / 2
5156 binding.rvReply.layoutManager = LinearLayoutManager (context)
5257 binding.rvReply.adapter = replyAdapter
53-
58+ binding.rvReplyContainer.layoutParams.height = getWindowHeight() / 2
5459 viewModel.getCommentReply(commentId!! )
5560
5661 lifecycleScope.launch {
@@ -63,7 +68,9 @@ class ChildCommentPopupFragment :
6368
6469 is WebsiteState .Loading -> Unit
6570
66- is WebsiteState .Success -> Unit
71+ is WebsiteState .Success -> {
72+ binding.rvReplyContainer.layoutParams.height = ViewGroup .LayoutParams .WRAP_CONTENT
73+ }
6774 }
6875 }
6976 }
@@ -80,6 +87,7 @@ class ChildCommentPopupFragment :
8087 when (state) {
8188 is WebsiteState .Error -> {
8289 showShortToast(R .string.send_failed)
90+ replyAdapter.replyPopup?.enableSendButton()
8391 }
8492
8593 is WebsiteState .Loading -> {
@@ -88,6 +96,7 @@ class ChildCommentPopupFragment :
8896
8997 is WebsiteState .Success -> {
9098 showShortToast(R .string.send_success)
99+ replyAdapter.replyPopup?.enableSendButton()
91100 viewModel.getCommentReply(commentId!! )
92101 replyAdapter.replyPopup?.dismiss()
93102 }
@@ -108,7 +117,40 @@ class ChildCommentPopupFragment :
108117 }
109118 }
110119 }
111-
120+ override fun onStart () {
121+ super .onStart()
122+ dialog?.window?.setSoftInputMode(WindowManager .LayoutParams .SOFT_INPUT_ADJUST_PAN )
123+ val bottomSheet = dialog?.findViewById<View >(com.google.android.material.R .id.design_bottom_sheet)
124+ bottomSheet?.let { sheet ->
125+ val screenHeight = getWindowHeight()
126+ sheet.minimumHeight = screenHeight / 2
127+ val behavior = BottomSheetBehavior .from(sheet)
128+ behavior.isFitToContents = false
129+ behavior.peekHeight = screenHeight / 2
130+ behavior.state = BottomSheetBehavior .STATE_COLLAPSED
131+ }
132+ }
133+ private fun getWindowHeight (): Int {
134+ val window = dialog?.window ? : return resources.displayMetrics.heightPixels
135+ return if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .R ) {
136+ window.windowManager.currentWindowMetrics.bounds.height()
137+ } else {
138+ @Suppress(" DEPRECATION" )
139+ window.windowManager.defaultDisplay.height
140+ }
141+ }
142+ override fun onCreateDialog (savedInstanceState : Bundle ? ): Dialog {
143+ val dialog = super .onCreateDialog(savedInstanceState) as BottomSheetDialog
144+ dialog.window?.apply {
145+ addFlags(WindowManager .LayoutParams .FLAG_LAYOUT_NO_LIMITS )
146+ @Suppress(" DEPRECATION" )
147+ addFlags(WindowManager .LayoutParams .FLAG_TRANSLUCENT_NAVIGATION )
148+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) {
149+ isNavigationBarContrastEnforced = false
150+ }
151+ }
152+ return dialog
153+ }
112154 @OptIn(ExperimentalBadgeUtils ::class )
113155 private fun attachRedDotCount (count : Int ) {
114156 val badgeDrawable = BadgeDrawable .create(requireContext())
0 commit comments