From 334f28c2fa3dabf26c46ee3052245d4e7f9d3396 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 8 Apr 2017 19:03:33 +0200 Subject: [PATCH] convert Config and Constants to kotlin --- .../com/simplemobiletools/draw/Config.java | 61 ------------------- .../com/simplemobiletools/draw/Constants.java | 12 ---- .../draw/activities/MainActivity.kt | 11 ++-- .../draw/activities/SettingsActivity.kt | 6 +- .../simplemobiletools/draw/helpers/Config.kt | 27 ++++++++ .../draw/helpers/Constants.kt | 6 ++ 6 files changed, 42 insertions(+), 81 deletions(-) delete mode 100644 app/src/main/java/com/simplemobiletools/draw/Config.java delete mode 100644 app/src/main/java/com/simplemobiletools/draw/Constants.java create mode 100644 app/src/main/kotlin/com/simplemobiletools/draw/helpers/Config.kt create mode 100644 app/src/main/kotlin/com/simplemobiletools/draw/helpers/Constants.kt diff --git a/app/src/main/java/com/simplemobiletools/draw/Config.java b/app/src/main/java/com/simplemobiletools/draw/Config.java deleted file mode 100644 index e19eaaa..0000000 --- a/app/src/main/java/com/simplemobiletools/draw/Config.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.simplemobiletools.draw; - -import android.content.Context; -import android.content.SharedPreferences; -import android.graphics.Color; - -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 void setIsDarkTheme(boolean isDarkTheme) { - mPrefs.edit().putBoolean(Constants.IS_DARK_THEME, isDarkTheme).apply(); - } - - public boolean getShowBrushSizeEnabled() { - return mPrefs.getBoolean(Constants.SHOW_BRUSH_SIZE, false); - } - - public void setShowBrushSizeEnabled(boolean showBrushSize) { - mPrefs.edit().putBoolean(Constants.SHOW_BRUSH_SIZE, showBrushSize).apply(); - } - - public int getBrushColor() { - return mPrefs.getInt(Constants.BRUSH_COLOR_KEY, Color.BLACK); - } - - public void setBrushColor(int color) { - mPrefs.edit().putInt(Constants.BRUSH_COLOR_KEY, color).apply(); - } - - public float getStrokeWidth() { - return mPrefs.getFloat(Constants.STROKE_WIDTH_KEY, 5.0f); - } - - public void setStrokeWidth(float strokeWidth) { - mPrefs.edit().putFloat(Constants.STROKE_WIDTH_KEY, strokeWidth).apply(); - } - - public int getBackgroundColor() { - return mPrefs.getInt(Constants.BACKGROUND_COLOR_KEY, Color.WHITE); - } - - public void setBackgroundColor(int color) { - mPrefs.edit().putInt(Constants.BACKGROUND_COLOR_KEY, color).apply(); - } -} diff --git a/app/src/main/java/com/simplemobiletools/draw/Constants.java b/app/src/main/java/com/simplemobiletools/draw/Constants.java deleted file mode 100644 index 58b3798..0000000 --- a/app/src/main/java/com/simplemobiletools/draw/Constants.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.simplemobiletools.draw; - -public class Constants { - // shared preferences - public static final String PREFS_KEY = "Draw"; - public static final String BRUSH_COLOR_KEY = "brush_color"; - public static final String STROKE_WIDTH_KEY = "stroke_width"; - public static final String BACKGROUND_COLOR_KEY = "background_color"; - public static final String IS_FIRST_RUN = "is_first_run"; - public static final String IS_DARK_THEME = "is_dark_theme"; - public static final String SHOW_BRUSH_SIZE = "show_brush_size"; -} diff --git a/app/src/main/kotlin/com/simplemobiletools/draw/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/draw/activities/MainActivity.kt index ac6b00e..537cc67 100644 --- a/app/src/main/kotlin/com/simplemobiletools/draw/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/draw/activities/MainActivity.kt @@ -23,6 +23,7 @@ import butterknife.ButterKnife import butterknife.OnClick import com.simplemobiletools.commons.activities.AboutActivity import com.simplemobiletools.draw.* +import com.simplemobiletools.draw.helpers.Config import yuku.ambilwarna.AmbilWarnaDialog import java.io.ByteArrayOutputStream import java.io.File @@ -57,17 +58,17 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener { mMyCanvas!!.setListener(this) mStrokeWidthBar!!.setOnSeekBarChangeListener(onStrokeWidthBarChangeListener) - setBackgroundColor(Config.newInstance(this).backgroundColor) + setBackgroundColor(Config.newInstance(this).canvasBackgroundColor) setColor(Config.newInstance(this).brushColor) - strokeWidth = Config.newInstance(this).strokeWidth + strokeWidth = Config.newInstance(this).brushSize mMyCanvas!!.setStrokeWidth(strokeWidth) mStrokeWidthBar!!.progress = strokeWidth.toInt() } override fun onResume() { super.onResume() - val isStrokeWidthBarEnabled = Config.newInstance(this).showBrushSizeEnabled + val isStrokeWidthBarEnabled = Config.newInstance(this).showBrushSize mStrokeWidthBar!!.visibility = if (isStrokeWidthBarEnabled) View.VISIBLE else View.GONE mMyCanvas!!.setIsStrokeWidthBarEnabled(isStrokeWidthBarEnabled) } @@ -75,7 +76,7 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener { override fun onPause() { super.onPause() Config.newInstance(this).brushColor = color - Config.newInstance(this).strokeWidth = strokeWidth + Config.newInstance(this).brushSize = strokeWidth } override fun onDestroy() { @@ -114,7 +115,7 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener { override fun onOk(dialog: AmbilWarnaDialog, pickedColor: Int) { setBackgroundColor(pickedColor) - Config.newInstance(applicationContext).backgroundColor = pickedColor + Config.newInstance(applicationContext).canvasBackgroundColor = pickedColor } }) diff --git a/app/src/main/kotlin/com/simplemobiletools/draw/activities/SettingsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/draw/activities/SettingsActivity.kt index 41a32e9..55d999b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/draw/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/draw/activities/SettingsActivity.kt @@ -2,7 +2,7 @@ package com.simplemobiletools.draw.activities import android.os.Bundle import com.simplemobiletools.commons.extensions.updateTextColors -import com.simplemobiletools.draw.Config +import com.simplemobiletools.draw.helpers.Config import com.simplemobiletools.draw.R import kotlinx.android.synthetic.main.activity_settings.* @@ -27,10 +27,10 @@ class SettingsActivity : SimpleActivity() { } private fun setupBrushSize() { - settings_show_brush_size.isChecked = Config.newInstance(this).showBrushSizeEnabled + settings_show_brush_size.isChecked = Config.newInstance(this).showBrushSize settings_show_brush_size_holder.setOnClickListener { settings_show_brush_size.toggle() - Config.newInstance(this).showBrushSizeEnabled = settings_show_brush_size.isChecked + Config.newInstance(this).showBrushSize = settings_show_brush_size.isChecked } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/draw/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/draw/helpers/Config.kt new file mode 100644 index 0000000..660a850 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/draw/helpers/Config.kt @@ -0,0 +1,27 @@ +package com.simplemobiletools.draw.helpers + +import android.content.Context +import android.graphics.Color +import com.simplemobiletools.commons.helpers.BaseConfig + +class Config(context: Context) : BaseConfig(context) { + companion object { + fun newInstance(context: Context) = Config(context) + } + + var showBrushSize: Boolean + get() = prefs.getBoolean(SHOW_BRUSH_SIZE, false) + set(showBrushSize) = prefs.edit().putBoolean(SHOW_BRUSH_SIZE, showBrushSize).apply() + + var brushColor: Int + get() = prefs.getInt(BRUSH_COLOR, Color.BLACK) + set(color) = prefs.edit().putInt(BRUSH_COLOR, color).apply() + + var brushSize: Float + get() = prefs.getFloat(BRUSH_SIZE, 5.0f) + set(brushSize) = prefs.edit().putFloat(BRUSH_SIZE, brushSize).apply() + + var canvasBackgroundColor: Int + get() = prefs.getInt(CANVAS_BACKGROUND_COLOR, Color.WHITE) + set(canvasBackgroundColor) = prefs.edit().putInt(CANVAS_BACKGROUND_COLOR, canvasBackgroundColor).apply() +} diff --git a/app/src/main/kotlin/com/simplemobiletools/draw/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/draw/helpers/Constants.kt new file mode 100644 index 0000000..927febc --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/draw/helpers/Constants.kt @@ -0,0 +1,6 @@ +package com.simplemobiletools.draw.helpers + +val BRUSH_COLOR = "brush_color" +val CANVAS_BACKGROUND_COLOR = "canvas_background_color" +val SHOW_BRUSH_SIZE = "show_brush_size" +val BRUSH_SIZE = "brush_size"