|
Dipping my toes into mobile development, and I'm going with a ToDo app (I know, babby's first mobile app, but I am throwing in some kink by allowing nested items). Starting with Android and Kotlin (which are both new to me), I'm having an issue that I can't seem to make work, and it should be easy. I'm using a RecyclerView to display the todo items, and I can't for the life of me make the items clickable. Here's the relevant section of MainActivity: Kotlin code:
Kotlin code:
I worked from a Java example that had this as an adapter: Java code:
Edit: I just wrote a new Java project with the equivalent of the Kotlin above, and it works as expected. LongSack fucked around with this message at 19:36 on Jul 19, 2022 |
# ¿ Jul 19, 2022 17:22 |
|
|
# ¿ May 10, 2024 14:41 |
|
Vesi posted:I switched to jetpack compose recently specifically to get away from recyclerview,, if you're just learning then maybe it's better to stick to the latest paradigms? Probably. I’ve only just started the UI, so not much effort lost. Still bugs me that that Kotlin code doesn’t work when the Java equivalent works just fine.
|
# ¿ Jul 19, 2022 23:02 |
|
FAT32 SHAMER posted:Try using If I understand you (and this is a huge if), I changed the ViewHolder to this: Kotlin code:
|
# ¿ Jul 20, 2022 13:23 |
|
brand engager posted:Had to dig up our old app that still uses recyclerviews, we only did view inflating in onCreateViewHolder and we setup any handlers in onBindViewHolder Except that the Java version works. It’s moot anyway since I took Vesi’s advice and switched to Compose. I like it, it reminds me of Blazor components and React functional components. The only thing I’m a little iffy on is that my MainActivity file is now at the size where if I were programming in C# I’d be looking to refactor some stuff out into separate classes. But since these are functions, I can’t really do that.
|
# ¿ Jul 20, 2022 21:25 |
|
smiling giraffe posted:Yeah you can, it’s kotlin, functions don’t need to be a class Interesting. I’m new to the language, so this is good info. Thanks.
|
# ¿ Jul 20, 2022 23:14 |
|
Question about state ... I'm storing the name of the most recent ToDoItemList in shared preferences and saving/retrieving the name in onPause and onResume: Kotlin code:
Kotlin code:
|
# ¿ Jul 28, 2022 16:00 |
|
smiling giraffe posted:You want to hold state in a viewmodel. This is an Android architecture component that is designed to persist as activities move through their lifecycle. I was already using view models, but for some reason had the current list name stored in a separate variable. I moved it into the view model, and adjusted my onXxx methods and everything works still. Thanks!
|
# ¿ Jul 28, 2022 18:23 |
|
So, while new to Android and Kotlin, I'm trying to do things the "correct" way with dependency injection. I started using Koin but it was a real pain trying to figure out what combinations of import I needed to use where to get inject(). Switched to Hilt and while some things are easier, it's been a huge pain in its own right (I've lost track of the number of adjustments I've had to make to my gradle scripts). But I think I have it finally working, except for one thing. I want to inject my MainViewModel into my MainActivity. However, MainViewModel has its own dependencies: Kotlin code:
Kotlin code:
Fine, I can do that. Let's try this: Kotlin code:
Well, yeah, that's kinda the point. What am I doing wrong? If I have to instantiate the MainViewModel in my MainActivity, I'll also have to instantiate all of its dependencies and all of their dependencies, at which point it stops making sense to use DI.
|
# ¿ Jul 31, 2022 19:33 |
|
Volmarias posted:You still use "by viewmodels()" when using hilt. Thank you, that did the trick.
|
# ¿ Aug 1, 2022 19:28 |
|
OK Next problem. When using state, how does compose determine that the state has in fact changed and that a recomposition is necessary? Does it use the reference (i.e., address), a hash code function, or some other method? I've got my ToDo app to where it displays some mocked-up items, I can scroll them using LazyColumn, and there's a button to the left of the item text which is used to either complete on incomplete item, or to delete a completed / canceled one. I'm working on the first part - completing an item. The code works just fine, I can step through in the debugger and see that the state of the item is being updated in my repository (currently memory-based) and in the state in my view model, however the item on the screen never gets updated. Weirdly, however, if I scroll the item off the page then back on, it does get displayed as expected (with the title in strikethrough). So something is happening that is causing the item not to be recomposed when the completed state changes, but the state is in fact changed as can be seen when I scroll it off and back on, which forces a recompose. The structure is like this: MainActivity -> RootPage -> ItemList -> (LazyColumn of) ItemCard Some relevant code ItemState Kotlin code:
Kotlin code:
Kotlin code:
Kotlin code:
Kotlin code:
Kotlin code:
|
# ¿ Aug 3, 2022 20:18 |
|
smiling giraffe posted:I think you want your viewmodel to expose your UI state in a StateFlow, which is collected in your activity when its in the appropriate lifecycle state. See: https://developer.android.com/kotlin/flow/stateflow-and-sharedflow#stateflow Thanks, I’ll check that out tomorrow quote:Also not sure why you are wrapping ToDoItem in a MutableState Originally it wasn’t, it was just something i tried to see if that would correct the problem. EDIT: I feel like when I first started WPF and didn’t understand data binding. My first version of my character portfolio app literally copied data from the DTO objects to the screen and back. Then one day it just “clicked”. I hope that this happens here too. LongSack fucked around with this message at 23:18 on Aug 3, 2022 |
# ¿ Aug 3, 2022 23:15 |
|
smiling giraffe posted:I think you want your viewmodel to expose your UI state in a StateFlow, which is collected in your activity when its in the appropriate lifecycle state. See: https://developer.android.com/kotlin/flow/stateflow-and-sharedflow#stateflow OK based on that page, I changed the state to a (Mutable)StateFlow. Making that change alone didn't change anything. Then I noticed the part about the change in the onCreate method, so I added that: Kotlin code:
|
# ¿ Aug 4, 2022 15:01 |
|
brand engager posted:this spot where you're making a state object further down in the composable is gonna create a new state every time the library checks if it needs to recompose, that's gonna cause some weird issues The state object is created in the viewmodel which is injected into the composable, and should be scoped to the lifetime of the composable quote:what is this function? That's where the LazyColumn gets its items from The book I worked through prior to starting this project(Kickstart Modern Android Development with Jetpack and Kotlin, here) develops an app using viewmodels and state and it simply uses a MutableState<T> object (with a public State<T> getter) and it works just by using copy() on it as the state changes, and it seems to work just fine.
|
# ¿ Aug 4, 2022 16:50 |
|
brand engager posted:You're also making one in the spot I quoted though which is a problem for reasons already mentioned Oh, that part is gone. I had put it in to see if it would help, but it didn't make any difference, so I removed it. It just passes the item now Edit: To expand on that a bit (as to why I tried that), when doing data binding in WPF everything that the UI binds to is observable. So if I have a list of Foo, not only is the collection observable (using ObservableCollection or ObservableDictionary), but the Foo model is itself completely observable, something like: C# code:
LongSack fucked around with this message at 17:55 on Aug 4, 2022 |
# ¿ Aug 4, 2022 17:48 |
|
smiling giraffe posted:ok try this: No change: Kotlin code:
|
# ¿ Aug 4, 2022 18:41 |
|
smiling giraffe posted:whats your viewmodel look like now with the StateFlow MainViewModel: Kotlin code:
|
# ¿ Aug 5, 2022 13:57 |
|
smiling giraffe posted:Not sure, looks fine to me. Put it on github if you want I’ll have a look https://github.com/vjkrammes/ToDoPlus Thanks!
|
# ¿ Aug 5, 2022 16:14 |
|
smiling giraffe posted:The issue is that you're doing IO operations in your repository on the main thread. Welp, I think I made the changes, but it's still not doing anything different. The real I/O is in the serializer classes (they are internal to the repository classes). I changed all the methods that actually do the I/O to suspend functions, (and of course made the necessary adjustments up the line). Then in the view models where I need to call the repository methods, I did as above. Nothing is different. It still doesn't update the screen until I scroll an item off an on again. I also started working on the assignees section, using similar code, and it updates just fine (you can only add assignees at the moment, but they do show up and are persisted to the file system). I updated the git repo to show the code in its current state. Also, as an aside, in the suspend functions in the serializers, the I/O calls (openXXX, read, write, readline, close, etc.) are all flagged as "inappropriate blocking method call" so that seems a bit off.
|
# ¿ Aug 5, 2022 20:43 |
|
smiling giraffe posted:override fun getItems(): List<ToDoItem> = items.toList() That seems to have done the trick! Thank you for all your input. quote:Why is StateFlow still in the viewmodel? You don't seem to be using the actual flow parts of it. You're also still recreating state objects on every attempted recompose https://github.com/vjkrammes/ToDoPl...Activity.kt#L31, that's gonna cause problems like mentioned before. Those changes were made by suggestion. Now that toList() is in place, I was able to change the state back to MutableState and can pass the state objects directly from the view models to the composables: Kotlin code:
|
# ¿ Aug 6, 2022 13:40 |
|
Next batch of questions - 1. I'm using Android Studio (Chipmunk, 2021.2.1 Patch 1), and several times a day it will just stop responding to the keyboard and the mouse wheel. The mouse itself still works, so I can hit the menu and save all before I quit and restart. Not the worst thing in the world, but it does get annoying because it kills the emulator and the restart time is irritating. 2. I was loading up some of my view models in the init block, but since switching my I/O methods to suspense, I can't do that any more. I'm using a factory method and @Provides (similar to how I would handle it in C#), but I'm wondering if there's a better way: Kotlin code:
I tried changing the code from a simple navController.navigate("edit/${item.id}") to this: Kotlin code:
Ideas? I have updated the git repo
|
# ¿ Aug 9, 2022 20:10 |
|
OK, I figured out the navigation thing. My navigation menu is shown below. The saveState was set to true. This was (not surprisingly) causing the state to be retained between page visits. Setting it to false seems to have corrected the problem.Kotlin code:
|
# ¿ Aug 12, 2022 20:56 |
|
OK, I've made a ton of progress on my todo app. I showed it to a friend a week or so ago, and she said she'd love something where she could organize items by category. So for a shopping list, you could group meat, produce, etc. and have all the things you need in one area of the store all together. So I've rewritten the code to work as a collection of categorized lists, switched to SQLite (using room) instead of JSON, and now have a basic working version of the app. I have only one thing that's driving me crazy. I'm using Toast (I will be switching to Snackbar) popups for status reporting ("Item created successfully", "Changes saved successfully", etc.) and it's working on every single page except one. I've looked at the working / non-working code side by side in Android Studio, and I can't see any differences. I've checked that the same imports are being used. I've also run it on my Galaxy S22 in case it's an emulator issue, but the behavior is the same. I've set some breakpoints, and as best as I can tell, the message(s) are being emitted in the viewmodel, but are not being collected in the page. It doesn't matter whether I use Toast or a Snackbar, the message does not show up. If i put some code inline in the composable as a test, the message shows just fine. I've put the code into a new repository here, but here are the parts in question: ViewModel: Kotlin code:
Kotlin code:
Kotlin code:
|
# ¿ Aug 22, 2022 20:09 |
|
smiling giraffe posted:Have you gone through it step by step in the debugger? That usually works for me I have. It’s complicated by the use of coroutines, but setting breakpoints in the viewmodel’s sendToastMessage function shows everything as expected. But setting a breakpoint inside the collect never gets tripped. That’s why i think the root of the problem is that messages are being emitted but not collected, for whatever reason
|
# ¿ Aug 22, 2022 22:04 |
|
I fixed the toast problem by rewriting the view model. For some reason, it works using the new view model, even though as best as I can tell the code is identical. Code on the left works, code on the right doesn't:
|
# ¿ Aug 24, 2022 15:42 |
|
|
# ¿ May 10, 2024 14:41 |
|
Next piece of weirdness. If I have a TextField and set the value to an item in the viewmodel state, then the soft keyboard disappears after every keystroke. The field doesn't lose focus, but the keyboard hides. For example: Kotlin code:
Kotlin code:
Ideas?
|
# ¿ Sep 1, 2022 15:04 |