4,960 questions
Best practices
0
votes
2
replies
54
views
Should a ViewModel live as long as the entire app? ViewModel inside Scaffold with BottomSheet + Google Maps
I am building a Jetpack Compose app that uses a Scaffold with a persistent bottom sheet.
The main content of the Scaffold show Google Maps, and the bottom sheet shows a menu.
I have a MapViewModel ...
Best practices
0
votes
2
replies
51
views
Add a NotMapped field to my model or create a view model to interface between the model and view?
I come from a MVVM in WPF background moving towards ASP.NET MVC. I have a model that is composed of another model called Message, like so:
public class User
{
public int Id { get; set; }
[...
0
votes
1
answer
85
views
Should I always initialize a viewmodel at the start? [closed]
Should I always initialize a viewmodel in the Activity, before onCreate() even, if it is needed later in the flow?
I do not know and am concerned if initialization of all the viewmodels right at the ...
1
vote
1
answer
81
views
Differences between long-running Flow processing with transform() and flatMapLatest()
I have a data stream coming from the server. The server does not provide any information about when the next piece of data will arrive. I have a Flow in my repository that emits data as it arrives. ...
0
votes
1
answer
51
views
Android ViewModel setValue function get stuck
To process the live data for signals, I defined following class SignalsViewModel and when I call the updateSignals function, it get stuck and doesn't run the next command.
I create the ...
1
vote
1
answer
150
views
Differences and best practices for using ViewModels in XML vs Jetpack Compose
I’m working on Android and I have some doubts about how to properly manage ViewModels depending on the type of UI I’m using (XML with Activities/Fragments vs Jetpack Compose).
Currently, my main ...
-6
votes
1
answer
129
views
SwiftUI alert @Binding view model isn't found in my view
I'm getting an Xcode error:
Cannot find $loginVM in scope.
I here's the simplified viewModel
import Foundation
import SwiftUI
@Observable
class LoginViewModel {
var showApiAlert: Bool = false
...
1
vote
1
answer
118
views
C# WPF strict MVVM: Basic TreeView SelectedItem ViewModel Problem
I’m trying to bind the SelectedItem of a WPF TreeView to a property in my MainViewModel in a pure MVVM setup (no code-behind).
Since TreeView.SelectedItem is not a dependency property, I created an ...
1
vote
1
answer
67
views
pointerInput in Canvas resets ViewModel's state to its previous value
I have a composable which uses Canvas to draw tasks on a clock-like dial. The tasks are read from a ViewModel state, which in turn loads them from a database. The list of tasks varies depending on the ...
0
votes
1
answer
55
views
What is the right way to instantiate a ViewModel inside a Tab Fragment and scope it to the parent navigation destination?
I'm using Navigation Component + Hilt DI. I have a Navigation Destination Fragment which hosts a viewpager with 2 fixed fragments. What is the right way to Instantiate the ViewModels within those ...
0
votes
1
answer
107
views
How to change a button control within a Collection View in Maui
How do I access the button within a collection view to stop and start a timer on it? The sender is a UserItem but I want to change the button within the same collection view.
XAML Code (Collection ...
0
votes
0
answers
83
views
Jetpack Compose / Hilt MVI: ViewModel state not reset when closing and reopening dialog
I’m building a Jetpack Compose screen using an MVI pattern (ViewState / ViewEvent / ViewEffect) and Hilt for DI. I have a modal dialog with an input field for a name. The dialog’s composable looks ...
1
vote
1
answer
68
views
How to redirect with viewmodel
In a GET action method, based in some condition, I need to either return the view, or redirect to another action method that receives a view model via POST. This second action method view is strongly ...
0
votes
1
answer
49
views
Get ViewModel from onDraw Class
I create a class to draw random elements in a fragment, I need pass a list of elements already store in ROOM database with a Dao and Repository class.
ViewModel has a simple function to get all data
...
0
votes
0
answers
102
views
Trying to act on objects from a separate view SwiftUI SceneKit macOS 'cannot find ** in scope'
I have an app using SceneKit in a SwiftUI framework, and I am trying to properly organise my views by defining a separate view for the interface panel as an ZStack overlay.
I have moved the buttons (...
1
vote
1
answer
199
views
How to fix "Cannot create an instance of class ViewModel" when using Hilt?
I am doing a JetPack Compose course and I am trying to run an example project showcasing use of ViewModel together with Hilt.
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
override ...
0
votes
1
answer
64
views
Unresolved reference: ViewModelScope
I'm trying to run the example program at https://developer.android.com/kotlin/coroutines#examples-overview in Android Studio Iguana:
import androidx.lifecycle.ViewModel
import kotlinx.coroutines....
1
vote
2
answers
57
views
What's the difference between "viewModel: MyViewModel = viewModel()" and "viewModel: MyViewModel = MyViewModel()" in Android Composable
Here my composable
@Composable
fun CounterScreen(viewModel: MyViewModel = viewModel()) {
val uiState by viewModel.uiState.collectAsState()
Column(
modifier = Modifier.fillMaxSize(),
...
5
votes
1
answer
236
views
Why is initialValue needed in collectAsStateWithLifecycle for StateFlow?
I have a ViewModel that exposes a StateFlow representing the UI state of a book list:
class BooksViewModel(private val getBooksUseCase: GetBooksUseCase) : ViewModel() {
val booksState: Flow<...
3
votes
0
answers
283
views
Using ViewModels in React Native (instead of Redux)
State management in React Native is a well-debated topic, with Redux often being the default choice. However, for developers coming from native mobile development (Kotlin/Swift), Redux can feel ...
0
votes
0
answers
68
views
Mockito can only mock non-private & non-final classes, but the root cause of this error might be different
I want to write some UI tests for a Composable screen.
I constantly facing the same error again and again. The viewmodel cannot be mocked, even though I have declared the viewmodel class as open.
here ...
1
vote
1
answer
102
views
java.lang.IllegalStateException: Failed to find type for when decoding androidx.lifecycle.SavedStateHandle@8019b8e
Hello I am receiving the error in the title based off the following code:
class MainViewModel (application: Application, private val savedStateHandle: SavedStateHandle) : AndroidViewModel(application){...
1
vote
0
answers
67
views
I cannot display a popup from a view model
I want to display a community toolkit popup from a view model. The type of popup I want is a message box where I can pass a string variable that is the message I want displayed. I followed this Learn ...
0
votes
0
answers
100
views
How to add a UserControl specific ViewModel but still reach the Dependency Property changed callback in WPF
I would like to create a WPF class library based on a TreeView to visualize JSON strings.
I define a JsonProperty DependencyProperty which I wan't to use to pass the JSON string. I'm able to get the ...
0
votes
1
answer
61
views
TextField's onValueChange doesn't modify view model state
TextField always get empty when I try to write in it.
My ViewModel:
class MyVM: ViewModel() {
var name by mutableStateOf("")
fun onNameChange(newString: String) {
name = ...
0
votes
1
answer
107
views
Property Command from EventToCommandBehavior didn't work
I followed the steps from the accepted answer from this link.I did my "own implementation", but the command doesn't raise. Also, I did the same as the post said, but it didn't work.
Here is ...
1
vote
0
answers
152
views
Shared ViewModel with Type-Safe Navigation in a Multimodule Project
As the title suggests, I am trying to determine the best approach for implementing a shared ViewModel in a multimodule project.
Here is the approach I currently use:
In my app module, I have the ...
1
vote
1
answer
132
views
unresolved reference uiState = uiState.copy from a data class
I have a data class in my models library
data class ExperiencesUiState(
val experiencesItems: Result<List<Experiences>> = Result.success(emptyList()),
val userMessages: List<String> =...
1
vote
0
answers
71
views
Add lifecycle viemodel to build.gradle.kts does not resolve with AndroidStudio
I set up latest Android studio and all is up to date.
I want to add viewModel to the dependencies of the build gradle.
All is now in this Version.
dependencies {
implementation(libs.androidx.core....
0
votes
2
answers
139
views
WPF MVVM Not updating view
I am developing an application which needs a stopwatch. And I want to show the elapsed time using a textblock. My problem is, that the view is not updating, but I can use ICommands.
Here I assign the ...
-1
votes
1
answer
70
views
WPF control doesn't update when property is changed [closed]
I am developing a WPF app for my final university project (thesis). One of the goals is to be able to dynamically change the language of the UI. I suppose that it is not a very good idea to do it the ...
0
votes
1
answer
38
views
Data not updating in RecyclerView in a BottomSheetDialogFragment
I have a BottomSheetDialogFragment containing a RecyclerView in my Android app. The data for the RecyclerView is being received one by one from a socket connection. I need to add these items to the ...
0
votes
1
answer
43
views
How to get the notification text from the ViewModel?
My application is going to get a new string from the ViewModel every few hours and show it to the user as a notification text. The problem is that I don't know how to get the string from the ViewModel ...
0
votes
0
answers
73
views
How can I pass multiple parameters to a community toolkit maui popup in a view model?
I'm trying to display a popup y closmessage box that will automatically close. But what I would like to do is send that message box two parameters. The first is a string that is the actual message. ...
0
votes
0
answers
217
views
Create HiltViewModel with custom ViewModelStoreOwner
I have this compose code who works perfectly:
val pagerVM = hiltViewModel(
creationCallback = { factory: JourneySummaryPagerViewModel.Factory ->
factory.create(selectedIndex.value)
}...
0
votes
0
answers
51
views
Can I data bind controls in a community toolkit maui popup in a view model?
I'm trying to create a message box that would display varied messages and headers. So what I'm kind of looking for is a way to data bind the displayed label controls in the XAML file.
Here's what I ...
1
vote
2
answers
147
views
How do I handle a function that is calling a repository and also being used in two separate ViewModels
I have two ViewModels that contain the same function. The function calls an interface/repository to retrieve data from a database, and the function then manipulates the data.
It is my understanding ...
3
votes
1
answer
96
views
Hilt injection order inconsistency leading to NullPointerExceptions in Compose ViewModel
I'm currently working on an application using Compose and Hilt, and occasionally (less than 10 times out of 1000 according to my automated tests), the order of calls to my ViewModel differs, leading ...
0
votes
1
answer
27
views
How am i supposed to do Unit Test for a viewmodel that have a function to get it's token from api?
i've been working on this assignment of mine. to make a unit test on a viewmodel. but the viewmodel actually fetch token from api. is making a dummy for the token right? also i have a problem to make ...
0
votes
1
answer
62
views
WPF Multidimensional Complex Object Model Binding and Wrapping for MVVM
I am a bit new to MVVM, I have a complex model List<List<SomeClass>>, lets call it a board, I want to bind it to a canvas in WPF, the canvas should act as map of Rectangles each Rectangle ...
0
votes
1
answer
142
views
How to create different viewmodel instance for fragments in viewpager scoped to parent fragment with Hilt
I have a fragment HomeFragment which is part of the app navigation graph. Within FragmentHome I have a viewpager with 3 fragment instances of same class ChildFragment. Each ChildFragment requires an ...
0
votes
1
answer
52
views
How to create a koin module for interface
I have an abstract class:
abstract class MyFirstViewModel(
private val firstParamArgs: FirstParamArgs,
private val secondParam: SecondParam):ViewModel(),ActionHandler{
override fun handleAction(...
0
votes
0
answers
64
views
NavigationViewModel gets unexpectedly cleared when a screen is popped from the back stack
Let's assume I have two screens:
Screen A
Screen B.
When I send a NavigationUiEvent.ShowScreenB to the NavigationModel, the NavigationModel emits that event into a SharedFlow.
That flow is ...
1
vote
3
answers
313
views
viewModel on Kotlin Multiplatform?
How do I import androidx.lifecycle.viewmodel.compose.viewModel on Kotlin Multiplatform (iOS, web, desktop, Android)?
Attempts:
[versions]
androidx-activityCompose = "1.10.0-alpha02"
androidx-...
0
votes
1
answer
46
views
How do I get access to a content page's control from its view model that it was initially bound to?
What I'm looking to do is create and write all my methods in my view model file, and only place code in the code-behind file for functions that are automatically generated like ...
0
votes
1
answer
39
views
ViewModel's LiveData variable is empty value and not the desired value from Flow
I am trying to make a simple app that uses firestore, that just insert the token of devices and fetch the names corresponding to that token. When I tried to fetch "name" value , the flow in ...
1
vote
1
answer
266
views
How to observe changes in a List when the ViewModel is in Java and the UI is in Jetpack Compose (Kotlin)
I am migrating an application from View to Compose. The application was written in Java so I need to migrate the UI to Kotlin.
I prefer to do it as gradually as possible so I'm leaving the ViewModel ...
0
votes
1
answer
59
views
Storing current data
All the data comes from the server in bulk. I cannot influence its volume. Therefore, to reduce the number of requests, I store this data in a single ViewModel, which I share on all screens. I ...
0
votes
1
answer
111
views
How can I fix the issue of StateFlow values not updating?
How can I fix the issue of StateFlow values not updating?
The value of StateFlow is not assigned to the BoardRequestDto object.
The latest value of StateFlow is not properly collected in the ...
1
vote
2
answers
81
views
Submitting a POST using a ViewModel
I'm creating a website where you can Create an event for a game.I have a Create Page. The page has a GameID field when creating a new Event. I'm attempting to use a ViewModel so that I can display the ...