convert widgetConfigureActivity to kotlin

This commit is contained in:
tibbi 2016-11-17 19:24:13 +01:00
parent dde19cded8
commit 577a016a72
3 changed files with 229 additions and 264 deletions

View file

@ -1,263 +0,0 @@
package com.simplemobiletools.calendar.activities;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import com.simplemobiletools.calendar.helpers.Config;
import com.simplemobiletools.calendar.Constants;
import com.simplemobiletools.calendar.interfaces.MonthlyCalendar;
import com.simplemobiletools.calendar.MonthlyCalendarImpl;
import com.simplemobiletools.calendar.MyWidgetProvider;
import com.simplemobiletools.calendar.R;
import com.simplemobiletools.calendar.Utils;
import com.simplemobiletools.calendar.models.Day;
import org.joda.time.DateTime;
import java.util.List;
import butterknife.BindDimen;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yuku.ambilwarna.AmbilWarnaDialog;
public class WidgetConfigureActivity extends AppCompatActivity implements MonthlyCalendar {
@BindView(R.id.top_left_arrow) ImageView mLeftArrow;
@BindView(R.id.top_right_arrow) ImageView mRightArrow;
@BindView(R.id.top_value) TextView mMonthTV;
@BindView(R.id.config_bg_color) View mBgColorPicker;
@BindView(R.id.config_bg_seekbar) SeekBar mBgSeekBar;
@BindView(R.id.config_text_color) View mTextColorPicker;
@BindView(R.id.config_calendar) View mWidgetBackground;
@BindView(R.id.config_save) Button mSaveBtn;
@BindDimen(R.dimen.day_text_size) float mDayTextSize;
@BindDimen(R.dimen.today_text_size) float mTodayTextSize;
private static Resources mRes;
private static String mPackageName;
private List<Day> mDays;
private float mBgAlpha;
private int mWidgetId;
private int mBgColorWithoutTransparency;
private int mBgColor;
private int mTextColorWithoutTransparency;
private int mTextColor;
private int mWeakTextColor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setResult(RESULT_CANCELED);
setContentView(R.layout.widget_config);
ButterKnife.bind(this);
initVariables();
final Intent intent = getIntent();
final Bundle extras = intent.getExtras();
if (extras != null)
mWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
if (mWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID)
finish();
}
private void initVariables() {
mRes = getResources();
mPackageName = getPackageName();
mDayTextSize /= mRes.getDisplayMetrics().density;
mTodayTextSize /= mRes.getDisplayMetrics().density;
final SharedPreferences prefs = initPrefs(this);
mTextColorWithoutTransparency = prefs.getInt(Constants.WIDGET_TEXT_COLOR, getResources().getColor(R.color.colorPrimary));
updateTextColors();
mBgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, 1);
if (mBgColor == 1) {
mBgColor = Color.BLACK;
mBgAlpha = .2f;
} else {
mBgAlpha = Color.alpha(mBgColor) / (float) 255;
}
mBgColorWithoutTransparency = Color.rgb(Color.red(mBgColor), Color.green(mBgColor), Color.blue(mBgColor));
mBgSeekBar.setOnSeekBarChangeListener(bgSeekbarChangeListener);
mBgSeekBar.setProgress((int) (mBgAlpha * 100));
updateBgColor();
new MonthlyCalendarImpl(this, getApplicationContext()).updateMonthlyCalendar(new DateTime());
}
private SharedPreferences initPrefs(Context context) {
return context.getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE);
}
@OnClick(R.id.config_save)
public void saveConfig() {
storeWidgetColors();
requestWidgetUpdate();
final Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
private void storeWidgetColors() {
final SharedPreferences prefs = getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE);
prefs.edit().putInt(Constants.WIDGET_BG_COLOR, mBgColor).apply();
prefs.edit().putInt(Constants.WIDGET_TEXT_COLOR, mTextColorWithoutTransparency).apply();
}
@OnClick(R.id.config_bg_color)
public void pickBackgroundColor() {
AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, mBgColorWithoutTransparency, new AmbilWarnaDialog.OnAmbilWarnaListener() {
@Override
public void onCancel(AmbilWarnaDialog dialog) {
}
@Override
public void onOk(AmbilWarnaDialog dialog, int color) {
mBgColorWithoutTransparency = color;
updateBgColor();
}
});
dialog.show();
}
@OnClick(R.id.config_text_color)
public void pickTextColor() {
AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, mTextColor, new AmbilWarnaDialog.OnAmbilWarnaListener() {
@Override
public void onCancel(AmbilWarnaDialog dialog) {
}
@Override
public void onOk(AmbilWarnaDialog dialog, int color) {
mTextColorWithoutTransparency = color;
updateTextColors();
updateDays();
}
});
dialog.show();
}
private void requestWidgetUpdate() {
final Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, this, MyWidgetProvider.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[]{mWidgetId});
sendBroadcast(intent);
}
private void updateTextColors() {
mTextColor = Utils.adjustAlpha(mTextColorWithoutTransparency, Constants.HIGH_ALPHA);
mWeakTextColor = Utils.adjustAlpha(mTextColorWithoutTransparency, Constants.LOW_ALPHA);
mLeftArrow.getDrawable().mutate().setColorFilter(mTextColor, PorterDuff.Mode.SRC_ATOP);
mRightArrow.getDrawable().mutate().setColorFilter(mTextColor, PorterDuff.Mode.SRC_ATOP);
mMonthTV.setTextColor(mTextColor);
mTextColorPicker.setBackgroundColor(mTextColor);
mSaveBtn.setTextColor(mTextColor);
updateLabels();
}
private void updateBgColor() {
mBgColor = Utils.adjustAlpha(mBgColorWithoutTransparency, mBgAlpha);
mWidgetBackground.setBackgroundColor(mBgColor);
mBgColorPicker.setBackgroundColor(mBgColor);
mSaveBtn.setBackgroundColor(mBgColor);
}
private void updateDays() {
final int len = mDays.size();
if (Config.Companion.newInstance(getApplicationContext()).getDisplayWeekNumbers()) {
final TextView weekNum = (TextView) findViewById(R.id.week_num);
weekNum.setTextColor(mWeakTextColor);
weekNum.setVisibility(View.VISIBLE);
for (int i = 0; i < 6; i++) {
final TextView weekIdTV = (TextView) findViewById(mRes.getIdentifier("week_num_" + i, "id", mPackageName));
weekIdTV.setText(mDays.get(i * 7).getWeekOfYear() + ":");
weekIdTV.setTextColor(mWeakTextColor);
weekIdTV.setVisibility(View.VISIBLE);
}
}
for (int i = 0; i < len; i++) {
final Day day = mDays.get(i);
final TextView dayTV = (TextView) findViewById(mRes.getIdentifier("day_" + i, "id", mPackageName));
int curTextColor = mWeakTextColor;
float curTextSize = mDayTextSize;
if (day.isThisMonth()) {
curTextColor = mTextColor;
}
if (day.isToday()) {
curTextSize = mTodayTextSize;
}
dayTV.setText(String.valueOf(day.getValue()));
dayTV.setTextColor(curTextColor);
dayTV.setTextSize(curTextSize);
}
}
private SeekBar.OnSeekBarChangeListener bgSeekbarChangeListener = new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mBgAlpha = (float) progress / (float) 100;
updateBgColor();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
};
@Override
public void updateMonthlyCalendar(final String month, final List<Day> days) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mDays = days;
updateMonth(month);
updateDays();
}
});
}
private void updateMonth(String month) {
mMonthTV.setText(month);
}
private void updateLabels() {
for (int i = 0; i < 7; i++) {
final TextView dayTV = (TextView) findViewById(mRes.getIdentifier("label_" + i, "id", mPackageName));
dayTV.setTextSize(mDayTextSize);
dayTV.setTextColor(mTextColor);
}
}
}

View file

@ -0,0 +1,228 @@
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
import android.graphics.PorterDuff
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.SeekBar
import android.widget.TextView
import com.simplemobiletools.calendar.*
import com.simplemobiletools.calendar.helpers.Config
import com.simplemobiletools.calendar.interfaces.MonthlyCalendar
import com.simplemobiletools.calendar.models.Day
import kotlinx.android.synthetic.main.first_row.*
import kotlinx.android.synthetic.main.top_navigation.*
import kotlinx.android.synthetic.main.widget_config.*
import org.joda.time.DateTime
import yuku.ambilwarna.AmbilWarnaDialog
class WidgetConfigureActivity : AppCompatActivity(), MonthlyCalendar {
lateinit var mRes: Resources
private var mDays: List<Day>? = null
private var mPackageName = packageName
private var mBgAlpha = 0f
private var mWidgetId = 0
private var mBgColorWithoutTransparency = 0
private var mBgColor = 0
private var mTextColorWithoutTransparency = 0
private var mTextColor = 0
private var mWeakTextColor = 0
private var mDayTextSize = 0f
private var mTodayTextSize = 0f
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setResult(Activity.RESULT_CANCELED)
setContentView(R.layout.widget_config)
initVariables()
val extras = intent.extras
if (extras != null)
mWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID)
if (mWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID)
finish()
config_save.setOnClickListener { saveConfig() }
config_bg_color.setOnClickListener { pickBackgroundColor() }
config_text_color.setOnClickListener { pickTextColor() }
}
private fun initVariables() {
mRes = resources
mDayTextSize = mRes.getDimension(R.dimen.day_text_size)
mDayTextSize /= mRes.displayMetrics.density
mTodayTextSize = mRes.getDimension(R.dimen.today_text_size)
mTodayTextSize /= mRes.displayMetrics.density
val prefs = initPrefs(this)
mTextColorWithoutTransparency = prefs.getInt(Constants.WIDGET_TEXT_COLOR, resources.getColor(R.color.colorPrimary))
updateTextColors()
mBgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, 1)
if (mBgColor == 1) {
mBgColor = Color.BLACK
mBgAlpha = .2f
} else {
mBgAlpha = Color.alpha(mBgColor) / 255.toFloat()
}
mBgColorWithoutTransparency = Color.rgb(Color.red(mBgColor), Color.green(mBgColor), Color.blue(mBgColor))
config_bg_seekbar.setOnSeekBarChangeListener(bgSeekbarChangeListener)
config_bg_seekbar.progress = (mBgAlpha * 100).toInt()
updateBgColor()
MonthlyCalendarImpl(this, applicationContext).updateMonthlyCalendar(DateTime())
}
private fun initPrefs(context: Context) = context.getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE)
fun saveConfig() {
storeWidgetColors()
requestWidgetUpdate()
Intent().apply {
putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mWidgetId)
setResult(Activity.RESULT_OK, this)
}
finish()
}
private fun storeWidgetColors() {
getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE).apply {
edit().putInt(Constants.WIDGET_BG_COLOR, mBgColor).putInt(Constants.WIDGET_TEXT_COLOR, mTextColorWithoutTransparency).apply()
}
}
fun pickBackgroundColor() {
val dialog = AmbilWarnaDialog(this, mBgColorWithoutTransparency, object : AmbilWarnaDialog.OnAmbilWarnaListener {
override fun onCancel(dialog: AmbilWarnaDialog) {
}
override fun onOk(dialog: AmbilWarnaDialog, color: Int) {
mBgColorWithoutTransparency = color
updateBgColor()
}
})
dialog.show()
}
fun pickTextColor() {
val dialog = AmbilWarnaDialog(this, mTextColor, object : AmbilWarnaDialog.OnAmbilWarnaListener {
override fun onCancel(dialog: AmbilWarnaDialog) {
}
override fun onOk(dialog: AmbilWarnaDialog, color: Int) {
mTextColorWithoutTransparency = color
updateTextColors()
updateDays()
}
})
dialog.show()
}
private fun requestWidgetUpdate() {
val intent = Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, this, MyWidgetProvider::class.java)
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, intArrayOf(mWidgetId))
sendBroadcast(intent)
}
private fun updateTextColors() {
mTextColor = Utils.adjustAlpha(mTextColorWithoutTransparency, Constants.HIGH_ALPHA)
mWeakTextColor = Utils.adjustAlpha(mTextColorWithoutTransparency, Constants.LOW_ALPHA)
top_left_arrow.drawable.mutate().setColorFilter(mTextColor, PorterDuff.Mode.SRC_ATOP)
top_right_arrow.drawable.mutate().setColorFilter(mTextColor, PorterDuff.Mode.SRC_ATOP)
top_value.setTextColor(mTextColor)
config_text_color.setBackgroundColor(mTextColor)
config_save.setTextColor(mTextColor)
updateLabels()
}
private fun updateBgColor() {
mBgColor = Utils.adjustAlpha(mBgColorWithoutTransparency, mBgAlpha)
config_calendar.setBackgroundColor(mBgColor)
config_bg_color.setBackgroundColor(mBgColor)
config_save.setBackgroundColor(mBgColor)
}
private fun updateDays() {
val len = mDays!!.size
if (Config.newInstance(applicationContext).displayWeekNumbers) {
week_num.setTextColor(mWeakTextColor)
week_num.visibility = View.VISIBLE
for (i in 0..5) {
val weekIdTV = findViewById(mRes.getIdentifier("week_num_" + i, "id", mPackageName)) as TextView?
weekIdTV!!.text = mDays!![i * 7].weekOfYear.toString() + ":"
weekIdTV.setTextColor(mWeakTextColor)
weekIdTV.visibility = View.VISIBLE
}
}
for (i in 0..len - 1) {
val day = mDays!![i]
val dayTV = findViewById(mRes.getIdentifier("day_" + i, "id", mPackageName)) as TextView?
var curTextColor = mWeakTextColor
var curTextSize = mDayTextSize
if (day.isThisMonth) {
curTextColor = mTextColor
}
if (day.isToday) {
curTextSize = mTodayTextSize
}
dayTV!!.text = day.value.toString()
dayTV.setTextColor(curTextColor)
dayTV.textSize = curTextSize
}
}
private val bgSeekbarChangeListener = object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
mBgAlpha = progress.toFloat() / 100.toFloat()
updateBgColor()
}
override fun onStartTrackingTouch(seekBar: SeekBar) {
}
override fun onStopTrackingTouch(seekBar: SeekBar) {
}
}
override fun updateMonthlyCalendar(month: String, days: List<Day>) {
runOnUiThread {
mDays = days
updateMonth(month)
updateDays()
}
}
private fun updateMonth(month: String) {
top_value.text = month
}
private fun updateLabels() {
for (i in 0..6) {
val dayTV = findViewById(mRes.getIdentifier("label_" + i, "id", mPackageName)) as TextView?
dayTV!!.textSize = mDayTextSize
dayTV.setTextColor(mTextColor)
}
}
}

View file

@ -19,6 +19,6 @@
<dimen name="normal_text_size">14sp</dimen>
<dimen name="day_text_size">16sp</dimen>
<dimen name="month_text_size">22sp</dimen>
<dimen name="today_text_size">32sp</dimen>
<dimen name="today_text_size">26sp</dimen>
<dimen name="config_text_size">18sp</dimen>
</resources>