mirror of
https://github.com/dzeiocom/OpenHealth.git
synced 2025-04-23 11:22:10 +00:00
misc: Cleanup Core classes
This commit is contained in:
parent
b7909df867
commit
9e463d7fd3
@ -5,6 +5,9 @@ import android.view.LayoutInflater
|
|||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.viewbinding.ViewBinding
|
import androidx.viewbinding.ViewBinding
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base around the Activity class to simplify usage
|
||||||
|
*/
|
||||||
abstract class BaseActivity<VB : ViewBinding>() : AppCompatActivity() {
|
abstract class BaseActivity<VB : ViewBinding>() : AppCompatActivity() {
|
||||||
|
|
||||||
|
|
||||||
@ -28,4 +31,4 @@ abstract class BaseActivity<VB : ViewBinding>() : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected open fun onCreated(savedInstanceState: Bundle?) {}
|
protected open fun onCreated(savedInstanceState: Bundle?) {}
|
||||||
}
|
}
|
||||||
|
@ -6,9 +6,11 @@ import android.view.ViewGroup
|
|||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import androidx.viewbinding.ViewBinding
|
import androidx.viewbinding.ViewBinding
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base around the adapter to simplify usage
|
||||||
|
*/
|
||||||
abstract class BaseAdapter<T, VB : ViewBinding> : RecyclerView.Adapter<BaseViewHolder<VB>>() {
|
abstract class BaseAdapter<T, VB : ViewBinding> : RecyclerView.Adapter<BaseViewHolder<VB>>() {
|
||||||
private var items = mutableListOf<T>()
|
private var items = mutableListOf<T>()
|
||||||
// private var lastPosition = -1
|
|
||||||
|
|
||||||
@SuppressLint("NotifyDataSetChanged")
|
@SuppressLint("NotifyDataSetChanged")
|
||||||
fun set(items: List<T>) {
|
fun set(items: List<T>) {
|
||||||
@ -29,6 +31,9 @@ abstract class BaseAdapter<T, VB : ViewBinding> : RecyclerView.Adapter<BaseViewH
|
|||||||
*/
|
*/
|
||||||
abstract val bindingInflater: (LayoutInflater, ViewGroup?, Boolean) -> VB
|
abstract val bindingInflater: (LayoutInflater, ViewGroup?, Boolean) -> VB
|
||||||
|
|
||||||
|
/**
|
||||||
|
* function run when an item is displayed
|
||||||
|
*/
|
||||||
abstract fun onBindData(holder: BaseViewHolder<VB>, item: T, position: Int)
|
abstract fun onBindData(holder: BaseViewHolder<VB>, item: T, position: Int)
|
||||||
|
|
||||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<VB> {
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<VB> {
|
||||||
@ -43,4 +48,4 @@ abstract class BaseAdapter<T, VB : ViewBinding> : RecyclerView.Adapter<BaseViewH
|
|||||||
|
|
||||||
override fun getItemCount(): Int = items.size
|
override fun getItemCount(): Int = items.size
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,9 @@ import androidx.room.Insert
|
|||||||
import androidx.room.OnConflictStrategy
|
import androidx.room.OnConflictStrategy
|
||||||
import androidx.room.Update
|
import androidx.room.Update
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base for a DAO interface
|
||||||
|
*/
|
||||||
interface BaseDao<T> {
|
interface BaseDao<T> {
|
||||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||||
suspend fun insert(vararg obj: T): List<Long>
|
suspend fun insert(vararg obj: T): List<Long>
|
||||||
@ -18,4 +20,4 @@ interface BaseDao<T> {
|
|||||||
|
|
||||||
@Delete
|
@Delete
|
||||||
suspend fun delete(vararg obj: T)
|
suspend fun delete(vararg obj: T)
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,9 @@ package com.dzeio.openhealth.core
|
|||||||
import androidx.lifecycle.ViewModelProvider
|
import androidx.lifecycle.ViewModelProvider
|
||||||
import androidx.viewbinding.ViewBinding
|
import androidx.viewbinding.ViewBinding
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base around the Fragment class to simplify usage
|
||||||
|
*/
|
||||||
abstract class BaseFragment<VM : BaseViewModel, VB : ViewBinding>(
|
abstract class BaseFragment<VM : BaseViewModel, VB : ViewBinding>(
|
||||||
private val viewModelClass: Class<VM>
|
private val viewModelClass: Class<VM>
|
||||||
) :
|
) :
|
||||||
|
@ -13,8 +13,14 @@ import androidx.viewbinding.ViewBinding
|
|||||||
import com.dzeio.openhealth.R
|
import com.dzeio.openhealth.R
|
||||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base around the DialogFragment class to simplify usage
|
||||||
|
*/
|
||||||
abstract class BaseFullscreenDialog<VM : BaseViewModel, VB : ViewBinding>(private val viewModelClass: Class<VM>) : DialogFragment() {
|
abstract class BaseFullscreenDialog<VM : BaseViewModel, VB : ViewBinding>(private val viewModelClass: Class<VM>) : DialogFragment() {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lazyload the viewModel
|
||||||
|
*/
|
||||||
val viewModel by lazy {
|
val viewModel by lazy {
|
||||||
ViewModelProvider(this)[viewModelClass]
|
ViewModelProvider(this)[viewModelClass]
|
||||||
}
|
}
|
||||||
@ -22,14 +28,15 @@ abstract class BaseFullscreenDialog<VM : BaseViewModel, VB : ViewBinding>(privat
|
|||||||
private var _binding: VB? = null
|
private var _binding: VB? = null
|
||||||
val binding get() = _binding!!
|
val binding get() = _binding!!
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Function to inflate the Fragment Bindings
|
* Function to inflate the Fragment Bindings
|
||||||
*/
|
*/
|
||||||
abstract val bindingInflater: (LayoutInflater) -> VB
|
abstract val bindingInflater: (LayoutInflater) -> VB
|
||||||
|
|
||||||
abstract val isFullscreenLayout: Boolean
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function run when the dialog was created
|
||||||
|
*/
|
||||||
open fun onCreated(savedInstanceState: Bundle?) {}
|
open fun onCreated(savedInstanceState: Bundle?) {}
|
||||||
|
|
||||||
override fun onCreateView(
|
override fun onCreateView(
|
||||||
@ -59,14 +66,18 @@ abstract class BaseFullscreenDialog<VM : BaseViewModel, VB : ViewBinding>(privat
|
|||||||
|
|
||||||
onDialogInit(dialog)
|
onDialogInit(dialog)
|
||||||
|
|
||||||
// onCreated()
|
|
||||||
|
|
||||||
dialog
|
dialog
|
||||||
} ?: throw IllegalStateException("Activity cannot be null")
|
} ?: throw IllegalStateException("Activity cannot be null")
|
||||||
}
|
}
|
||||||
|
|
||||||
open fun onDialogInit(dialog: Dialog): Unit {}
|
/**
|
||||||
|
* Function to modify the Dialog
|
||||||
|
*/
|
||||||
|
open fun onDialogInit(dialog: Dialog) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FIXME: Remove it from the Base and put it in the implementations
|
||||||
|
*/
|
||||||
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
|
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
|
||||||
super.onCreateOptionsMenu(menu, inflater)
|
super.onCreateOptionsMenu(menu, inflater)
|
||||||
|
|
||||||
@ -75,10 +86,6 @@ abstract class BaseFullscreenDialog<VM : BaseViewModel, VB : ViewBinding>(privat
|
|||||||
super.onCreateOptionsMenu(menu, inflater)
|
super.onCreateOptionsMenu(menu, inflater)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
|
||||||
super.onDestroy()
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Destroy binding
|
* Destroy binding
|
||||||
*/
|
*/
|
||||||
@ -86,4 +93,4 @@ abstract class BaseFullscreenDialog<VM : BaseViewModel, VB : ViewBinding>(privat
|
|||||||
super.onDestroyView()
|
super.onDestroyView()
|
||||||
_binding = null
|
_binding = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,8 +8,16 @@ import androidx.fragment.app.DialogFragment
|
|||||||
import androidx.viewbinding.ViewBinding
|
import androidx.viewbinding.ViewBinding
|
||||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base around the DialogFragment class to simplify usage
|
||||||
|
*/
|
||||||
abstract class BaseSimpleDialog<VB : ViewBinding> : DialogFragment() {
|
abstract class BaseSimpleDialog<VB : ViewBinding> : DialogFragment() {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function to inflate the Fragment Bindings
|
||||||
|
*/
|
||||||
|
abstract val bindingInflater: (LayoutInflater) -> VB
|
||||||
|
|
||||||
private var _binding: VB? = null
|
private var _binding: VB? = null
|
||||||
val binding get() = _binding!!
|
val binding get() = _binding!!
|
||||||
|
|
||||||
@ -36,16 +44,20 @@ abstract class BaseSimpleDialog<VB : ViewBinding> : DialogFragment() {
|
|||||||
} ?: throw IllegalStateException("Activity cannot be null")
|
} ?: throw IllegalStateException("Activity cannot be null")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function to add more customization to the AlertDialogBuilder
|
||||||
|
*/
|
||||||
open fun onBuilderInit(builder: MaterialAlertDialogBuilder) {}
|
open fun onBuilderInit(builder: MaterialAlertDialogBuilder) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function that allow to modificate some elements of the final dialog
|
||||||
|
*/
|
||||||
open fun onDialogInit(dialog: AlertDialog) {}
|
open fun onDialogInit(dialog: AlertDialog) {}
|
||||||
|
|
||||||
open fun onCreated() {}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Function to inflate the Fragment Bindings
|
* Function run when the dialog is created
|
||||||
*/
|
*/
|
||||||
abstract val bindingInflater: (LayoutInflater) -> VB
|
open fun onCreated() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Destroy binding
|
* Destroy binding
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package com.dzeio.openhealth.core
|
package com.dzeio.openhealth.core
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
@ -8,11 +7,23 @@ import android.view.ViewGroup
|
|||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import androidx.viewbinding.ViewBinding
|
import androidx.viewbinding.ViewBinding
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base around the Fragment class to simplify usage
|
||||||
|
*
|
||||||
|
* Without ViewModel support (use `BaseFragment` instead)
|
||||||
|
*/
|
||||||
abstract class BaseStaticFragment<VB : ViewBinding> : Fragment() {
|
abstract class BaseStaticFragment<VB : ViewBinding> : Fragment() {
|
||||||
|
|
||||||
private var _binding: VB? = null
|
private var _binding: VB? = null
|
||||||
val binding get() = _binding!!
|
val binding get() = _binding!!
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function to inflate the Fragment Bindings
|
||||||
|
*
|
||||||
|
* use like this: `ViewBinding::inflater`
|
||||||
|
*/
|
||||||
|
abstract val bindingInflater: (LayoutInflater, ViewGroup?, Boolean) -> VB
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setup everything!
|
* Setup everything!
|
||||||
*/
|
*/
|
||||||
@ -28,12 +39,6 @@ abstract class BaseStaticFragment<VB : ViewBinding> : Fragment() {
|
|||||||
return binding.root
|
return binding.root
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Function to inflate the Fragment Bindings
|
|
||||||
*
|
|
||||||
* use like this: `ViewBinding::inflater`
|
|
||||||
*/
|
|
||||||
abstract val bindingInflater: (LayoutInflater, ViewGroup?, Boolean) -> VB
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Destroy binding
|
* Destroy binding
|
||||||
|
@ -3,7 +3,10 @@ package com.dzeio.openhealth.core
|
|||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import androidx.viewbinding.ViewBinding
|
import androidx.viewbinding.ViewBinding
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple implementation of RecyclerView.ViewHolder to limitate usage
|
||||||
|
*/
|
||||||
class BaseViewHolder<VB : ViewBinding>(
|
class BaseViewHolder<VB : ViewBinding>(
|
||||||
val binding : VB
|
val binding : VB
|
||||||
) : RecyclerView.ViewHolder(binding.root) {
|
) : RecyclerView.ViewHolder(binding.root) {
|
||||||
}
|
}
|
||||||
|
@ -2,4 +2,7 @@ package com.dzeio.openhealth.core
|
|||||||
|
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
|
|
||||||
abstract class BaseViewModel : ViewModel()
|
/**
|
||||||
|
* Simple Extension of the base ViewModel
|
||||||
|
*/
|
||||||
|
abstract class BaseViewModel : ViewModel()
|
||||||
|
@ -9,6 +9,9 @@ import androidx.work.Worker
|
|||||||
import androidx.work.WorkerParameters
|
import androidx.work.WorkerParameters
|
||||||
import com.dzeio.openhealth.Application
|
import com.dzeio.openhealth.Application
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Worker Wrapper to simplify work and usage
|
||||||
|
*/
|
||||||
abstract class BaseWorker(context: Context, params: WorkerParameters) : Worker(context, params) {
|
abstract class BaseWorker(context: Context, params: WorkerParameters) : Worker(context, params) {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@ -19,4 +22,4 @@ abstract class BaseWorker(context: Context, params: WorkerParameters) : Worker(c
|
|||||||
.enqueueUniquePeriodicWork(tag, ExistingPeriodicWorkPolicy.KEEP, request)
|
.enqueueUniquePeriodicWork(tag, ExistingPeriodicWorkPolicy.KEEP, request)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,9 @@ package com.dzeio.openhealth.core
|
|||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple Observable implementation
|
||||||
|
*/
|
||||||
open class Observable<T>(baseValue: T) {
|
open class Observable<T>(baseValue: T) {
|
||||||
|
|
||||||
private val functionObservers: ArrayList<(T) -> Unit> = ArrayList()
|
private val functionObservers: ArrayList<(T) -> Unit> = ArrayList()
|
||||||
|
@ -20,7 +20,7 @@ import com.google.android.material.datepicker.DateValidatorPointBackward
|
|||||||
import com.google.android.material.datepicker.MaterialDatePicker
|
import com.google.android.material.datepicker.MaterialDatePicker
|
||||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import java.util.*
|
import java.util.Date
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class EditWaterDialog :
|
class EditWaterDialog :
|
||||||
@ -31,8 +31,6 @@ class EditWaterDialog :
|
|||||||
|
|
||||||
private val args: EditWaterDialogArgs by navArgs()
|
private val args: EditWaterDialogArgs by navArgs()
|
||||||
|
|
||||||
override val isFullscreenLayout = true
|
|
||||||
|
|
||||||
var newValue: Int = 0
|
var newValue: Int = 0
|
||||||
|
|
||||||
override fun onDialogInit(dialog: Dialog) {
|
override fun onDialogInit(dialog: Dialog) {
|
||||||
|
@ -28,8 +28,6 @@ class EditWeightDialog :
|
|||||||
override val bindingInflater: (LayoutInflater) -> DialogEditWeightBinding =
|
override val bindingInflater: (LayoutInflater) -> DialogEditWeightBinding =
|
||||||
DialogEditWeightBinding::inflate
|
DialogEditWeightBinding::inflate
|
||||||
|
|
||||||
override val isFullscreenLayout = true
|
|
||||||
|
|
||||||
private val args: EditWeightDialogArgs by navArgs()
|
private val args: EditWeightDialogArgs by navArgs()
|
||||||
|
|
||||||
lateinit var weight: Weight
|
lateinit var weight: Weight
|
||||||
|
Loading…
x
Reference in New Issue
Block a user