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