inherit config from the common BaseConfig

This commit is contained in:
tibbi 2016-12-28 23:48:35 +01:00
parent afd330b799
commit a6ed9523f0
12 changed files with 46 additions and 113 deletions

View File

@ -25,7 +25,6 @@ class SettingsActivity : SimpleActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_settings)
setupDarkTheme()
setupSundayFirst()
setupWeekNumbers()
setupVibrate()
@ -33,15 +32,6 @@ class SettingsActivity : SimpleActivity() {
setupEventReminder()
}
private fun setupDarkTheme() {
settings_dark_theme.isChecked = mConfig.isDarkTheme
settings_dark_theme_holder.setOnClickListener {
settings_dark_theme.toggle()
mConfig.isDarkTheme = settings_dark_theme.isChecked
restartActivity()
}
}
private fun setupSundayFirst() {
settings_sunday_first.isChecked = mConfig.isSundayFirst
settings_sunday_first_holder.setOnClickListener {

View File

@ -2,7 +2,6 @@ package com.simplemobiletools.calendar.activities
import android.app.Activity
import android.appwidget.AppWidgetManager
import android.content.Context
import android.content.Intent
import android.content.res.Resources
import android.graphics.Color
@ -11,8 +10,10 @@ import android.support.v7.app.AppCompatActivity
import android.widget.SeekBar
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.adapters.EventListWidgetAdapterOld
import com.simplemobiletools.calendar.helpers.*
import com.simplemobiletools.calendar.helpers.Config
import com.simplemobiletools.calendar.helpers.Formatter
import com.simplemobiletools.calendar.helpers.HIGH_ALPHA
import com.simplemobiletools.calendar.helpers.MyWidgetListProvider
import com.simplemobiletools.calendar.models.ListEvent
import com.simplemobiletools.calendar.models.ListItem
import com.simplemobiletools.calendar.models.ListSection
@ -61,11 +62,11 @@ class WidgetListConfigureActivity : AppCompatActivity() {
private fun initVariables() {
mRes = resources
val prefs = initPrefs(this)
mTextColorWithoutTransparency = prefs.getInt(WIDGET_TEXT_COLOR, resources.getColor(R.color.color_primary))
val config = Config.newInstance(this)
mTextColorWithoutTransparency = config.widgetTextColor
updateTextColors()
mBgColor = prefs.getInt(WIDGET_BG_COLOR, 1)
mBgColor = config.widgetBgColor
if (mBgColor == 1) {
mBgColor = Color.BLACK
mBgAlpha = .2f
@ -79,8 +80,6 @@ class WidgetListConfigureActivity : AppCompatActivity() {
updateBgColor()
}
private fun initPrefs(context: Context) = context.getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE)
fun saveConfig() {
storeWidgetColors()
requestWidgetUpdate()
@ -93,8 +92,9 @@ class WidgetListConfigureActivity : AppCompatActivity() {
}
private fun storeWidgetColors() {
getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE).apply {
edit().putInt(WIDGET_BG_COLOR, mBgColor).putInt(WIDGET_TEXT_COLOR, mTextColorWithoutTransparency).apply()
Config.newInstance(this).apply {
widgetBgColor = mBgColor
widgetTextColor = mTextColorWithoutTransparency
}
}

View File

@ -2,7 +2,6 @@ package com.simplemobiletools.calendar.activities
import android.app.Activity
import android.appwidget.AppWidgetManager
import android.content.Context
import android.content.Intent
import android.content.res.Resources
import android.graphics.Color
@ -14,7 +13,10 @@ import android.widget.SeekBar
import android.widget.TextView
import com.simplemobiletools.calendar.MonthlyCalendarImpl
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.helpers.*
import com.simplemobiletools.calendar.helpers.Config
import com.simplemobiletools.calendar.helpers.HIGH_ALPHA
import com.simplemobiletools.calendar.helpers.LOW_ALPHA
import com.simplemobiletools.calendar.helpers.MyWidgetMonthlyProvider
import com.simplemobiletools.calendar.interfaces.MonthlyCalendar
import com.simplemobiletools.calendar.models.Day
import com.simplemobiletools.commons.extensions.adjustAlpha
@ -66,11 +68,11 @@ class WidgetMonthlyConfigureActivity : AppCompatActivity(), MonthlyCalendar {
mTodayTextSize = mRes.getDimension(R.dimen.today_text_size)
mTodayTextSize /= mRes.displayMetrics.density
val prefs = initPrefs(this)
mTextColorWithoutTransparency = prefs.getInt(WIDGET_TEXT_COLOR, resources.getColor(R.color.color_primary))
val config = Config.newInstance(this)
mTextColorWithoutTransparency = config.widgetTextColor
updateTextColors()
mBgColor = prefs.getInt(WIDGET_BG_COLOR, 1)
mBgColor = config.widgetBgColor
if (mBgColor == 1) {
mBgColor = Color.BLACK
mBgAlpha = .2f
@ -86,8 +88,6 @@ class WidgetMonthlyConfigureActivity : AppCompatActivity(), MonthlyCalendar {
MonthlyCalendarImpl(this, applicationContext).updateMonthlyCalendar(DateTime())
}
private fun initPrefs(context: Context) = context.getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE)
fun saveConfig() {
storeWidgetColors()
requestWidgetUpdate()
@ -100,8 +100,9 @@ class WidgetMonthlyConfigureActivity : AppCompatActivity(), MonthlyCalendar {
}
private fun storeWidgetColors() {
getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE).apply {
edit().putInt(WIDGET_BG_COLOR, mBgColor).putInt(WIDGET_TEXT_COLOR, mTextColorWithoutTransparency).apply()
Config.newInstance(this).apply {
widgetBgColor = mBgColor
widgetTextColor = mTextColorWithoutTransparency
}
}

View File

@ -3,8 +3,6 @@ package com.simplemobiletools.calendar.adapters
import android.appwidget.AppWidgetManager
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.graphics.Color
import android.widget.RemoteViews
import android.widget.RemoteViewsService
import com.simplemobiletools.calendar.R
@ -26,12 +24,10 @@ class EventListWidgetAdapter(val context: Context, val intent: Intent) : RemoteV
val appWidgetId: Int
var events: List<ListItem>
val prefs: SharedPreferences
val textColor: Int
init {
prefs = context.getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE)
textColor = prefs.getInt(WIDGET_TEXT_COLOR, Color.WHITE).adjustAlpha(HIGH_ALPHA)
textColor = Config.newInstance(context).widgetTextColor.adjustAlpha(HIGH_ALPHA)
appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID)
events = ArrayList<ListItem>()
}

View File

@ -61,7 +61,7 @@ class DayFragment : Fragment(), DBHelper.EventUpdateListener, DBHelper.GetEvents
}
private fun setupButtons() {
val baseColor = if (mConfig.isDarkTheme) Color.WHITE else Color.BLACK
val baseColor = Color.BLACK
mTextColor = baseColor.adjustAlpha(HIGH_ALPHA)
mHolder.apply {

View File

@ -92,7 +92,7 @@ class MonthFragment : Fragment(), MonthlyCalendar {
}
private fun setupButtons() {
val baseColor = if (mConfig.isDarkTheme) Color.WHITE else Color.BLACK
val baseColor = Color.BLACK
mTextColor = baseColor.adjustAlpha(HIGH_ALPHA)
mTextColorWithEvent = mRes.getColor(R.color.color_primary).adjustAlpha(HIGH_ALPHA)
mWeakTextColor = baseColor.adjustAlpha(LOW_ALPHA)

View File

@ -1,68 +1,54 @@
package com.simplemobiletools.calendar.helpers
import android.content.Context
import android.content.SharedPreferences
import android.media.RingtoneManager
import com.simplemobiletools.commons.helpers.BaseConfig
import java.util.*
class Config(val context: Context) {
private val mPrefs: SharedPreferences
class Config(context: Context) : BaseConfig(context) {
companion object {
fun newInstance(context: Context) = Config(context)
}
init {
mPrefs = context.getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE)
}
var isFirstRun: Boolean
get() = mPrefs.getBoolean(IS_FIRST_RUN, true)
set(firstRun) = mPrefs.edit().putBoolean(IS_FIRST_RUN, firstRun).apply()
var isDarkTheme: Boolean
get() = mPrefs.getBoolean(IS_DARK_THEME, false)
set(isDarkTheme) = mPrefs.edit().putBoolean(IS_DARK_THEME, isDarkTheme).apply()
var isSundayFirst: Boolean
get() {
val isSundayFirst = Calendar.getInstance(Locale.getDefault()).firstDayOfWeek == Calendar.SUNDAY
return mPrefs.getBoolean(SUNDAY_FIRST, isSundayFirst)
return prefs.getBoolean(SUNDAY_FIRST, isSundayFirst)
}
set(sundayFirst) = mPrefs.edit().putBoolean(SUNDAY_FIRST, sundayFirst).apply()
set(sundayFirst) = prefs.edit().putBoolean(SUNDAY_FIRST, sundayFirst).apply()
var displayWeekNumbers: Boolean
get() = mPrefs.getBoolean(WEEK_NUMBERS, false)
set(displayWeekNumbers) = mPrefs.edit().putBoolean(WEEK_NUMBERS, displayWeekNumbers).apply()
get() = prefs.getBoolean(WEEK_NUMBERS, false)
set(displayWeekNumbers) = prefs.edit().putBoolean(WEEK_NUMBERS, displayWeekNumbers).apply()
var vibrateOnReminder: Boolean
get() = mPrefs.getBoolean(VIBRATE, false)
set(vibrate) = mPrefs.edit().putBoolean(VIBRATE, vibrate).apply()
get() = prefs.getBoolean(VIBRATE, false)
set(vibrate) = prefs.edit().putBoolean(VIBRATE, vibrate).apply()
var reminderSound: String
get() = mPrefs.getString(REMINDER_SOUND, getDefaultNotificationSound())
set(path) = mPrefs.edit().putString(REMINDER_SOUND, path).apply()
get() = prefs.getString(REMINDER_SOUND, getDefaultNotificationSound())
set(path) = prefs.edit().putString(REMINDER_SOUND, path).apply()
var storedView: Int
get() = mPrefs.getInt(VIEW, MONTHLY_VIEW)
set(view) = mPrefs.edit().putInt(VIEW, view).apply()
get() = prefs.getInt(VIEW, MONTHLY_VIEW)
set(view) = prefs.edit().putInt(VIEW, view).apply()
var defaultReminderType: Int
get() = mPrefs.getInt(REMINDER_TYPE, REMINDER_AT_START)
get() = prefs.getInt(REMINDER_TYPE, REMINDER_AT_START)
set(type) {
var newType = type
if (newType == REMINDER_CUSTOM && defaultReminderMinutes == 0)
newType = REMINDER_AT_START
mPrefs.edit().putInt(REMINDER_TYPE, newType).apply()
prefs.edit().putInt(REMINDER_TYPE, newType).apply()
}
var defaultReminderMinutes: Int
get() = mPrefs.getInt(REMINDER_MINUTES, 10)
get() = prefs.getInt(REMINDER_MINUTES, 10)
set(mins) {
if (mins == 0)
defaultReminderType = REMINDER_AT_START
mPrefs.edit().putInt(REMINDER_MINUTES, mins).apply()
prefs.edit().putInt(REMINDER_MINUTES, mins).apply()
}
fun getDefaultNotificationSound() = RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_NOTIFICATION)?.toString() ?: ""

View File

@ -28,9 +28,6 @@ val HOUR_MINS = 60
val DAY_MINS = 1440
// Shared Preferences
val PREFS_KEY = "Calendar"
val IS_FIRST_RUN = "is_first_run"
val IS_DARK_THEME = "is_dark_theme"
val SUNDAY_FIRST = "sunday_first"
val WEEK_NUMBERS = "week_numbers"
val VIBRATE = "vibrate"
@ -38,8 +35,6 @@ val REMINDER_SOUND = "reminder_sound"
val VIEW = "view"
val REMINDER_TYPE = "reminder_type"
val REMINDER_MINUTES = "reminder_minutes"
val WIDGET_BG_COLOR = "widget_bg_color"
val WIDGET_TEXT_COLOR = "widget_text_color"
val letterIDs = intArrayOf(R.string.sunday_letter, R.string.monday_letter, R.string.tuesday_letter, R.string.wednesday_letter,
R.string.thursday_letter, R.string.friday_letter, R.string.saturday_letter)

View File

@ -7,7 +7,6 @@ import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.res.Resources
import android.graphics.Color
import android.net.Uri
import android.widget.RemoteViews
import com.simplemobiletools.calendar.R
@ -35,17 +34,15 @@ class MyWidgetListProvider : AppWidgetProvider() {
mContext = context
mRes = mContext.resources
val prefs = initPrefs(context)
val storedTextColor = prefs.getInt(WIDGET_TEXT_COLOR, Color.WHITE)
mTextColor = storedTextColor.adjustAlpha(HIGH_ALPHA)
val config = Config.newInstance(context)
mTextColor = config.widgetTextColor.adjustAlpha(HIGH_ALPHA)
mWidgetManager = AppWidgetManager.getInstance(mContext)
mRemoteViews = RemoteViews(mContext.packageName, R.layout.widget_event_list)
mIntent = Intent(mContext, MyWidgetListProvider::class.java)
val bgColor = prefs.getInt(WIDGET_BG_COLOR, Color.BLACK)
mRemoteViews.setInt(R.id.widget_event_list_holder, "setBackgroundColor", bgColor)
mRemoteViews.setInt(R.id.widget_event_list_holder, "setBackgroundColor", config.widgetBgColor)
mRemoteViews.setInt(R.id.widget_event_list_empty, "setTextColor", mTextColor)
Intent(context, WidgetService::class.java).apply {
@ -62,6 +59,4 @@ class MyWidgetListProvider : AppWidgetProvider() {
mWidgetManager.updateAppWidget(appWidgetIds, mRemoteViews)
mWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.widget_event_list)
}
private fun initPrefs(context: Context) = context.getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE)
}

View File

@ -50,10 +50,9 @@ class MyWidgetMonthlyProvider : AppWidgetProvider(), MonthlyCalendar {
mRes = mContext.resources
mCalendar = MonthlyCalendarImpl(this, mContext)
val prefs = initPrefs(context)
val storedTextColor = prefs.getInt(WIDGET_TEXT_COLOR, Color.WHITE)
mTextColor = storedTextColor.adjustAlpha(HIGH_ALPHA)
mWeakTextColor = storedTextColor.adjustAlpha(LOW_ALPHA)
val config = Config.newInstance(context)
mTextColor = config.widgetTextColor.adjustAlpha(HIGH_ALPHA)
mWeakTextColor = config.widgetTextColor.adjustAlpha(LOW_ALPHA)
mDayTextSize = mRes.getDimension(R.dimen.day_text_size) / mRes.displayMetrics.density
mTodayTextSize = mRes.getDimension(R.dimen.today_text_size) / mRes.displayMetrics.density
@ -65,8 +64,7 @@ class MyWidgetMonthlyProvider : AppWidgetProvider(), MonthlyCalendar {
updateLabelColor()
updateTopViews()
val bgColor = prefs.getInt(WIDGET_BG_COLOR, Color.BLACK)
mRemoteViews.setInt(R.id.calendar_holder, "setBackgroundColor", bgColor)
mRemoteViews.setInt(R.id.calendar_holder, "setBackgroundColor", config.widgetBgColor)
mCalendar?.updateMonthlyCalendar(DateTime())
}
@ -103,8 +101,6 @@ class MyWidgetMonthlyProvider : AppWidgetProvider(), MonthlyCalendar {
setupAppOpenIntent(R.id.top_value)
}
private fun initPrefs(context: Context) = context.getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE)
override fun onReceive(context: Context, intent: Intent) {
val action = intent.action
when (action) {

View File

@ -7,7 +7,6 @@ import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.helpers.Config
import com.simplemobiletools.calendar.helpers.MEDIUM_ALPHA
import com.simplemobiletools.commons.extensions.adjustAlpha
import java.util.*
@ -57,7 +56,7 @@ class SmallMonthView(context: Context, attrs: AttributeSet, defStyle: Int) : Vie
a.recycle()
}
val baseColor = if (Config.newInstance(context).isDarkTheme) Color.WHITE else Color.BLACK
val baseColor = Color.BLACK
mTextColor = baseColor.adjustAlpha(MEDIUM_ALPHA)
mColoredTextColor = resources.getColor(R.color.color_primary).adjustAlpha(MEDIUM_ALPHA)

View File

@ -11,31 +11,6 @@
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/settings_dark_theme_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:padding="@dimen/activity_margin">
<TextView
android:id="@+id/settings_dark_theme_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="@string/dark_theme"/>
<android.support.v7.widget.SwitchCompat
android:id="@+id/settings_dark_theme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:background="@null"
android:clickable="false"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/settings_sunday_first_holder"
android:layout_width="match_parent"