Commit 1c063526 authored by Joey's avatar Joey

insulin: refactor list and editor as Fragments

Signed-off-by: Joey's avatarJoey <bevilacquajoey@gmail.com>
Change-Id: I5d46e8f2a768883456c51de055b5359d5517a48f
parent e0ccb4f5
......@@ -55,12 +55,10 @@ object Activities {
}
}
object Insulin {
object Editor : AddressableActivity {
override val className = "$PACKAGE_NAME.insulin.ui.EditorActivity"
object Insulin : AddressableActivity {
override val className = "$PACKAGE_NAME.insulin.ui.InsulinActivity"
const val EXTRA_UID = "insulin_uid"
}
const val EXTRA_EDITOR_UID = "insulin_uid"
}
object Settings : AddressableActivity {
......
......@@ -13,10 +13,13 @@ import android.app.Activity
import android.view.LayoutInflater
import android.widget.ArrayAdapter
import android.widget.EditText
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.widget.AppCompatSpinner
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.button.MaterialButton
import it.diab.core.util.Activities
import it.diab.core.util.intentTo
import it.diab.data.entities.Glucose
import it.diab.data.entities.Insulin
import it.diab.glucose.R
......@@ -36,6 +39,8 @@ class AddInsulinDialog(
private val addButton: MaterialButton
private val removeButton: MaterialButton
private val editorIcon: ImageView
private lateinit var insulins: Array<Insulin>
init {
......@@ -47,6 +52,7 @@ class AddInsulinDialog(
valueEditText = view.findViewById(R.id.glucose_editor_insulin_value)
addButton = view.findViewById(R.id.glucose_editor_insulin_add)
removeButton = view.findViewById(R.id.glucose_editor_insulin_remove)
editorIcon = view.findViewById(R.id.glucose_editor_insulin_editor)
dialog.setContentView(view)
}
......@@ -77,11 +83,16 @@ class AddInsulinDialog(
}
nameSpinner.setSelection(if (spinnerPosition == -1) 0 else spinnerPosition)
editorIcon.setOnClickListener {
activity.startActivity(intentTo(Activities.Insulin))
}
}
fun show(onAdd: (Insulin, Float) -> Unit, onRemove: () -> Unit) {
if (nameSpinner.adapter.isEmpty) {
VibrationUtil.vibrateForError(activity)
activity.startActivity(intentTo(Activities.Insulin))
Toast.makeText(activity, R.string.glucose_editor_no_insulin, Toast.LENGTH_LONG).show()
return
}
......
......@@ -16,6 +16,16 @@
android:padding="16dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<ImageView
android:id="@+id/glucose_editor_insulin_editor"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_margin="8dp"
android:background="?selectableItemBackgroundBorderless"
android:src="@drawable/ic_editor_insulin"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/glucose_editor_insulin_title"
android:layout_width="0dp"
......
......@@ -12,9 +12,9 @@
<application>
<activity
android:name=".ui.EditorActivity"
android:name=".ui.InsulinActivity"
android:excludeFromRecents="true"
android:theme="@style/AppTheme.DialogActivity">
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
</intent-filter>
......
......@@ -6,7 +6,7 @@
* The text of the license can be found in the LICENSE file
* or at https://www.gnu.org/licenses/gpl.txt
*/
package it.diab.adapters
package it.diab.insulin.adapters
import android.view.LayoutInflater
import android.view.ViewGroup
......@@ -14,11 +14,11 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DiffUtil
import it.diab.R
import it.diab.data.entities.Insulin
import it.diab.core.util.event.Event
import it.diab.holders.InsulinHolderCallbacks
import it.diab.holders.InsulinHolder
import it.diab.data.entities.Insulin
import it.diab.insulin.R
import it.diab.insulin.holders.InsulinHolder
import it.diab.insulin.holders.InsulinHolderCallbacks
class InsulinAdapter : PagedListAdapter<Insulin, InsulinHolder>(CALLBACK), InsulinHolderCallbacks {
......@@ -28,7 +28,7 @@ class InsulinAdapter : PagedListAdapter<Insulin, InsulinHolder>(CALLBACK), Insul
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): InsulinHolder {
return InsulinHolder(
LayoutInflater.from(parent.context)
.inflate(R.layout.item_insulin, parent, false),
.inflate(R.layout.item_list_insulin, parent, false),
this
)
}
......
......@@ -6,33 +6,31 @@
* The text of the license can be found in the LICENSE file
* or at https://www.gnu.org/licenses/gpl.txt
*/
package it.diab.insulin.ui
package it.diab.insulin.fragments
import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.TextView
import androidx.appcompat.R as Rx
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatEditText
import androidx.appcompat.widget.AppCompatSpinner
import androidx.appcompat.widget.SwitchCompat
import androidx.lifecycle.ViewModelProviders
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.button.MaterialButton
import it.diab.core.util.Activities
import it.diab.data.entities.Insulin
import it.diab.data.entities.TimeFrame
import it.diab.data.repositories.InsulinRepository
import it.diab.core.util.Activities
import it.diab.ui.util.UIUtils
import it.diab.insulin.R
import it.diab.insulin.viewmodels.EditorViewModel
import it.diab.insulin.viewmodels.EditorViewModelFactory
import it.diab.ui.util.UIUtils
import it.diab.ui.widgets.BottomSheetDialogFragmentExt
class EditorActivity : AppCompatActivity() {
class EditorFragment : BottomSheetDialogFragmentExt() {
private lateinit var dialog: BottomSheetDialog
private lateinit var titleView: TextView
private lateinit var editText: AppCompatEditText
private lateinit var spinner: AppCompatSpinner
......@@ -42,19 +40,23 @@ class EditorActivity : AppCompatActivity() {
private lateinit var deleteButton: MaterialButton
private lateinit var viewModel: EditorViewModel
private var editMode = false
public override fun onCreate(savedInstance: Bundle?) {
super.onCreate(savedInstance)
private var editMode = false
setFinishOnTouchOutside(true)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val factory = EditorViewModelFactory(InsulinRepository.getInstance(this))
val context = context ?: return
val factory = EditorViewModelFactory(InsulinRepository.getInstance(context))
viewModel = ViewModelProviders.of(this, factory)[EditorViewModel::class.java]
}
val layoutInflater = getSystemService(LayoutInflater::class.java)
@SuppressLint("InflateParams")
val view = layoutInflater.inflate(R.layout.dialog_insulin_edit, null)
override fun onCreateDialogView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val view = inflater.inflate(R.layout.fragment_insulin_edit, container, false)
titleView = view.findViewById(R.id.insulin_edit_dialog_title)
editText = view.findViewById(R.id.insulin_edit_name)
......@@ -64,50 +66,54 @@ class EditorActivity : AppCompatActivity() {
saveButton = view.findViewById(R.id.insulin_edit_save)
deleteButton = view.findViewById(R.id.insulin_edit_delete)
dialog = BottomSheetDialog(this).apply {
setContentView(view)
setOnDismissListener { finish() }
}
UIUtils.setWhiteNavBarIfNeeded(requireContext(), dialog)
setupUI()
UIUtils.setWhiteNavBarIfNeeded(this, dialog)
dialog.show()
return view
}
private fun setupUI() {
val uid = intent.getLongExtra(Activities.Insulin.Editor.EXTRA_UID, -1)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val uid = arguments?.getLong(Activities.Insulin.EXTRA_EDITOR_UID, -1L) ?: -1L
editMode = uid >= 0
viewModel.setInsulin(uid, this::setup)
}
private fun setup(insulin: Insulin) {
val timeFrames = Array(TimeFrame.values().size) { getString(TimeFrame.values()[it].string) }
titleView.text = getString(if (editMode) R.string.insulin_editor_edit else R.string.insulin_editor_add)
spinner.adapter = ArrayAdapter(
this,
Rx.layout.support_simple_spinner_dropdown_item,
timeFrames
)
editText.setText(insulin.name)
basalSwitch.isChecked = insulin.isBasal
halfUnitsSwitch.isChecked = insulin.hasHalfUnits
saveButton.setOnClickListener { onSaveInsulin() }
setupSpinner(insulin)
saveButton.setOnClickListener { saveInsulin() }
deleteButton.setOnClickListener { deleteInsulin() }
if (!editMode) {
titleView.text = getString(R.string.insulin_editor_add)
deleteButton.visibility = View.GONE
return
}
}
private fun setupSpinner(insulin: Insulin) {
val context = context ?: return
titleView.text = getString(R.string.insulin_editor_edit)
deleteButton.setOnClickListener { onDeleteInsulin() }
val timeFrames = TimeFrame.values()
spinner.apply {
adapter = ArrayAdapter(
context,
androidx.appcompat.R.layout.support_simple_spinner_dropdown_item,
timeFrames.map { timeFrame -> getString(timeFrame.string) }
)
viewModel.setInsulin(uid) {
editText.setText(it.name)
spinner.setSelection(if (editMode) it.timeFrame.toInt() + 1 else 0)
basalSwitch.isChecked = it.isBasal
halfUnitsSwitch.isChecked = it.hasHalfUnits
setSelection(insulin.timeFrame.toInt() + 1)
}
}
private fun onSaveInsulin() {
viewModel.insulin.run {
private fun saveInsulin() {
viewModel.insulin.apply {
name = editText.text.toString()
isBasal = basalSwitch.isChecked
hasHalfUnits = halfUnitsSwitch.isChecked
......@@ -115,11 +121,11 @@ class EditorActivity : AppCompatActivity() {
}
viewModel.save()
dialog.dismiss()
dismiss()
}
private fun onDeleteInsulin() {
private fun deleteInsulin() {
viewModel.delete()
dialog.dismiss()
dismiss()
}
}
}
\ No newline at end of file
......@@ -6,40 +6,37 @@
* The text of the license can be found in the LICENSE file
* or at https://www.gnu.org/licenses/gpl.txt
*/
package it.diab.fragments
package it.diab.insulin.fragments
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.paging.PagedList
import androidx.recyclerview.widget.RecyclerView
import it.diab.R
import it.diab.adapters.InsulinAdapter
import it.diab.data.entities.Insulin
import it.diab.data.repositories.InsulinRepository
import it.diab.core.util.Activities
import it.diab.core.util.event.EventObserver
import it.diab.core.util.intentTo
import it.diab.viewmodels.insulin.InsulinViewModel
import it.diab.viewmodels.insulin.InsulinViewModelFactory
import it.diab.data.repositories.InsulinRepository
import it.diab.insulin.R
import it.diab.insulin.adapters.InsulinAdapter
import it.diab.insulin.viewmodels.ListViewModel
import it.diab.insulin.viewmodels.ListViewModelFactory
class InsulinFragment : BaseFragment() {
override val titleRes = R.string.fragment_insulin
class ListFragment : Fragment() {
private lateinit var recyclerView: RecyclerView
private lateinit var viewModel: InsulinViewModel
private lateinit var viewModel: ListViewModel
override fun onCreate(savedInstance: Bundle?) {
super.onCreate(savedInstance)
val context = context ?: return
val factory = InsulinViewModelFactory(InsulinRepository.getInstance(context))
viewModel = ViewModelProviders.of(this, factory)[InsulinViewModel::class.java]
val factory = ListViewModelFactory(InsulinRepository.getInstance(context))
viewModel = ViewModelProviders.of(this, factory)[ListViewModel::class.java]
}
override fun onCreateView(
......@@ -47,21 +44,26 @@ class InsulinFragment : BaseFragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_insulin, container, false)
val view = inflater.inflate(R.layout.fragment_insulin_list, container, false)
recyclerView = view.findViewById(R.id.insulin_list)
val adapter = InsulinAdapter()
recyclerView.adapter = adapter
viewModel.list.observe(viewLifecycleOwner, Observer<PagedList<Insulin>>(adapter::submitList))
viewModel.list.observe(viewLifecycleOwner, Observer(adapter::submitList))
adapter.editInsulin.observe(viewLifecycleOwner, EventObserver(this::onItemClick))
return view
}
private fun onItemClick(uid: Long) {
val intent = intentTo(Activities.Insulin.Editor).apply {
putExtra(Activities.Insulin.Editor.EXTRA_UID, uid)
val editorFragment = EditorFragment().apply {
arguments = Bundle().apply {
putLong(Activities.Insulin.EXTRA_EDITOR_UID, uid)
}
}
startActivity(intent)
childFragmentManager.beginTransaction()
.add(editorFragment, "editor")
.commit()
}
}
......@@ -6,14 +6,14 @@
* The text of the license can be found in the LICENSE file
* or at https://www.gnu.org/licenses/gpl.txt
*/
package it.diab.holders
package it.diab.insulin.holders
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import it.diab.R
import it.diab.data.entities.Insulin
import it.diab.insulin.R
class InsulinHolder(
view: View,
......
......@@ -6,7 +6,7 @@
* The text of the license can be found in the LICENSE file
* or at https://www.gnu.org/licenses/gpl.txt
*/
package it.diab.holders
package it.diab.insulin.holders
interface InsulinHolderCallbacks {
......
/*
* Copyright (c) 2019 Bevilacqua Joey
*
* Licensed under the GNU GPLv3 license
*
* The text of the license can be found in the LICENSE file
* or at https://www.gnu.org/licenses/gpl.txt
*/
package it.diab.insulin.ui
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import it.diab.insulin.R
class InsulinActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_insulin)
}
}
\ No newline at end of file
......@@ -6,13 +6,13 @@
* The text of the license can be found in the LICENSE file
* or at https://www.gnu.org/licenses/gpl.txt
*/
package it.diab.viewmodels.insulin
package it.diab.insulin.viewmodels
import androidx.lifecycle.ViewModel
import androidx.paging.LivePagedListBuilder
import it.diab.data.repositories.InsulinRepository
class InsulinViewModel internal constructor(
class ListViewModel internal constructor(
insulinRepository: InsulinRepository
) : ViewModel() {
......
......@@ -6,17 +6,17 @@
* The text of the license can be found in the LICENSE file
* or at https://www.gnu.org/licenses/gpl.txt
*/
package it.diab.viewmodels.insulin
package it.diab.insulin.viewmodels
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import it.diab.data.repositories.InsulinRepository
class InsulinViewModelFactory(
class ListViewModelFactory(
private val insulinRepository: InsulinRepository
) : ViewModelProvider.NewInstanceFactory() {
@Suppress("unchecked_cast")
override fun <T : ViewModel?> create(modelClass: Class<T>) =
InsulinViewModel(insulinRepository) as T
ListViewModel(insulinRepository) as T
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (c) 2018 Bevilacqua Joey
Copyright (c) 2019 Bevilacqua Joey
Licensed under the GNU GPLv3 license
......
<!--
Copyright (c) 2018 Bevilacqua Joey
Licensed under the GNU GPLv3 license
The text of the license can be found in the LICENSE file
or at https://www.gnu.org/licenses/gpl.txt
-->
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:name="it.diab.insulin.fragments.ListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:tag="it.diab.insulin.fragments.ListFragment" />
......@@ -11,8 +11,6 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="288dp"
android:orientation="vertical"
android:paddingStart="16dp"
android:paddingTop="16dp"
android:paddingEnd="16dp">
......
......@@ -17,4 +17,4 @@
android:paddingEnd="4dp"
app:gridColumns="2"
app:gridLayout="true"
tools:listitem="@layout/item_insulin" />
tools:listitem="@layout/item_list_insulin" />
......@@ -8,6 +8,9 @@
-->
<resources>
<!-- Insulin -->
<string name="insulin_add_item">जोड़े</string>
<!-- Insulin Editor -->
<string name="insulin_editor_add">इंसुलिन जोड़े</string>
<string name="insulin_editor_edit">इंसुलिन संपादित करें</string>
......
......@@ -8,6 +8,9 @@
-->
<resources>
<!-- Insulin -->
<string name="insulin_add_item">Aggiungi</string>
<!-- Insulin Editor -->
<string name="insulin_editor_add">Aggungi insulina</string>
<string name="insulin_editor_edit">Modifica insulina</string>
......
......@@ -8,6 +8,9 @@
-->
<resources>
<!-- Insulin -->
<string name="insulin_add_item">Add</string>
<!-- Insulin Editor -->
<string name="insulin_editor_add">Add insulin</string>
<string name="insulin_editor_edit">Edit insulin</string>
......
/*
* Copyright (c) 2019 Bevilacqua Joey
*
* Licensed under the GNU GPLv3 license
*
* The text of the license can be found in the LICENSE file
* or at https://www.gnu.org/licenses/gpl.txt
*/
package it.diab.ui.widgets
import android.os.Bundle
import android.view.ContextThemeWrapper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import it.diab.ui.R
abstract class BottomSheetDialogFragmentExt : BottomSheetDialogFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val dialogInflater = LayoutInflater.from(ContextThemeWrapper(requireContext(), R.style.AppTheme))
return onCreateDialogView(dialogInflater, container, savedInstanceState)
}
override fun getTheme() = R.style.AppTheme_BottomSheetDialog
abstract fun onCreateDialogView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment