convert Config to kotlin

This commit is contained in:
tibbi 2016-11-17 18:23:21 +01:00
parent b3fefeb810
commit faaecbc229
5 changed files with 62 additions and 84 deletions

View File

@ -1,80 +0,0 @@
package com.simplemobiletools.calendar;
import android.content.Context;
import android.content.SharedPreferences;
import java.util.Locale;
public class Config {
private SharedPreferences mPrefs;
public static Config newInstance(Context context) {
return new Config(context);
}
public Config(Context context) {
mPrefs = context.getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE);
}
public boolean getIsFirstRun() {
return mPrefs.getBoolean(Constants.IS_FIRST_RUN, true);
}
public void setIsFirstRun(boolean firstRun) {
mPrefs.edit().putBoolean(Constants.IS_FIRST_RUN, firstRun).apply();
}
public boolean getIsDarkTheme() {
return mPrefs.getBoolean(Constants.IS_DARK_THEME, false);
}
public void setIsDarkTheme(boolean isDarkTheme) {
mPrefs.edit().putBoolean(Constants.IS_DARK_THEME, isDarkTheme).apply();
}
public boolean getIsSundayFirst() {
boolean isSundayFirst = java.util.Calendar.getInstance(Locale.getDefault()).getFirstDayOfWeek() == java.util.Calendar.SUNDAY;
return mPrefs.getBoolean(Constants.SUNDAY_FIRST, isSundayFirst);
}
public void setIsSundayFirst(boolean sundayFirst) {
mPrefs.edit().putBoolean(Constants.SUNDAY_FIRST, sundayFirst).apply();
}
public boolean getDisplayWeekNumbers() {
return mPrefs.getBoolean(Constants.WEEK_NUMBERS, false);
}
public void setDisplayWeekNumbers(boolean displayWeekNumbers) {
mPrefs.edit().putBoolean(Constants.WEEK_NUMBERS, displayWeekNumbers).apply();
}
public int getStoredView() {
return mPrefs.getInt(Constants.VIEW, Constants.MONTHLY_VIEW);
}
public void setStoredView(int view) {
mPrefs.edit().putInt(Constants.VIEW, view).apply();
}
public int getDefaultReminderType() {
return mPrefs.getInt(Constants.REMINDER_TYPE, Constants.REMINDER_AT_START);
}
public void setDefaultReminderType(int type) {
if (type == Constants.REMINDER_CUSTOM && getDefaultReminderMinutes() == 0)
type = Constants.REMINDER_AT_START;
mPrefs.edit().putInt(Constants.REMINDER_TYPE, type).apply();
}
public int getDefaultReminderMinutes() {
return mPrefs.getInt(Constants.REMINDER_MINUTES, 10);
}
public void setDefaultReminderMinutes(int mins) {
if (mins == 0)
setDefaultReminderType(Constants.REMINDER_AT_START);
mPrefs.edit().putInt(Constants.REMINDER_MINUTES, mins).apply();
}
}

View File

@ -130,7 +130,7 @@ public class MyWidgetProvider extends AppWidgetProvider implements MonthlyCalend
}
public void updateDays(List<Day> days) {
final boolean displayWeekNumbers = Config.newInstance(mContext).getDisplayWeekNumbers();
final boolean displayWeekNumbers = Config.Companion.newInstance(mContext).getDisplayWeekNumbers();
final int len = days.size();
final String packageName = mContext.getPackageName();
mRemoteViews.setInt(R.id.week_num, "setTextColor", mWeakTextColor);
@ -193,7 +193,7 @@ public class MyWidgetProvider extends AppWidgetProvider implements MonthlyCalend
}
private void updateLabelColor() {
final boolean mSundayFirst = Config.newInstance(mContext).getIsSundayFirst();
final boolean mSundayFirst = Config.Companion.newInstance(mContext).isSundayFirst();
final String packageName = mContext.getPackageName();
int letters[] = Utils.getLetterIDs();
for (int i = 0; i < 7; i++) {

View File

@ -54,7 +54,7 @@ public class AboutActivity extends SimpleActivity {
}
private void setupRateUs() {
if (mConfig.getIsFirstRun()) {
if (mConfig.isFirstRun()) {
mRateUs.setVisibility(View.GONE);
}
}

View File

@ -186,7 +186,7 @@ public class WidgetConfigureActivity extends AppCompatActivity implements Monthl
private void updateDays() {
final int len = mDays.size();
if (Config.newInstance(getApplicationContext()).getDisplayWeekNumbers()) {
if (Config.Companion.newInstance(getApplicationContext()).getDisplayWeekNumbers()) {
final TextView weekNum = (TextView) findViewById(R.id.week_num);
weekNum.setTextColor(mWeakTextColor);
weekNum.setVisibility(View.VISIBLE);

View File

@ -0,0 +1,58 @@
package com.simplemobiletools.calendar
import android.content.Context
import android.content.SharedPreferences
import java.util.*
class Config(context: Context) {
private val mPrefs: SharedPreferences
companion object {
fun newInstance(context: Context) = Config(context)
}
init {
mPrefs = context.getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE)
}
var isFirstRun: Boolean
get() = mPrefs.getBoolean(Constants.IS_FIRST_RUN, true)
set(firstRun) = mPrefs.edit().putBoolean(Constants.IS_FIRST_RUN, firstRun).apply()
var isDarkTheme: Boolean
get() = mPrefs.getBoolean(Constants.IS_DARK_THEME, false)
set(isDarkTheme) = mPrefs.edit().putBoolean(Constants.IS_DARK_THEME, isDarkTheme).apply()
var isSundayFirst: Boolean
get() {
val isSundayFirst = java.util.Calendar.getInstance(Locale.getDefault()).firstDayOfWeek == java.util.Calendar.SUNDAY
return mPrefs.getBoolean(Constants.SUNDAY_FIRST, isSundayFirst)
}
set(sundayFirst) = mPrefs.edit().putBoolean(Constants.SUNDAY_FIRST, sundayFirst).apply()
var displayWeekNumbers: Boolean
get() = mPrefs.getBoolean(Constants.WEEK_NUMBERS, false)
set(displayWeekNumbers) = mPrefs.edit().putBoolean(Constants.WEEK_NUMBERS, displayWeekNumbers).apply()
var storedView: Int
get() = mPrefs.getInt(Constants.VIEW, Constants.MONTHLY_VIEW)
set(view) = mPrefs.edit().putInt(Constants.VIEW, view).apply()
var defaultReminderType: Int
get() = mPrefs.getInt(Constants.REMINDER_TYPE, Constants.REMINDER_AT_START)
set(type) {
var newType = type
if (newType == Constants.REMINDER_CUSTOM && defaultReminderMinutes == 0)
newType = Constants.REMINDER_AT_START
mPrefs.edit().putInt(Constants.REMINDER_TYPE, newType).apply()
}
var defaultReminderMinutes: Int
get() = mPrefs.getInt(Constants.REMINDER_MINUTES, 10)
set(mins) {
if (mins == 0)
defaultReminderType = Constants.REMINDER_AT_START
mPrefs.edit().putInt(Constants.REMINDER_MINUTES, mins).apply()
}
}