Skip to content

Commit

Permalink
Merge pull request #126 from choffmann/web-compose-frontend
Browse files Browse the repository at this point in the history
Rework EntryViewModel and various Web compose frontend stuff
  • Loading branch information
choffmann authored Jun 24, 2022
2 parents d0070c1 + 9f3c0da commit 9b3412c
Show file tree
Hide file tree
Showing 56 changed files with 3,380 additions and 1,505 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,12 @@ data class Category(
val image: Image? = null,
val budget: Float? = null
)
@Serializable
data class Nullable(
val id: Int?,
val name: String,
val color: String,
val image: Image,
val budget: Float
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import de.hsfl.budgetBinder.presentation.flow.RouterFlow
import de.hsfl.budgetBinder.presentation.flow.UiEventSharedFlow
import de.hsfl.budgetBinder.presentation.viewmodel.auth.login.LoginViewModel
import de.hsfl.budgetBinder.presentation.viewmodel.auth.register.RegisterViewModel
import de.hsfl.budgetBinder.presentation.viewmodel.*
import de.hsfl.budgetBinder.presentation.viewmodel.category.summary.CategorySummaryViewModel
import de.hsfl.budgetBinder.presentation.viewmodel.category._CategoryViewModel
import de.hsfl.budgetBinder.presentation.viewmodel.category.create.CategoryCreateViewModel
import de.hsfl.budgetBinder.presentation.viewmodel.category.detail.CategoryDetailViewModel
import de.hsfl.budgetBinder.presentation.viewmodel.category.edit.CategoryEditViewModel
import de.hsfl.budgetBinder.presentation.viewmodel.dashboard.DashboardViewModel
import de.hsfl.budgetBinder.presentation.viewmodel.entry.EntryViewModel
import de.hsfl.budgetBinder.presentation.viewmodel.navdrawer.NavDrawerViewModel
import de.hsfl.budgetBinder.presentation.viewmodel.settings.SettingsEditServerUrlViewModel
import de.hsfl.budgetBinder.presentation.viewmodel.settings.SettingsEditUserViewModel
Expand Down Expand Up @@ -79,7 +79,7 @@ fun kodein(ktorEngine: HttpClientEngine) = DI {
bindSingleton { StoreUserStateUseCase() }
bindSingleton { StoreServerUrlUseCase() }
bindSingleton { StoreDarkModeUseCase() }
bindSingleton { EntriesUseCases(instance(), instance(), instance(), instance(), instance()) }
bindSingleton { EntryUseCases(instance(), instance(), instance(), instance(), instance(), instance()) }
bindSingleton { CategoriesUseCases(instance(), instance(), instance(), instance(), instance(), instance()) }
bindSingleton { SettingsUseCases(instance(), instance(), instance()) }
bindSingleton { AuthUseCases(instance(), instance(), instance()) }
Expand All @@ -102,7 +102,7 @@ fun kodein(ktorEngine: HttpClientEngine) = DI {
bindSingleton { CategoryDetailViewModel(instance(), instance(), instance()) }
bindSingleton { CategoryEditViewModel(instance(), instance(), instance()) }
bindSingleton { CategoryCreateViewModel(instance(), instance(), instance()) }
bindSingleton { EntryViewModel(instance(), instance()) }
bindSingleton { EntryViewModel(instance(), instance(), instance()) }
bindSingleton { DashboardViewModel(instance(), instance(), instance(), instance()) }
bindSingleton { NavDrawerViewModel(instance(), instance(), instance()) }
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package de.hsfl.budgetBinder.domain.usecase

data class EntriesUseCases(
data class EntryUseCases(
val getAllEntriesUseCase: GetAllEntriesUseCase,
val getCategoryListUseCase : GetAllCategoriesUseCase,
val getEntryByIdUseCase: GetEntryByIdUseCase,
val createNewEntryUseCase: CreateNewEntryUseCase,
val changeEntryByIdUseCase: ChangeEntryByIdUseCase,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package de.hsfl.budgetBinder.presentation


sealed class Screen {
sealed class Welcome: Screen() {
object Screen1: Welcome()
Expand All @@ -21,29 +19,10 @@ sealed class Screen {
}
sealed class Entry: Screen() {
data class Overview(val id: Int): Entry()
object Edit: Entry()
data class Edit(val id: Int): Entry()
object Create: Entry()
}
object Login : Screen()
object Register : Screen()
object Dashboard : Screen()

@Deprecated(message = "use sealed class Welcome")
object _Welcome : Screen()
@Deprecated(message = "use sealed class Settings")
object _Settings : Screen()
@Deprecated(message = "use sealed class Settings")
object SettingsChangeUserData : Screen()
@Deprecated(message = "use sealed class Category")
object CategorySummary : Screen()
@Deprecated(message = "use sealed class Category")
object CategoryEdit : Screen()
@Deprecated(message = "use sealed class Category")
object CategoryCreate : Screen()
@Deprecated(message = "use sealed class Category")
object CategoryCreateOnRegister : Screen()
@Deprecated(message = "use sealed class Entry")
object EntryEdit : Screen()
@Deprecated(message = "use sealed class Entry")
object EntryCreate : Screen()
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.flow.*

class EntryViewModel(
private val entriesUseCases: EntriesUseCases,
private val entryUseCases: EntryUseCases,
private val scope: CoroutineScope = CoroutineScope(Dispatchers.Unconfined + SupervisorJob())
) {
private val _state = MutableStateFlow<UiState>(UiState.Empty)
val state: StateFlow<UiState> = _state

fun getAllEntries() {
entriesUseCases.getAllEntriesUseCase().onEach {
entryUseCases.getAllEntriesUseCase().onEach {
when (it) {
is DataResponse.Success -> _state.value = UiState.Success(it.data)
is DataResponse.Error -> _state.value = UiState.Error(it.error!!.message)
Expand All @@ -28,7 +28,7 @@ class EntryViewModel(
}

fun getEntryById(id: Int) {
entriesUseCases.getEntryByIdUseCase(id).onEach {
entryUseCases.getEntryByIdUseCase(id).onEach {
when (it) {
is DataResponse.Success -> _state.value = UiState.Success(it.data)
is DataResponse.Error -> _state.value = UiState.Error(it.error!!.message)
Expand All @@ -39,7 +39,7 @@ class EntryViewModel(
}

fun createEntry(entry: Entry.In) {
entriesUseCases.createNewEntryUseCase(entry).onEach {
entryUseCases.createNewEntryUseCase(entry).onEach {
when (it) {
is DataResponse.Success -> _state.value = UiState.Success(it.data)
is DataResponse.Error -> _state.value = UiState.Error(it.error!!.message)
Expand All @@ -50,7 +50,7 @@ class EntryViewModel(
}

fun changeEntry(entry: Entry.Patch, id: Int) {
entriesUseCases.changeEntryByIdUseCase(entry, id).onEach {
entryUseCases.changeEntryByIdUseCase(entry, id).onEach {
when (it) {
is DataResponse.Success -> _state.value = UiState.Success(it.data)
is DataResponse.Error -> _state.value = UiState.Error(it.error!!.message)
Expand All @@ -61,7 +61,7 @@ class EntryViewModel(
}

fun removeEntry(id: Int) {
entriesUseCases.deleteEntryByIdUseCase(id).onEach {
entryUseCases.deleteEntryByIdUseCase(id).onEach {
when (it) {
is DataResponse.Success -> _state.value = UiState.Success(it.data)
is DataResponse.Error -> _state.value = UiState.Error(it.error!!.message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class LoginViewModel(

private fun validateInput() {
if (validateEmail(email = emailText.value.email)) {
//TODO: Check what frontend this is opened from.
// Web -> super.login(...),
// everyone else -> toggleDialog()
toggleDialog()
} else {
_emailText.value = emailText.value.copy(emailValid = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ class CategoryEditViewModel(
onSuccess = { routerFlow.navigateTo(Screen.Category.Summary) }
)
is CategoryEditEvent.LifeCycle -> event.value.handleLifeCycle(
onLaunch = { initSateFlows() },
onLaunch = { initStateFlows() },
onDispose = { resetStateFlows() }
)
}
}

private fun initSateFlows() {
private fun initStateFlows() {
super.updateCurrentCategoryId()
super.getById(id = currentCategoryId, onSuccess = { category ->
_categoryState.value = category
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package de.hsfl.budgetBinder.presentation.viewmodel.entry

import de.hsfl.budgetBinder.presentation.event.LifecycleEvent

// View is not allowed to declare what should be done, only notify what has happened, names are assigned as such

sealed class EntryEvent {
//User-made Data Input
data class EnteredName(val value: String) : EntryEvent()
data class EnteredAmount(val value: Float) : EntryEvent()
object EnteredRepeat : EntryEvent()
data class EnteredCategoryID(val value: Int?) : EntryEvent()
object EnteredAmountSign : EntryEvent()

//Action
object OnCreateEntry : EntryEvent()
object OnEditEntry : EntryEvent()
object OnDeleteEntry : EntryEvent()
object OnDeleteDialogConfirm : EntryEvent()
object OnDeleteDialogDismiss : EntryEvent()
object OnCancel: EntryEvent()


//LifeCycle
data class LifeCycle(val value: LifecycleEvent): EntryEvent()

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package de.hsfl.budgetBinder.presentation.viewmodel.entry

data class EntryInputState(
val name: String = "",
val amount: Float = 0f,
val repeat: Boolean = false,
val categoryID: Int? = null,
val amountSign: Boolean = false
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package de.hsfl.budgetBinder.presentation.viewmodel.entry

import de.hsfl.budgetBinder.common.Category
import de.hsfl.budgetBinder.common.Entry

data class EntryState(
val selectedEntry: Entry = Entry(0,"",0f,false,null),
val categoryList: List<Category> = listOf(Category(0,"No category","ffffff",Category.Image.DEFAULT,0f))
)
Loading

0 comments on commit 9b3412c

Please sign in to comment.