Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
FAT32 SHAMER
Aug 16, 2012



Volmarias posted:

The change from MVC to MVI is a pretty rough one to try to shoehorn in, and I get the feeling that any generated results would be hideous at best.

Wait MVI? What the hell happened to MVVM??

Adbot
ADBOT LOVES YOU

brand engager
Mar 23, 2011

Can they quit hopping architectures for like 5 minutes

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...

FAT32 SHAMER posted:

Wait MVI? What the hell happened to MVVM??

I can't find this now, maybe I've Mandela Effected myself but I could have sworn that the compose docs suggested using MVI instead of just MVVM. gently caress!

brand engager
Mar 23, 2011

Does compose not have something to wrap an existing activity or fragment into a compose hierarchy? AndroidView only works for views, which isn't that useful since the activity/fragment and its viewmodel do the useful work on views

FAT32 SHAMER
Aug 16, 2012



brand engager posted:

Does compose not have something to wrap an existing activity or fragment into a compose hierarchy? AndroidView only works for views, which isn't that useful since the activity/fragment and its viewmodel do the useful work on views

Your option is to “inflate” a composable in a fragment (I.e. use the fragment as the composable host which is then hosted by your activity) and nuke any viewbinding and xml associated with it, or go whole hog and use your activity to host composables and map their flow using the new jetpack composable navigation and nuke all of your fragments and refactor large swathes of your view models

FAT32 SHAMER
Aug 16, 2012



Oh also after using it ive decided using uris for deep linking is fuckin nice compared to the PendingIntentBuilder

brand engager
Mar 23, 2011

FAT32 SHAMER posted:

Your option is to “inflate” a composable in a fragment (I.e. use the fragment as the composable host which is then hosted by your activity) and nuke any viewbinding and xml associated with it, or go whole hog and use your activity to host composables and map their flow using the new jetpack composable navigation and nuke all of your fragments and refactor large swathes of your view models

That's the opposite of what I meant but it's not possible anyways. They just don't have an equivalent of UIViewControllerRepresentable

LongSack
Jan 17, 2003

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:
        recyclerView = findViewById(R.id.recyclerView)
        adapter = ToDoAdapter(manager.getTopLevelItems(), this)
        val layoutManager: RecyclerView.LayoutManager = LinearLayoutManager(applicationContext)
        recyclerView.layoutManager = layoutManager
        recyclerView.itemAnimator = DefaultItemAnimator()
        recyclerView.addItemDecoration(DividerItemDecoration(this, LinearLayoutManager.VERTICAL))
        recyclerView.adapter = this.adapter
Here's the adapter:
Kotlin code:
class ToDoAdapter(
    private val dataSet: Array<ToDoItem>,
    private val cellClickListener: ICellClickListener
) :
    RecyclerView.Adapter<ToDoAdapter.ViewHolder>() {
    private val format = SimpleDateFormat("yyyy/MM/dd")

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view), View.OnClickListener {
        val txtPlus: TextView = view.findViewById(R.id.txtPlus)
        val txtName: TextView = view.findViewById(R.id.txtName)
        val txtDueDate: TextView = view.findViewById(R.id.txtDueDate)

        init {
            view.isClickable = true
            view.isFocusable = true
            view.setOnClickListener(this)
        }

        override fun onClick(p0: View?) {
            val i = adapterPosition
            val item = dataSet[i]
            Log.i("info", "Item ${item.name} clicked")
            cellClickListener.onCellClickListener(item)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.todo_item, parent, false)
        return ViewHolder(view)
    }
	...
}
I set breakpoints and see that the init is being run, so (in theory) the clickable, focusable, and onClick properties should be set. Yet the onClick method never gets called.

I worked from a Java example that had this as an adapter:
Java code:
public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.ListItemHolder> {
    private List<Note> mNoteList;
    private MainActivity mMainActivity;

    public NoteAdapter(MainActivity mainActivity, List<Note> noteList){
        mMainActivity = mainActivity;
        mNoteList = noteList;
    }

    @NonNull
    @Override
    public NoteAdapter.ListItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater
                .from(parent.getContext())
                .inflate(R.layout.listitem, parent, false);
        return new ListItemHolder(itemView);
    }

    public class ListItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView mTitle;
        TextView mDescription;
        TextView mStatus;

        public ListItemHolder(View view) {
            super(view);
            mTitle = view.findViewById(R.id.textViewTitle);
            mDescription = view.findViewById(R.id.textViewDescription);
            mStatus = view.findViewById(R.id.textViewStatus);
            view.setClickable(true);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            mMainActivity.showNote(getAdapterPosition());
        }
    }
}
and this works just fine. What am I doing wrong?

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

Vesi
Jan 12, 2005

pikachu looking at?
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?

LongSack
Jan 17, 2003

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.

FAT32 SHAMER
Aug 16, 2012



Try using

val onClick: (dataToSendToHostFragment: Any) -> Unit

Instead of the callback method, then call onClick.invoke(data) from the root of the viewholder

Note: Any is whatever the object you’re returning from the adapter to the fragment is

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...

LongSack posted:

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.

Honestly, it SHOULD work the same, though there's probably some subtle bug that we all missed or which wasn't included here. You should be able to see the Java code that the Kotlin code transpiles to, which will at least make it easier to compare and see what if anything is different.

LongSack
Jan 17, 2003

FAT32 SHAMER posted:

Try using

val onClick: (dataToSendToHostFragment: Any) -> Unit

Instead of the callback method, then call onClick.invoke(data) from the root of the viewholder

Note: Any is whatever the object you’re returning from the adapter to the fragment is

If I understand you (and this is a huge if), I changed the ViewHolder to this:
Kotlin code:
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view)/*, View.OnClickListener*/ {
    val txtPlus: TextView = view.findViewById(R.id.txtPlus)
    val txtName: TextView = view.findViewById(R.id.txtName)
    val txtDueDate: TextView = view.findViewById(R.id.txtDueDate)
    init {
        view.isClickable = true
        view.setOnClickListener {
            onClick.invoke(dataSet[adapterPosition])
        }
    }

    val onClick: (data: ToDoItem) -> Unit = {
        Log.i("info", "Item ${it.name} clicked");
    }
}
and it still doesn't work

brand engager
Mar 23, 2011

Aren't you supposed to do all this setup in the onBind or whatever it's called? The recyclerView is probably messing with those viewHolders every time it recycles one so I wouldn't count on handlers set in a constructor

brand engager
Mar 23, 2011

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

LongSack
Jan 17, 2003

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.

smiling giraffe
Nov 12, 2015
Yeah you can, it’s kotlin, functions don’t need to be a class

brand engager
Mar 23, 2011

LongSack posted:

Except that the Java version works.

It probably shouldn't have

LongSack
Jan 17, 2003

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.

smiling giraffe
Nov 12, 2015
“be in a class”* even, but yeah, kotlin is great and apart from a few minor things, compose is great, and infinitely better than recyclerviews

LongSack
Jan 17, 2003

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:
override fun onResume() {
    super.onResume()
    val prefs = getSharedPreferences(SHARED_PREFERENCES_NAME, MODE_PRIVATE)
    currentListName.value = prefs.getString(CURRENT_LIST_NAME, DEFAULT_LIST_NAME).toString()
    val file = toDoFileRepository.read(currentListName.value!!)
    toDoRepository.loadFile(file)
}

override fun onPause() {
    super.onPause()
    val prefs = getSharedPreferences(SHARED_PREFERENCES_NAME, MODE_PRIVATE)
    val editor = prefs.edit()
    editor.putString(CURRENT_LIST_NAME, currentListName.value)
    editor.commit()
}
Because these functions are not in a Composable, I can't use remember so I'm using MutableLiveState<String> and passing that value down to my root Composable:
Kotlin code:
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
        ToDoPlusTheme {
            val navController = rememberNavController()
            NavHost(navController = navController, startDestination = "root") {
                composable("root") {
                    Root(
                        currentListName,
                        doChoose,
                        navController
                    )
                }
            }
        }
    }
}
@Composable
fun Root(
    currentListName: MutableLiveData<String>,
    chooseNewList: (String) -> Unit,
    navController: NavController
) {
  ...
}
Is there a better way to handle this?

smiling giraffe
Nov 12, 2015
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.

View models typically expose their state using some implementation of the observable pattern, historically this has been livedata, but more recently StateFlow. This allows activities/fragments to observe and react to state when they are ready to do so.

There should be plenty of good guides out there for getting setup with a viewmodel

LongSack
Jan 17, 2003

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.

View models typically expose their state using some implementation of the observable pattern, historically this has been livedata, but more recently StateFlow. This allows activities/fragments to observe and react to state when they are ready to do so.

There should be plenty of good guides out there for getting setup with a viewmodel

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!

LongSack
Jan 17, 2003

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:
@HiltViewModel
class MainViewModel @Inject constructor(private val toDoRepository: ToDoRepository) : ViewModel() {

    private val allItems = ArrayList<ToDoItem>()

    // public properties
    var currentFile: MutableState<ToDoFile?> = mutableStateOf(null)
    var topItem: MutableState<ToDoItem?> = mutableStateOf(null)
    var items: SnapshotStateList<ToDoItem> = mutableStateListOf()

    fun isValid() = currentFile.value != null

		...
}
I tried
Kotlin code:
@AndroidEntryPoint
class MainActivity : ComponentActivity() {

    @Inject
    lateinit var mainViewModel: MainViewModel

       ...
}
but get this error message: "Injection of an @HiltViewModel class is prohibited since it does not create a ViewModel instance correctly. Access the ViewModel via the Android APIs (e.g. ViewModelProvider) instead."

Fine, I can do that. Let's try this:
Kotlin code:
@AndroidEntryPoint
class MainActivity : ComponentActivity() {

    lateinit var mainViewModel: MainViewModel

		...

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mainViewModel = ViewModelProvider
            .AndroidViewModelFactory
            .getInstance(application)
            .create(MainViewModel::class.java)
    }

	...
But now I get an exception that at its root has this message: "Caused by: java.lang.InstantiationException: java.lang.Class<solutions.vjk.todoplus.viewmodels.MainViewModel> has no zero argument constructor"

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.

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...
You still use "by viewmodels()" when using hilt.

LongSack
Jan 17, 2003

Volmarias posted:

You still use "by viewmodels()" when using hilt.

Thank you, that did the trick.

LongSack
Jan 17, 2003

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:
data class ItemState(
    val currentFile: ToDoFile?,
    val items: List<ToDoItem>,
    val isLoading: Boolean,
    val errorMessage: String? = null
)
MainViewModel
Kotlin code:
@HiltViewModel
class MainViewModel @Inject constructor(private val toDoRepository: IToDoRepository) : ViewModel() {

    private val _state = mutableStateOf(
        ItemState(
            null,
            emptyList(),
            true,
            null
        )
    )
    val state: State<ItemState>
        get() = _state
...
    val completeItem: (item: ToDoItem) -> Unit = { item ->
        if (!item.complete) {
            var error: String? = null
            val result = toDoRepository.complete(item)
            if (!result.isSuccessResult) {
                error = result.getMessage()
            }
            val newItems = ArrayList<ToDoItem>(toDoRepository.getItems()) // update state with a completely new list
            _state.value = state.value.copy(items = newItems, errorMessage = error) // which SHOULD cause recomposition
        }
    }
MainActivity
Kotlin code:
@AndroidEntryPoint
class MainActivity : ComponentActivity() {

    private val mainViewModel: MainViewModel by viewModels()

    @Inject
    lateinit var toDoFileRepository: IToDoFileRepository

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ToDoPlusTheme {
                val navController = rememberNavController()
                NavHost(navController = navController, startDestination = "root") {
                    composable("root") {
                        RootPage(
                            state = mainViewModel.state.value,
                            repository = toDoFileRepository,
                            itemCount = mainViewModel.itemCount(),
                            doChoose = mainViewModel.chooseNewList,
                            doNew = mainViewModel.makeNewList,
                            doSearch = mainViewModel.searchItems,
                            doDelete = mainViewModel.deleteItem,
                            doComplete = mainViewModel.completeItem,
                            doEdit = { item -> navController.navigate("edit/${item.id}") }
                        )
                    }
                }
            }
        }
    }
RootPage
Kotlin code:
@Composable
fun RootPage(
    state: ItemState,
    repository: IToDoFileRepository,
    itemCount: Int,
    doChoose: (String) -> Unit,
    doNew: () -> Unit,
    doSearch: () -> Unit,
    doDelete: (item: ToDoItem) -> Unit,
    doComplete: (item: ToDoItem) -> Unit,
    doEdit: (item: ToDoItem) -> Unit
) {
...
        ItemList(
            state = state,
            icons = arrayOf(
                R.drawable.ic_baseline_delete_24,
                R.drawable.ic_baseline_check_24
            ),
            doDelete,
            doComplete,
            doEdit
        )
...
ItemList
Kotlin code:
    if (state.items.isNotEmpty()) {
        LazyColumn(modifier = Modifier.fillMaxSize()) {
            items(state.items) { toDoItem ->
                ItemCard(
                    item = mutableStateOf(toDoItem),
                    buttonIconId = if (toDoItem.complete || toDoItem.canceled) icons[0] else icons[1],
                    onButtonClick = if (toDoItem.complete || toDoItem.canceled) doDelete else doComplete,
                    onItemClick = doEdit
                )
                Divider(color = MaterialTheme.colors.onSurface)
            }
        }
    }
and finally, ItemCard
Kotlin code:
@Composable
fun ItemCard(
    item: MutableState<ToDoItem>,
    buttonIconId: Int,
    onButtonClick: (item: ToDoItem) -> Unit,
    onItemClick: (item: ToDoItem) -> Unit,
    rowHeight: Dp = 40.dp
) {
    Row {
        ItemButton(
            iconid = buttonIconId,
            onClick = { onButtonClick(item.value) }
        )
        Spacer(modifier = Modifier.width(8.dp))
        Row(
            modifier = Modifier
                .clickable { onItemClick(item.value) }
                .fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(
                text = item.value.name,
                fontSize = 24.sp,
                fontWeight = if (item.value.dueDate.get(Calendar.YEAR) > 2020) FontWeight.Bold else FontWeight.Normal,
                style =
                if (item.value.complete || item.value.canceled) {
                    TextStyle(textDecoration = TextDecoration.LineThrough)
                } else if (item.value.inProgress) {
                    TextStyle(fontStyle = FontStyle.Italic)
                } else {
                    TextStyle(textDecoration = TextDecoration.None)
                },
                overflow = TextOverflow.Ellipsis
            )
            Spacer(modifier = Modifier.width(4.dp))
            Column(
                horizontalAlignment = Alignment.End,
                modifier = Modifier.weight(1f)
            ) {
                Row {
                    Annotations(item = item.value)
                }
                Text(
                    text = displayDate(item.value.dueDate, SimpleDateFormat("yyyy/MM/dd")),
                    fontSize = 16.sp,
                    fontStyle = FontStyle.Italic,
                    textAlign = TextAlign.End,
                )
            }
        }
    }
}

smiling giraffe
Nov 12, 2015
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

I think the State class you are using in the viewmodel only causes recomposition within the context of a composable function.

More generally you shouldn't need to inject your repo into the activity and pass it to the composables.

Giving the Activity the Repo you're letting it know to much, the Activity should just be like: "here is some ui state from the viewmodel that I will observe. when a button is clicked i will tell the viewmodel it happened". Then it's the viewmodels job to decide how that button click should result in a change of state.

Also not sure why you are wrapping ToDoItem in a MutableState

LongSack
Jan 17, 2003

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

I think the State class you are using in the viewmodel only causes recomposition within the context of a composable function.

More generally you shouldn't need to inject your repo into the activity and pass it to the composables.

Giving the Activity the Repo you're letting it know to much, the Activity should just be like: "here is some ui state from the viewmodel that I will observe. when a button is clicked i will tell the viewmodel it happened". Then it's the viewmodels job to decide how that button click should result in a change of state.

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

FAT32 SHAMER
Aug 16, 2012



Here’s a comment I saw a while ago that really resonated with me re: keeping state in the viewmodel/MVI in general:

https://www.reddit.com/r/mAndroidDe...ossmf&context=3

I’m on mobile otherwise I’d try to embed the quote with the codeblocks

LongSack
Jan 17, 2003

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:
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                mainViewModel.state.collect { state ->
			// WHAT GOES HERE?
                }
            }
        }
...
I can't call a composable function there. I tried moving the entire setContent block into that place, and again -- while it works, the items are still not being updated on the screen until scrolled off and back on again. What am I missing?

brand engager
Mar 23, 2011

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
Kotlin code:
item = mutableStateOf(toDoItem),
what is this function?
Kotlin code:
items(state.items) { toDoItem ->

brand engager
Mar 23, 2011

Also that StateFlow class isn't a Compose state, it just happens to have use state in the name. I don't think it'll cause recomposing like the actual androidx.compose.runtime.State type will

LongSack
Jan 17, 2003

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
Kotlin code:
item = mutableStateOf(toDoItem),

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?
Kotlin code:
items(state.items) { toDoItem ->

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.

smiling giraffe
Nov 12, 2015

LongSack posted:

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:
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                mainViewModel.state.collect { state ->
			// WHAT GOES HERE?
                }
            }
        }
...
I can't call a composable function there. I tried moving the entire setContent block into that place, and again -- while it works, the items are still not being updated on the screen until scrolled off and back on again. What am I missing?

try this:

nm :)

smiling giraffe fucked around with this message at 17:32 on Aug 4, 2022

brand engager
Mar 23, 2011

LongSack posted:

The state object is created in the viewmodel which is injected into the composable, and should be scoped to the lifetime of the composable

You're also making one in the spot I quoted though which is a problem for reasons already mentioned

LongSack posted:

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.

Oh ok thought you had created your own forEach()

LongSack
Jan 17, 2003

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:
public class Foo : NotifyBase {
  private int _id;
  public int Id {
    get -> _id;
    set -> SetProperty(ref _id, value);
 }

  private string _name;
  public string Name {
    get -> _name;
    set -> SetProperty(ref _name, value);
  }
So i wanted to try something like that to see if it made any difference. It didn't, so I pulled it out, but I must have posted that question while it was still in the code base

LongSack fucked around with this message at 17:55 on Aug 4, 2022

smiling giraffe
Nov 12, 2015
ok try this:
Kotlin code:
    override fun onCreate(savedInstanceState: Bundle?) {
    setContent {
        YourAppTheme {
            val state by mainViewModel.state.collectAsState()
            YourComposable(state)
        }
    }
}
...
I posted this before then got cold feet as I realised it wasn't collecting the flow in a lifecycle aware way. However for the use-case of ui state i think its fine. This is good further reading I think https://manuelvivo.dev/coroutines-addrepeatingjob

FAT32 SHAMER
Aug 16, 2012



smiling giraffe posted:

ok try this:
Kotlin code:
    override fun onCreate(savedInstanceState: Bundle?) {
    setContent {
        YourAppTheme {
            val state by mainViewModel.state.collectAsState()
            YourComposable(state)
        }
    }
}
...
I posted this before then got cold feet as I realised it wasn't collecting the flow in a lifecycle aware way. However for the use-case of ui state i think its fine. This is good further reading I think https://manuelvivo.dev/coroutines-addrepeatingjob

Wouldn’t it be lifecycle aware by way of the viewmodel? All it needs to know is if it’s active or not, and it’ll flow as soon as everything comes back after an onResume or w/e

Adbot
ADBOT LOVES YOU

brand engager
Mar 23, 2011

Usually I do code reviews in android studio, and piecing the whole thing together from different posts is kinda a pain. Think you're gonna have to figure out the issue yourself lol

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply