Skip to content

Commit fb88f83

Browse files
committed
Apply OmegaWindowBackground for DialogFragment
1 parent 6cc5af4 commit fb88f83

File tree

4 files changed

+59
-41
lines changed

4 files changed

+59
-41
lines changed
Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package com.omega_r.base.annotations
22

3+
import android.graphics.drawable.ColorDrawable
4+
import android.view.Window
35
import androidx.annotation.AttrRes
6+
import androidx.annotation.ColorRes
47
import androidx.annotation.DrawableRes
8+
import com.omega_r.libs.extensions.context.getColorByAttribute
9+
import com.omega_r.libs.extensions.context.getCompatColor
10+
import com.omega_r.libs.extensions.context.getCompatDrawable
511

612
/**
713
* Created by Anton Knyazev on 2019-09-23.
@@ -10,5 +16,22 @@ import androidx.annotation.DrawableRes
1016
@Retention(AnnotationRetention.RUNTIME)
1117
annotation class OmegaWindowBackground(
1218
@DrawableRes val drawableRes: Int = 0,
13-
@AttrRes val colorAttrRes: Int = 0
14-
)
19+
@AttrRes val colorAttrRes: Int = 0,
20+
@ColorRes val colorRes: Int = 0
21+
) {
22+
fun apply(window: Window) {
23+
with(window.context) {
24+
when {
25+
drawableRes > 0 -> {
26+
window.setBackgroundDrawable(getCompatDrawable(drawableRes))
27+
}
28+
colorRes > 0 -> {
29+
window.setBackgroundDrawable(ColorDrawable(getCompatColor(colorRes)))
30+
}
31+
colorAttrRes > 0 -> {
32+
window.setBackgroundDrawable(ColorDrawable(getColorByAttribute(colorAttrRes)))
33+
}
34+
}
35+
}
36+
}
37+
}

core/src/main/java/com/omega_r/base/components/OmegaActivity.kt

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import com.omega_r.base.mvp.model.Action
2525
import com.omega_r.base.mvp.views.findAnnotation
2626
import com.omega_r.base.tools.DialogManager
2727
import com.omega_r.libs.extensions.context.getColorByAttribute
28+
import com.omega_r.libs.extensions.context.getCompatColor
29+
import com.omega_r.libs.extensions.context.getCompatDrawable
2830
import com.omega_r.libs.omegatypes.Text
2931
import com.omegar.libs.omegalaunchers.ActivityLauncher
3032
import com.omegar.libs.omegalaunchers.BaseIntentLauncher
@@ -73,16 +75,7 @@ abstract class OmegaActivity : MvpAppCompatActivity(), OmegaComponent {
7375
setTitle(it.resId)
7476
}
7577
is OmegaWindowBackground -> {
76-
if (it.drawableRes > 0) {
77-
window.setBackgroundDrawable(
78-
ContextCompat.getDrawable(this, it.drawableRes)
79-
)
80-
} else if (it.colorAttrRes > 0) {
81-
window.setBackgroundDrawable(
82-
ColorDrawable(getColorByAttribute(it.colorAttrRes))
83-
)
84-
}
85-
78+
it.apply(window)
8679
}
8780
}
8881
}
@@ -296,7 +289,7 @@ abstract class OmegaActivity : MvpAppCompatActivity(), OmegaComponent {
296289
finish()
297290
}
298291

299-
protected fun <T: View> bindAndSetClick(@IdRes res: Int, block: () -> Unit): Lazy<T> {
292+
protected fun <T : View> bindAndSetClick(@IdRes res: Int, block: () -> Unit): Lazy<T> {
300293
return bind(res) {
301294
setOnClickListener(this, block)
302295
}

core/src/main/java/com/omega_r/base/components/OmegaBottomSheetDialogFragment.kt

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package com.omega_r.base.components
22

3+
import android.annotation.SuppressLint
34
import android.app.Dialog
45
import android.content.DialogInterface
56
import android.content.Intent
67
import android.os.Bundle
78
import android.view.*
89
import androidx.annotation.*
910
import androidx.recyclerview.widget.RecyclerView
10-
import com.omega_r.base.annotations.OmegaClickViews
11-
import com.omega_r.base.annotations.OmegaContentView
12-
import com.omega_r.base.annotations.OmegaMenu
13-
import com.omega_r.base.annotations.OmegaTheme
11+
import com.omega_r.base.annotations.*
1412
import com.omega_r.base.binders.IdHolder
1513
import com.omega_r.base.binders.managers.ResettableBindersManager
1614
import com.omega_r.base.clickers.ClickManager
@@ -104,21 +102,24 @@ abstract class OmegaBottomSheetDialogFragment : MvpBottomSheetDialogFragment(),
104102
}
105103
}
106104

105+
@SuppressLint("RestrictedApi")
106+
override fun setupDialog(dialog: Dialog, style: Int) {
107+
super.setupDialog(dialog, style)
108+
this::class.findAnnotation<OmegaWindowBackground>()?.apply(dialog.window!!)
109+
}
110+
107111
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
108112
val contentView = this::class.findAnnotation<OmegaContentView>()
109-
val view = if (contentView != null) {
110-
var themedInflater = inflater
111-
val theme = this::class.findAnnotation<OmegaTheme>()
112-
theme?.let {
113-
val contextThemeWrapper = ContextThemeWrapper(activity, theme.resId)
114-
themedInflater = inflater.cloneInContext(contextThemeWrapper)
115-
}
113+
114+
val themedInflater = this::class.findAnnotation<OmegaTheme>()?.let {
115+
inflater.cloneInContext(ContextThemeWrapper(activity, it.resId))
116+
} ?: inflater
117+
118+
return if (contentView != null) {
116119
themedInflater.inflate(contentView.layoutRes, container, false)
117120
} else {
118-
super.onCreateView(inflater, container, savedInstanceState)
121+
super.onCreateView(themedInflater, container, savedInstanceState)
119122
}
120-
121-
return view
122123
}
123124

124125
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

core/src/main/java/com/omega_r/base/components/OmegaDialogFragment.kt

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package com.omega_r.base.components
22

3+
import android.annotation.SuppressLint
34
import android.app.Dialog
45
import android.content.DialogInterface
56
import android.content.Intent
67
import android.os.Bundle
78
import android.view.*
89
import androidx.annotation.*
910
import androidx.recyclerview.widget.RecyclerView
10-
import com.omega_r.base.annotations.OmegaClickViews
11-
import com.omega_r.base.annotations.OmegaContentView
12-
import com.omega_r.base.annotations.OmegaMenu
13-
import com.omega_r.base.annotations.OmegaTheme
11+
import com.omega_r.base.annotations.*
1412
import com.omega_r.base.binders.IdHolder
1513
import com.omega_r.base.binders.managers.ResettableBindersManager
1614
import com.omega_r.base.clickers.ClickManager
@@ -108,21 +106,24 @@ abstract class OmegaDialogFragment : MvpAppCompatDialogFragment(), OmegaComponen
108106
}
109107
}
110108

109+
@SuppressLint("RestrictedApi")
110+
override fun setupDialog(dialog: Dialog, style: Int) {
111+
super.setupDialog(dialog, style)
112+
this::class.findAnnotation<OmegaWindowBackground>()?.apply(dialog.window!!)
113+
}
114+
111115
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
112116
val contentView = this::class.findAnnotation<OmegaContentView>()
113-
val view = if (contentView != null) {
114-
var themedInflater = inflater
115-
val theme = this::class.findAnnotation<OmegaTheme>()
116-
theme?.let {
117-
val contextThemeWrapper = ContextThemeWrapper(activity, theme.resId)
118-
themedInflater = inflater.cloneInContext(contextThemeWrapper)
119-
}
117+
118+
val themedInflater = this::class.findAnnotation<OmegaTheme>()?.let {
119+
inflater.cloneInContext(ContextThemeWrapper(activity, it.resId))
120+
} ?: inflater
121+
122+
return if (contentView != null) {
120123
themedInflater.inflate(contentView.layoutRes, container, false)
121124
} else {
122-
super.onCreateView(inflater, container, savedInstanceState)
125+
super.onCreateView(themedInflater, container, savedInstanceState)
123126
}
124-
125-
return view
126127
}
127128

128129
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

0 commit comments

Comments
 (0)