Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ package com.instructure.pandautils.views

import android.content.Context
import android.content.res.Configuration
import android.os.Bundle
import android.os.Parcel
import android.os.Parcelable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
Expand Down Expand Up @@ -137,6 +140,13 @@ open class CanvasWebViewWrapper @JvmOverloads constructor(
}

private fun initVisibility(html: String) {
updateButtonVisibility(html)
if (binding.themeSwitchButton.visibility == android.view.View.VISIBLE) {
changeButtonTheme()
}
}

private fun updateButtonVisibility(html: String) {
val nightModeFlags: Int = context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
if (nightModeFlags == Configuration.UI_MODE_NIGHT_YES && html.isNotEmpty()) {
binding.themeSwitchButton.setVisible()
Expand All @@ -145,6 +155,57 @@ open class CanvasWebViewWrapper @JvmOverloads constructor(
}
}

override fun onConfigurationChanged(newConfig: Configuration?) {
super.onConfigurationChanged(newConfig)
html?.let { htmlContent ->
updateButtonVisibility(htmlContent)
if (binding.themeSwitchButton.visibility == android.view.View.VISIBLE) {
changeButtonTheme()
changeContentTheme(htmlContent)
}
}
}

override fun onSaveInstanceState(): Parcelable {
val superState = super.onSaveInstanceState()
return SavedState(superState).apply {
this.themeSwitched = this@CanvasWebViewWrapper.themeSwitched
}
}

override fun onRestoreInstanceState(state: Parcelable?) {
when (state) {
is SavedState -> {
super.onRestoreInstanceState(state.superState)
themeSwitched = state.themeSwitched
}
else -> super.onRestoreInstanceState(state)
}
}

private class SavedState : BaseSavedState {
var themeSwitched: Boolean = false

constructor(superState: Parcelable?) : super(superState)

private constructor(parcel: Parcel) : super(parcel) {
themeSwitched = parcel.readInt() == 1
}

override fun writeToParcel(out: Parcel, flags: Int) {
super.writeToParcel(out, flags)
out.writeInt(if (themeSwitched) 1 else 0)
}

companion object {
@JvmField
val CREATOR = object : Parcelable.Creator<SavedState> {
override fun createFromParcel(parcel: Parcel) = SavedState(parcel)
override fun newArray(size: Int): Array<SavedState?> = arrayOfNulls(size)
}
}
}

private fun formatHtml(data: String): String {
val textDarkest = colorResToHexString(if (themeSwitched) R.color.licorice else R.color.textDarkest)
val textDark = colorResToHexString(if (themeSwitched) R.color.ash else R.color.textDark)
Expand Down