1
0
mirror of https://github.com/dzeiocom/OpenHealth.git synced 2025-04-23 03:12:14 +00:00

misc: Cleanup Core classes

This commit is contained in:
Florian Bouillon 2023-01-07 23:16:31 +01:00
parent b7909df867
commit 9e463d7fd3
Signed by: Florian Bouillon
GPG Key ID: BEEAF3722D0EBF64
13 changed files with 79 additions and 34 deletions

View File

@ -5,6 +5,9 @@ import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
/**
* Base around the Activity class to simplify usage
*/
abstract class BaseActivity<VB : ViewBinding>() : AppCompatActivity() {

View File

@ -6,9 +6,11 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
/**
* Base around the adapter to simplify usage
*/
abstract class BaseAdapter<T, VB : ViewBinding> : RecyclerView.Adapter<BaseViewHolder<VB>>() {
private var items = mutableListOf<T>()
// private var lastPosition = -1
@SuppressLint("NotifyDataSetChanged")
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
/**
* function run when an item is displayed
*/
abstract fun onBindData(holder: BaseViewHolder<VB>, item: T, position: Int)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<VB> {

View File

@ -5,7 +5,9 @@ import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Update
/**
* Base for a DAO interface
*/
interface BaseDao<T> {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(vararg obj: T): List<Long>

View File

@ -3,6 +3,9 @@ package com.dzeio.openhealth.core
import androidx.lifecycle.ViewModelProvider
import androidx.viewbinding.ViewBinding
/**
* Base around the Fragment class to simplify usage
*/
abstract class BaseFragment<VM : BaseViewModel, VB : ViewBinding>(
private val viewModelClass: Class<VM>
) :

View File

@ -13,8 +13,14 @@ import androidx.viewbinding.ViewBinding
import com.dzeio.openhealth.R
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() {
/**
* Lazyload the viewModel
*/
val viewModel by lazy {
ViewModelProvider(this)[viewModelClass]
}
@ -22,14 +28,15 @@ abstract class BaseFullscreenDialog<VM : BaseViewModel, VB : ViewBinding>(privat
private var _binding: VB? = null
val binding get() = _binding!!
/**
* Function to inflate the Fragment Bindings
*/
abstract val bindingInflater: (LayoutInflater) -> VB
abstract val isFullscreenLayout: Boolean
/**
* Function run when the dialog was created
*/
open fun onCreated(savedInstanceState: Bundle?) {}
override fun onCreateView(
@ -59,14 +66,18 @@ abstract class BaseFullscreenDialog<VM : BaseViewModel, VB : ViewBinding>(privat
onDialogInit(dialog)
// onCreated()
dialog
} ?: 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) {
super.onCreateOptionsMenu(menu, inflater)
@ -75,10 +86,6 @@ abstract class BaseFullscreenDialog<VM : BaseViewModel, VB : ViewBinding>(privat
super.onCreateOptionsMenu(menu, inflater)
}
override fun onDestroy() {
super.onDestroy()
}
/**
* Destroy binding
*/

View File

@ -8,8 +8,16 @@ import androidx.fragment.app.DialogFragment
import androidx.viewbinding.ViewBinding
import com.google.android.material.dialog.MaterialAlertDialogBuilder
/**
* Base around the DialogFragment class to simplify usage
*/
abstract class BaseSimpleDialog<VB : ViewBinding> : DialogFragment() {
/**
* Function to inflate the Fragment Bindings
*/
abstract val bindingInflater: (LayoutInflater) -> VB
private var _binding: VB? = null
val binding get() = _binding!!
@ -36,16 +44,20 @@ abstract class BaseSimpleDialog<VB : ViewBinding> : DialogFragment() {
} ?: throw IllegalStateException("Activity cannot be null")
}
/**
* Function to add more customization to the AlertDialogBuilder
*/
open fun onBuilderInit(builder: MaterialAlertDialogBuilder) {}
/**
* Function that allow to modificate some elements of the final dialog
*/
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

View File

@ -1,6 +1,5 @@
package com.dzeio.openhealth.core
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
@ -8,11 +7,23 @@ import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
/**
* Base around the Fragment class to simplify usage
*
* Without ViewModel support (use `BaseFragment` instead)
*/
abstract class BaseStaticFragment<VB : ViewBinding> : Fragment() {
private var _binding: VB? = null
val binding get() = _binding!!
/**
* Function to inflate the Fragment Bindings
*
* use like this: `ViewBinding::inflater`
*/
abstract val bindingInflater: (LayoutInflater, ViewGroup?, Boolean) -> VB
/**
* Setup everything!
*/
@ -28,12 +39,6 @@ abstract class BaseStaticFragment<VB : ViewBinding> : Fragment() {
return binding.root
}
/**
* Function to inflate the Fragment Bindings
*
* use like this: `ViewBinding::inflater`
*/
abstract val bindingInflater: (LayoutInflater, ViewGroup?, Boolean) -> VB
/**
* Destroy binding

View File

@ -3,6 +3,9 @@ package com.dzeio.openhealth.core
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
/**
* Simple implementation of RecyclerView.ViewHolder to limitate usage
*/
class BaseViewHolder<VB : ViewBinding>(
val binding : VB
) : RecyclerView.ViewHolder(binding.root) {

View File

@ -2,4 +2,7 @@ package com.dzeio.openhealth.core
import androidx.lifecycle.ViewModel
/**
* Simple Extension of the base ViewModel
*/
abstract class BaseViewModel : ViewModel()

View File

@ -9,6 +9,9 @@ import androidx.work.Worker
import androidx.work.WorkerParameters
import com.dzeio.openhealth.Application
/**
* Worker Wrapper to simplify work and usage
*/
abstract class BaseWorker(context: Context, params: WorkerParameters) : Worker(context, params) {
companion object {

View File

@ -3,6 +3,9 @@ package com.dzeio.openhealth.core
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
/**
* Simple Observable implementation
*/
open class Observable<T>(baseValue: T) {
private val functionObservers: ArrayList<(T) -> Unit> = ArrayList()

View File

@ -20,7 +20,7 @@ import com.google.android.material.datepicker.DateValidatorPointBackward
import com.google.android.material.datepicker.MaterialDatePicker
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import dagger.hilt.android.AndroidEntryPoint
import java.util.*
import java.util.Date
@AndroidEntryPoint
class EditWaterDialog :
@ -31,8 +31,6 @@ class EditWaterDialog :
private val args: EditWaterDialogArgs by navArgs()
override val isFullscreenLayout = true
var newValue: Int = 0
override fun onDialogInit(dialog: Dialog) {

View File

@ -28,8 +28,6 @@ class EditWeightDialog :
override val bindingInflater: (LayoutInflater) -> DialogEditWeightBinding =
DialogEditWeightBinding::inflate
override val isFullscreenLayout = true
private val args: EditWeightDialogArgs by navArgs()
lateinit var weight: Weight