@@ -7,13 +7,15 @@ import android.content.Intent
77import android.content.Intent.ACTION_SEND
88import android.content.pm.PackageManager
99import android.content.res.ColorStateList
10+ import android.content.res.Configuration
1011import android.graphics.Color
1112import android.graphics.drawable.ColorDrawable
1213import android.net.Uri
1314import android.os.Build
1415import android.os.Bundle
1516import android.util.Log
1617import android.view.*
18+ import android.view.View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
1719import android.widget.Toast
1820import androidx.activity.result.ActivityResultLauncher
1921import androidx.activity.viewModels
@@ -48,7 +50,6 @@ import kotlin.collections.ArrayList
4850@AndroidEntryPoint
4951class MainActivity : AppCompatActivity () {
5052
51- private lateinit var pickImageLauncher: ActivityResultLauncher <String >
5253 private lateinit var pickIconLauncher: ActivityResultLauncher <String >
5354 private val viewModel: MainViewModel by viewModels()
5455
@@ -143,6 +144,15 @@ class MainActivity : AppCompatActivity() {
143144 SaveImageBSDialogFragment .safetyHide(this @MainActivity.supportFragmentManager)
144145 }
145146
147+ override fun onWindowFocusChanged (hasFocus : Boolean ) {
148+ super .onWindowFocusChanged(hasFocus)
149+ if (hasFocus){
150+ hideSystemUI()
151+ } else {
152+ showSystemUI()
153+ }
154+ }
155+
146156 private fun hideSystemUI () {
147157 // Enables regular immersive mode.
148158 // For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
@@ -185,10 +195,6 @@ class MainActivity : AppCompatActivity() {
185195 }
186196
187197 private fun registerResultCallback () {
188- pickImageLauncher =
189- registerForActivityResult(MultiPickContract ()) { uri: List <Uri ?>? ->
190- handleActivityResult(REQ_CODE_PICK_IMAGE , uri)
191- }
192198 pickIconLauncher =
193199 registerForActivityResult(PickImageContract ()) { uri: Uri ? ->
194200 handleActivityResult(REQ_PICK_ICON , listOf (uri))
@@ -250,6 +256,7 @@ class MainActivity : AppCompatActivity() {
250256 if (it == null ) {
251257 return @observe
252258 }
259+ Log .i(" initObserver" , " $it " )
253260 launchView.post {
254261 launchView.ivPhoto.config = it
255262 }
@@ -308,9 +315,12 @@ class MainActivity : AppCompatActivity() {
308315 ((launchView.ivPhoto.background as ? ColorDrawable )?.color ? : Color .BLACK ).toColor(
309316 bgColor
310317 ) {
311- launchView.rvPanel.setBackgroundColor(it.animatedValue as Int )
312- launchView.setBackgroundColor(it.animatedValue as Int )
313- setStatusBarColor(it.animatedValue as Int )
318+ val c = it.animatedValue as Int
319+ if (launchView.isEdit()) {
320+ launchView.rvPanel.setBackgroundColor(it.animatedValue as Int )
321+ setActivityBackground(c)
322+ setStatusBarColor(c, isInEditMode = true )
323+ }
314324 }
315325 funcAdapter.textColor.toColor(titleTextColor) {
316326 val c = it.animatedValue as Int
@@ -334,6 +344,8 @@ class MainActivity : AppCompatActivity() {
334344
335345 @SuppressLint(" ClickableViewAccessibility" )
336346 private fun initView () {
347+ setActivityBackground(ContextCompat .getColor(this , R .color.md_theme_dark_background))
348+ setStatusBarColor(ContextCompat .getColor(this , R .color.md_theme_dark_background), false )
337349 // prepare MotionLayout
338350 launchView.setListener {
339351 onModeChange { _, newMode ->
@@ -538,9 +550,25 @@ class MainActivity : AppCompatActivity() {
538550 }
539551 }
540552
541- private fun setStatusBarColor (color : Int ) {
553+ private fun setStatusBarColor (color : Int , isInEditMode : Boolean ) {
542554 window.clearFlags(WindowManager .LayoutParams .FLAG_TRANSLUCENT_STATUS )
543555 window.addFlags(WindowManager .LayoutParams .FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS )
556+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .R ) {
557+ val systemUiAppearance = if (isInEditMode && this .isNight()) {
558+ WindowInsetsController .APPEARANCE_LIGHT_STATUS_BARS
559+ } else {
560+ 0
561+ }
562+ window.insetsController?.setSystemBarsAppearance(systemUiAppearance,
563+ WindowInsetsController .APPEARANCE_LIGHT_STATUS_BARS )
564+ } else {
565+ val systemUiVisibilityFlags = if (! isInEditMode && ! this .isNight()) {
566+ window.decorView.systemUiVisibility or SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
567+ } else {
568+ window.decorView.systemUiVisibility and SYSTEM_UI_FLAG_LIGHT_STATUS_BAR .inv ()
569+ }
570+ window.decorView.systemUiVisibility = systemUiVisibilityFlags
571+ }
544572 window.statusBarColor = color
545573 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
546574 window.findViewById<View >(android.R .id.content)?.foreground = null
@@ -583,9 +611,6 @@ class MainActivity : AppCompatActivity() {
583611 val mime = " image/*"
584612 val result = kotlin.runCatching {
585613 when (requestCode) {
586- REQ_CODE_PICK_IMAGE -> {
587- pickImageLauncher.launch(mime)
588- }
589614 REQ_PICK_ICON -> {
590615 pickIconLauncher.launch(mime)
591616 }
@@ -698,15 +723,19 @@ class MainActivity : AppCompatActivity() {
698723 launchView.ivPhoto.reset()
699724 bgTransformAnimator?.cancel()
700725 (launchView.background as ? ColorDrawable ? )?.color?.toColor(
701- this .colorBackground
726+ ContextCompat .getColor( this , R .color.md_theme_dark_background)
702727 ) {
703728 val c = it.animatedValue as Int
704- setStatusBarColor (c)
705- launchView.setBackgroundColor(c )
729+ setActivityBackground (c)
730+ setStatusBarColor(c, isInEditMode = false )
706731 }
707732 hideDetailPanel()
708733 }
709734
735+ private fun setActivityBackground (color : Int ){
736+ (launchView.parent as ? View ? )?.setBackgroundColor(color)
737+ }
738+
710739 private fun selectTab (index : Int ) {
711740 launchView.tabLayout.getTabAt(index).let {
712741 launchView.tabLayout.selectTab(it)
0 commit comments