Convert Theme to Kotlin

pull/1400/head
Alex Baker 3 years ago
parent 73d536a0c3
commit 2eac8dd20c

@ -81,7 +81,8 @@ abstract class BaseDateTimePicker : BottomSheetDialogFragment() {
.findViewById<CoordinatorLayout>(com.google.android.material.R.id.coordinator)
val containerLayout =
dialog.findViewById<FrameLayout>(com.google.android.material.R.id.container)
val buttons = theme.getLayoutInflater(context).inflate(R.layout.dialog_date_time_picker_buttons, null)
val buttons = theme.getLayoutInflater(requireContext())
.inflate(R.layout.dialog_date_time_picker_buttons, null)
buttons.findViewById<View>(R.id.cancel_button).setOnClickListener { dismiss() }
buttons.findViewById<View>(R.id.ok_button).setOnClickListener { sendSelected() }
buttons.layoutParams = FrameLayout.LayoutParams(

@ -90,7 +90,7 @@ class DateTimePicker : BaseDateTimePicker() {
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
binding = DialogDateTimePickerBinding.inflate(theme.getLayoutInflater(context))
binding = DialogDateTimePickerBinding.inflate(theme.getLayoutInflater(requireContext()))
setupShortcutsAndCalendar()
ButterKnife.bind(this, binding.root)
binding.shortcuts.nextWeekButton.text =

@ -69,7 +69,7 @@ class StartDatePicker : BaseDateTimePicker() {
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
binding = DialogStartDatePickerBinding.inflate(theme.getLayoutInflater(context))
binding = DialogStartDatePickerBinding.inflate(theme.getLayoutInflater(requireContext()))
setupShortcutsAndCalendar()
ButterKnife.bind(this, binding.root)
binding.calendarView.setOnDateChangeListener { _, y, m, d ->

@ -1,64 +0,0 @@
package org.tasks.themes;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.PixelFormat;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import javax.inject.Inject;
import org.tasks.R;
public class Theme {
private final ThemeBase themeBase;
private final ThemeColor themeColor;
private final ThemeAccent themeAccent;
@Inject
public Theme(ThemeBase themeBase, ThemeColor themeColor, ThemeAccent themeAccent) {
this.themeBase = themeBase;
this.themeColor = themeColor;
this.themeAccent = themeAccent;
}
public Theme withThemeColor(ThemeColor themeColor) {
return new Theme(themeBase, themeColor, themeAccent);
}
public ThemeBase getThemeBase() {
return themeBase;
}
public ThemeColor getThemeColor() {
return themeColor;
}
public LayoutInflater getLayoutInflater(Context context) {
return (LayoutInflater) wrap(context).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void applyThemeAndStatusBarColor(Activity activity) {
applyTheme(activity);
themeColor.applyToSystemBars(activity);
themeColor.applyTaskDescription(activity, activity.getString(R.string.app_name));
}
public void applyTheme(Activity activity) {
themeBase.set(activity);
applyToContext(activity);
activity.getWindow().setFormat(PixelFormat.RGBA_8888);
}
public void applyToContext(Context context) {
Resources.Theme theme = context.getTheme();
themeColor.applyStyle(theme);
themeAccent.applyStyle(theme);
}
private Context wrap(Context context) {
ContextThemeWrapper wrapper = themeBase.wrap(context);
applyToContext(wrapper);
return wrapper;
}
}

@ -0,0 +1,43 @@
package org.tasks.themes
import android.app.Activity
import android.content.Context
import android.graphics.PixelFormat
import android.view.LayoutInflater
import org.tasks.R
import javax.inject.Inject
class Theme @Inject constructor(
val themeBase: ThemeBase,
val themeColor: ThemeColor,
private val themeAccent: ThemeAccent
) {
fun withThemeColor(themeColor: ThemeColor) = Theme(themeBase, themeColor, themeAccent)
fun getLayoutInflater(context: Context) =
wrap(context).getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
fun applyThemeAndStatusBarColor(activity: Activity) {
applyTheme(activity)
themeColor.applyToSystemBars(activity)
themeColor.applyTaskDescription(activity, activity.getString(R.string.app_name))
}
fun applyTheme(activity: Activity) {
themeBase.set(activity)
applyToContext(activity)
activity.window.setFormat(PixelFormat.RGBA_8888)
}
fun applyToContext(context: Context) {
val theme = context.theme
themeColor.applyStyle(theme)
themeAccent.applyStyle(theme)
}
private fun wrap(context: Context): Context {
val wrapper = themeBase.wrap(context)
applyToContext(wrapper)
return wrapper
}
}
Loading…
Cancel
Save