Skip to content

Commit

Permalink
Override widget colours depending on app dark theme setting
Browse files Browse the repository at this point in the history
  • Loading branch information
premnirmal committed Aug 8, 2021
1 parent 11829cf commit efe4d86
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import androidx.preference.MultiSelectListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.work.ExistingPeriodicWorkPolicy.REPLACE
import com.github.premnirmal.ticker.AppPreferences
import com.github.premnirmal.ticker.CustomTabs
import com.github.premnirmal.ticker.components.InAppMessage
Expand All @@ -44,7 +43,7 @@ import com.github.premnirmal.ticker.showDialog
import com.github.premnirmal.ticker.widget.WidgetDataProvider
import com.github.premnirmal.tickerwidget.BuildConfig
import com.github.premnirmal.tickerwidget.R
import kotlinx.android.synthetic.main.fragment_settings.toolbar
import kotlinx.android.synthetic.main.fragment_settings.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.threeten.bp.format.TextStyle.SHORT
Expand Down Expand Up @@ -118,8 +117,8 @@ class SettingsFragment : PreferenceFragmentCompat(), ChildFragment,
}

override fun onDisplayPreferenceDialog(preference: Preference) {
when {
preference.key == AppPreferences.START_TIME || preference.key == AppPreferences.END_TIME-> {
when (preference.key) {
AppPreferences.START_TIME, AppPreferences.END_TIME -> {
val pref = preference as TimePreference
val dialog = createTimePickerDialog(pref)
dialog.show()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import com.github.premnirmal.ticker.network.data.Quote
import com.github.premnirmal.ticker.widget.WidgetData
import com.github.premnirmal.ticker.widget.WidgetData.Companion.ChangeType.Percent
import com.github.premnirmal.tickerwidget.R
import com.github.premnirmal.tickerwidget.R.id

class WidgetPreviewAdapter(private var widgetData: WidgetData) : BaseAdapter() {

Expand Down Expand Up @@ -61,8 +60,8 @@ class WidgetPreviewAdapter(private var widgetData: WidgetData) : BaseAdapter() {
val gainLossPercentString = SpannableString(gainLossPercentFormatted)
val priceString = SpannableString(priceFormatted)

layout.findViewById<TextView>(id.ticker)?.text = stock.symbol
layout.findViewById<TextView>(id.holdings)?.text = if (widgetData.isCurrencyEnabled()) {
layout.findViewById<TextView>(R.id.ticker)?.text = stock.symbol
layout.findViewById<TextView>(R.id.holdings)?.text = if (widgetData.isCurrencyEnabled()) {
"${stock.currencySymbol}${stock.holdingsString()}"
} else {
stock.holdingsString()
Expand Down Expand Up @@ -106,7 +105,7 @@ class WidgetPreviewAdapter(private var widgetData: WidgetData) : BaseAdapter() {

if (stockViewLayout == R.layout.stockview3) {
val changeType = widgetData.changeType()
val changeText = layout.findViewById<TextView>(id.change)
val changeText = layout.findViewById<TextView>(R.id.change)
if (changeType === Percent) {
changeText?.text = changePercentString
} else {
Expand All @@ -117,32 +116,36 @@ class WidgetPreviewAdapter(private var widgetData: WidgetData) : BaseAdapter() {
notifyDataSetChanged()
}
} else {
layout.findViewById<TextView>(id.changePercent)?.text = changePercentString
layout.findViewById<TextView>(id.changeValue)?.text = changeValueString
layout.findViewById<TextView>(id.gain_loss)?.text = gainLossString
layout.findViewById<TextView>(id.gain_loss_percent)?.text = gainLossPercentString
layout.findViewById<TextView>(R.id.changePercent)?.text = changePercentString
layout.findViewById<TextView>(R.id.changeValue)?.text = changeValueString
layout.findViewById<TextView>(R.id.gain_loss)?.text = gainLossString
layout.findViewById<TextView>(R.id.gain_loss_percent)?.text = gainLossPercentString
}
layout.findViewById<TextView>(id.totalValue)?.text = priceString
layout.findViewById<TextView>(R.id.totalValue)?.text = priceString

val color: Int = if (change < 0f || changeInPercent < 0f) {
ContextCompat.getColor(layout.context, widgetData.negativeTextColor)
} else {
ContextCompat.getColor(layout.context, widgetData.positiveTextColor)
}
if (stockViewLayout == R.layout.stockview3) {
layout.findViewById<TextView>(id.change)?.setTextColor(color)
layout.findViewById<TextView>(R.id.change)?.setTextColor(color)
} else {
layout.findViewById<TextView>(id.changePercent)?.setTextColor(color)
layout.findViewById<TextView>(id.changeValue)?.setTextColor(color)
layout.findViewById<TextView>(R.id.changePercent)?.setTextColor(color)
layout.findViewById<TextView>(R.id.changeValue)?.setTextColor(color)
}

val colorGainLoss: Int = if (gainLoss < 0f || gainLoss < 0f) {
ContextCompat.getColor(layout.context, widgetData.negativeTextColor)
} else {
ContextCompat.getColor(layout.context, widgetData.positiveTextColor)
}
layout.findViewById<TextView>(id.gain_loss)?.setTextColor(colorGainLoss)
layout.findViewById<TextView>(id.gain_loss_percent)?.setTextColor(colorGainLoss)
layout.findViewById<TextView>(R.id.gain_loss)?.setTextColor(colorGainLoss)
layout.findViewById<TextView>(R.id.gain_loss_percent)?.setTextColor(colorGainLoss)

layout.findViewById<TextView>(R.id.ticker)?.setTextColor(widgetData.textColor())
layout.findViewById<TextView>(R.id.totalValue)?.setTextColor(widgetData.textColor())
layout.findViewById<TextView>(R.id.holdings)?.setTextColor(widgetData.textColor())

return layout
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,9 @@ import com.github.premnirmal.ticker.ui.SettingsTextView
import com.github.premnirmal.ticker.widget.WidgetData
import com.github.premnirmal.ticker.widget.WidgetDataProvider
import com.github.premnirmal.tickerwidget.R
import kotlinx.android.synthetic.main.fragment_widget_settings.setting_add_stock
import kotlinx.android.synthetic.main.fragment_widget_settings.setting_autosort
import kotlinx.android.synthetic.main.fragment_widget_settings.setting_autosort_checkbox
import kotlinx.android.synthetic.main.fragment_widget_settings.setting_bold
import kotlinx.android.synthetic.main.fragment_widget_settings.setting_bold_checkbox
import kotlinx.android.synthetic.main.fragment_widget_settings.setting_currency
import kotlinx.android.synthetic.main.fragment_widget_settings.setting_currency_checkbox
import kotlinx.android.synthetic.main.fragment_widget_settings.setting_hide_header
import kotlinx.android.synthetic.main.fragment_widget_settings.setting_hide_header_checkbox
import kotlinx.android.synthetic.main.fragment_widget_settings.setting_layout_type
import kotlinx.android.synthetic.main.fragment_widget_settings.setting_widget_name
import kotlinx.android.synthetic.main.fragment_widget_settings.setting_widget_width
import kotlinx.android.synthetic.main.fragment_widget_settings.*
import kotlinx.android.synthetic.main.widget_2x1.list
import kotlinx.android.synthetic.main.widget_header.last_updated
import kotlinx.android.synthetic.main.widget_header.next_update
import kotlinx.android.synthetic.main.widget_header.widget_header
import kotlinx.android.synthetic.main.widget_header.*
import javax.inject.Inject

class WidgetSettingsFragment : BaseFragment(), OnClickListener {
Expand Down Expand Up @@ -224,6 +211,7 @@ class WidgetSettingsFragment : BaseFragment(), OnClickListener {
}

private fun updatePreview(widgetData: WidgetData) {
widget_layout.setBackgroundResource(widgetData.backgroundResource())
val lastUpdatedText = when (val fetchState = stocksProvider.fetchState) {
is FetchState.Success -> getString(R.string.last_fetch, fetchState.displayString)
is FetchState.Failure -> getString(R.string.refresh_failed)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ class StockWidget : AppWidgetProvider() {
val nextUpdate: String = stocksProvider.nextFetch()
val nextUpdateText: String = context.getString(R.string.next_fetch, nextUpdate)
remoteViews.setTextViewText(R.id.next_update, nextUpdateText)
remoteViews.setInt(R.id.widget_layout, "setBackgroundResource", widgetData.backgroundResource())
// Refresh icon and progress
val refreshing = appPreferences.isRefreshing()
if (refreshing) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import android.content.Context
import android.content.SharedPreferences
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.content.ContextCompat
import com.github.premnirmal.ticker.AppPreferences
import com.github.premnirmal.ticker.AppPreferences.Companion.toCommaSeparatedString
Expand Down Expand Up @@ -37,6 +39,7 @@ class WidgetData {
@Inject internal lateinit var stocksProvider: IStocksProvider
@Inject internal lateinit var context: Context
@Inject internal lateinit var widgetDataProvider: WidgetDataProvider
@Inject internal lateinit var appPreferences: AppPreferences

private val position: Int
val widgetId: Int
Expand Down Expand Up @@ -75,8 +78,11 @@ class WidgetData {
}
}

val nightMode: Boolean
get() = appPreferences.nightMode == AppCompatDelegate.MODE_NIGHT_YES

val positiveTextColor: Int
@ColorRes get() = R.color.text_widget_positive
@ColorRes get() = if (nightMode) R.color.text_widget_positive_dark else R.color.text_widget_positive

val negativeTextColor: Int
@ColorRes get() = R.color.text_widget_negative
Expand Down Expand Up @@ -126,7 +132,11 @@ class WidgetData {
}

@ColorInt fun textColor(): Int {
return ContextCompat.getColor(context, R.color.widget_text)
return if (nightMode) {
ContextCompat.getColor(context, R.color.dark_widget_text)
} else {
ContextCompat.getColor(context, R.color.widget_text)
}
}

@LayoutRes fun stockViewLayout(): Int {
Expand All @@ -138,6 +148,15 @@ class WidgetData {
}
}

@DrawableRes
fun backgroundResource(): Int {
return if (nightMode) {
R.drawable.app_widget_background_dark
} else {
R.drawable.app_widget_background
}
}

fun autoSortEnabled(): Boolean = preferences.getBoolean(AUTOSORT, false)

fun setAutoSort(autoSort: Boolean) {
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/app_widget_background_dark.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="?attr/appWidgetRadius" />
<gradient
android:angle="135"
android:centerColor="#752a2a2a"
android:endColor="#98000000"
android:startColor="#98000000" />
</shape>
3 changes: 2 additions & 1 deletion app/src/main/res/layout/fragment_widget_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
android:layout_height="250dp"
android:padding="44dp">
<include
layout="@layout/widget_2x1"/>
layout="@layout/widget_2x1"
android:id="@+id/widget_layout"/>
</FrameLayout>

<LinearLayout
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<string name="setting_currency">Mostrar moneda</string>
<string name="setting_currency_desc">Mostrar moneda en el widget</string>
<string name="choose_text_size">Tamaño del texto de widget</string>
<string name="app_theme">Tema de la aplicación</string>
<string name="app_theme">Tema de la aplicación y el widget</string>
<string name="text_size_updated_message">Tamaño del texto actualizado</string>
<string name="bg_updated_message">Antecedentes actualizados</string>
<string name="layout_updated_message">Tipo de diseño actualizado</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
<string name="setting_currency">Show currency</string>
<string name="setting_currency_desc">Show currency in the widget</string>
<string name="choose_text_size">Widget Text Size</string>
<string name="app_theme">App Theme</string>
<string name="app_theme">App and Widget Theme</string>
<string name="text_size_updated_message">Text size updated</string>
<string name="bg_updated_message">Background updated</string>
<string name="layout_updated_message">Layout type updated</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/widget_colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
<resources>
<color name="text_widget_header">#aaa</color>
<color name="widget_text">#1b1b1b</color>
<color name="dark_widget_text">#d9fcfe</color>
<color name="text_widget_positive">#009900</color>
<color name="text_widget_positive_dark">#ccff66</color>
<color name="text_widget_negative">#ff6666</color>
<color name="widget_bg">#faf9f6</color>
</resources>
4 changes: 2 additions & 2 deletions app/version.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
versionName=3.9.744
versionCode=300900744
versionName=3.9.745
versionCode=300900745

0 comments on commit efe4d86

Please sign in to comment.