mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Refactor EditSettingActivity
This commit is contained in:
@@ -20,12 +20,12 @@
|
||||
package org.isoron.uhabits.automation
|
||||
|
||||
import android.os.*
|
||||
|
||||
import org.isoron.androidbase.activities.*
|
||||
import androidx.appcompat.app.*
|
||||
import org.isoron.uhabits.*
|
||||
import org.isoron.uhabits.activities.*
|
||||
import org.isoron.uhabits.core.models.*
|
||||
|
||||
class EditSettingActivity : BaseActivity() {
|
||||
class EditSettingActivity : AppCompatActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
val app = applicationContext as HabitsApplication
|
||||
@@ -34,13 +34,16 @@ class EditSettingActivity : BaseActivity() {
|
||||
.setArchivedAllowed(false)
|
||||
.setCompletedAllowed(true)
|
||||
.build())
|
||||
AndroidThemeSwitcher(this, app.component.preferences).apply()
|
||||
|
||||
val args = SettingUtils.parseIntent(this.intent, habits)
|
||||
|
||||
val controller = EditSettingController(this)
|
||||
val rootView = EditSettingRootView(this, habits, controller, args)
|
||||
val screen = BaseScreen(this)
|
||||
screen.setRootView(rootView)
|
||||
setScreen(screen)
|
||||
val view = EditSettingRootView(
|
||||
context = this,
|
||||
habitList = app.component.habitList,
|
||||
onSave = controller::onSave,
|
||||
args = args,
|
||||
)
|
||||
setContentView(view)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,70 +20,56 @@
|
||||
package org.isoron.uhabits.automation
|
||||
|
||||
import android.R.layout.*
|
||||
import android.annotation.*
|
||||
import android.content.*
|
||||
import android.view.*
|
||||
import androidx.appcompat.widget.*
|
||||
import androidx.appcompat.widget.Toolbar
|
||||
import android.widget.*
|
||||
import butterknife.*
|
||||
import org.isoron.androidbase.activities.*
|
||||
import org.isoron.androidbase.utils.*
|
||||
import org.isoron.uhabits.R
|
||||
import org.isoron.uhabits.*
|
||||
import org.isoron.uhabits.core.models.*
|
||||
import org.isoron.uhabits.databinding.*
|
||||
import org.isoron.uhabits.utils.*
|
||||
import java.util.*
|
||||
|
||||
@SuppressLint("ViewConstructor")
|
||||
class EditSettingRootView(
|
||||
context: Context,
|
||||
private val habitList: HabitList,
|
||||
private val controller: EditSettingController,
|
||||
private val onSave: (habit: Habit, action: Int) -> Unit,
|
||||
args: SettingUtils.Arguments?
|
||||
) : BaseRootView(context) {
|
||||
) : FrameLayout(context) {
|
||||
|
||||
@BindView(R.id.toolbar)
|
||||
lateinit var tbar: Toolbar
|
||||
|
||||
@BindView(R.id.habitSpinner)
|
||||
lateinit var habitSpinner: AppCompatSpinner
|
||||
|
||||
@BindView(R.id.actionSpinner)
|
||||
lateinit var actionSpinner: AppCompatSpinner
|
||||
private var binding = AutomationBinding.inflate(LayoutInflater.from(context))
|
||||
|
||||
init {
|
||||
addView(inflate(getContext(), R.layout.automation, null))
|
||||
ButterKnife.bind(this)
|
||||
addView(binding.root)
|
||||
setupToolbar(
|
||||
toolbar = binding.toolbar,
|
||||
title = resources.getString(R.string.app_name),
|
||||
color = PaletteColor(11),
|
||||
displayHomeAsUpEnabled = false,
|
||||
)
|
||||
populateHabitSpinner()
|
||||
habitSpinner.onItemSelectedListener = object: AdapterView.OnItemSelectedListener {
|
||||
binding.habitSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
|
||||
override fun onNothingSelected(parent: AdapterView<*>?) {
|
||||
}
|
||||
|
||||
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
|
||||
populateActionSpinner(habitList.getByPosition(position).isNumerical)
|
||||
}
|
||||
}
|
||||
args?.let {
|
||||
habitSpinner.setSelection(habitList.indexOf(it.habit))
|
||||
populateActionSpinner(it.habit.isNumerical)
|
||||
actionSpinner.setSelection(mapActionToSpinnerPosition(it.action))
|
||||
binding.buttonSave.setOnClickListener {
|
||||
val habit = habitList.getByPosition(binding.habitSpinner.selectedItemPosition)
|
||||
val action = mapSpinnerPositionToAction(
|
||||
isNumerical = habit.isNumerical,
|
||||
itemPosition = binding.actionSpinner.selectedItemPosition,
|
||||
)
|
||||
onSave(habit, action)
|
||||
}
|
||||
args?.let {
|
||||
binding.habitSpinner.setSelection(habitList.indexOf(it.habit))
|
||||
populateActionSpinner(it.habit.isNumerical)
|
||||
binding.actionSpinner.setSelection(mapActionToSpinnerPosition(it.action))
|
||||
}
|
||||
}
|
||||
|
||||
override fun getToolbar(): Toolbar {
|
||||
return tbar
|
||||
}
|
||||
|
||||
override fun getToolbarColor(): Int {
|
||||
val res = StyledResources(context)
|
||||
if (!res.getBoolean(R.attr.useHabitColorAsPrimary))
|
||||
return super.getToolbarColor()
|
||||
|
||||
return res.getColor(R.attr.aboutScreenColor)
|
||||
}
|
||||
|
||||
@OnClick(R.id.buttonSave)
|
||||
fun onClickSave() {
|
||||
val habit = habitList.getByPosition(habitSpinner.selectedItemPosition)
|
||||
val action = mapSpinnerPositionToAction(habit.isNumerical,
|
||||
actionSpinner.selectedItemPosition)
|
||||
controller.onSave(habit, action)
|
||||
}
|
||||
|
||||
private fun mapSpinnerPositionToAction(isNumerical: Boolean, itemPosition: Int): Int {
|
||||
@@ -102,7 +88,7 @@ class EditSettingRootView(
|
||||
}
|
||||
|
||||
private fun mapActionToSpinnerPosition(action: Int): Int {
|
||||
return when(action) {
|
||||
return when (action) {
|
||||
ACTION_CHECK -> 0
|
||||
ACTION_UNCHECK -> 1
|
||||
ACTION_TOGGLE -> 2
|
||||
@@ -116,13 +102,13 @@ class EditSettingRootView(
|
||||
val names = habitList.mapTo(LinkedList()) { it.name }
|
||||
val adapter = ArrayAdapter(context, simple_spinner_item, names)
|
||||
adapter.setDropDownViewResource(simple_spinner_dropdown_item)
|
||||
habitSpinner.adapter = adapter
|
||||
binding.habitSpinner.adapter = adapter
|
||||
}
|
||||
|
||||
private fun populateActionSpinner(isNumerical: Boolean) {
|
||||
val entries = (if (isNumerical) R.array.actions_numerical else R.array.actions_yes_no)
|
||||
val adapter = ArrayAdapter.createFromResource(context, entries, simple_spinner_item)
|
||||
adapter.setDropDownViewResource(simple_spinner_dropdown_item)
|
||||
actionSpinner.adapter = adapter
|
||||
binding.actionSpinner.adapter = adapter
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,7 +105,12 @@ fun Activity.showSendFileScreen(archiveFilename: String) {
|
||||
})
|
||||
}
|
||||
|
||||
fun View.setupToolbar(toolbar: Toolbar, title: String, color: PaletteColor) {
|
||||
fun View.setupToolbar(
|
||||
toolbar: Toolbar,
|
||||
title: String,
|
||||
color: PaletteColor,
|
||||
displayHomeAsUpEnabled: Boolean = true,
|
||||
) {
|
||||
toolbar.elevation = InterfaceUtils.dpToPixels(context, 2f)
|
||||
val res = StyledResources(context)
|
||||
toolbar.title = title
|
||||
@@ -119,7 +124,7 @@ fun View.setupToolbar(toolbar: Toolbar, title: String, color: PaletteColor) {
|
||||
val activity = context as AppCompatActivity
|
||||
activity.window.statusBarColor = darkerColor
|
||||
activity.setSupportActionBar(toolbar)
|
||||
activity.supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
||||
activity.supportActionBar?.setDisplayHomeAsUpEnabled(displayHomeAsUpEnabled)
|
||||
}
|
||||
|
||||
fun Int.toMeasureSpec(mode: Int) =
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
android:background="@color/white">
|
||||
android:background="?attr/windowBackgroundColor">
|
||||
|
||||
<com.google.android.material.appbar.AppBarLayout
|
||||
android:layout_width="match_parent"
|
||||
|
||||
Reference in New Issue
Block a user