https://kotlinlang.org logo
#compose
Title
# compose
h

henrikhorbovyi

06/06/2020, 6:52 AM
Hello, I have the following code in my project (I saw it in JetNews repo)
Copy code
@Model
object AppNavigator {
    var currentScreen: Screen = Screen.Home()
}

/**
 * Temporary solution pending navigation support.
 */
fun navigateTo(destination: Screen) {
    AppNavigator.currentScreen = destination
}
But now I'm using dev12, and we know that
@Model
is deprecated. How do you guys would fix it? I've tryied:
Copy code
object AppNavigator {
    var currentScreen: MutableState<Screen> = state { Screen.Home() }
}
But it does not work, states must be called inside composables
k

Kazemihabib1996

06/06/2020, 7:10 AM
var currentScreen by mutableStateOf(Screen.Home())
🙌 1
☝️ 1
There are some problems with auto Import in the recent versions of AndroidStudio If the Idea throws error: Type 'MutableState<TypeVariable(T)>' has no method 'getValue(ThemeState, KProperty<*>)' and thus it cannot serve as a delegate Import getValue and SetValue manually.
Copy code
import androidx.compose.getValue
import androidx.compose.setValue
v

Val Salamakha

06/06/2020, 8:57 AM
@Kazemihabib1996 it does not work.
🤔 1
k

Kazemihabib1996

06/06/2020, 9:05 AM
@Val Salamakha throws error?
v

Val Salamakha

06/06/2020, 9:05 AM
yes
k

Kazemihabib1996

06/06/2020, 9:06 AM
what's the error?
v

Val Salamakha

06/06/2020, 9:07 AM
Copy code
import androidx.compose.getValue
import androidx.compose.setValue
The abovementioned is not active and they are shown as unused .
k

Kazemihabib1996

06/06/2020, 9:12 AM
I had experiences like that but it builds with out problem, it's the kotlin plugins bug I think
v

Val Salamakha

06/06/2020, 9:22 AM
Finally it works as Screen.Home
Copy code
var currentScreen by mutableStateOf( Screen.Home )
Anyway thanks to @Kazemihabib1996
👍 1
@henrikhorbovyi The last version of Jetnews is working when you change code in the file Status.kt to the following :
Copy code
import androidx.compose.getValue
import androidx.compose.setValueobject                                              ...                                                 object JetnewsStatus {
    var currentScreen by mutableStateOf( Screen.Home as Screen )
    val favorites by mutableStateOf(ModelList<String>())
    val selectedTopics by mutableStateOf(ModelList<String>())

}
other code without changes.
👍 1
✔️ 1
a

Adam Powell

06/06/2020, 1:44 PM
You don't need to use mutableStateOf around ModelLists
👍 1
n

nickbutcher

06/06/2020, 1:49 PM
Jetnews is updated to
dev12
on the
develop
branch. We still need to merge this back https://github.com/android/compose-samples/blob/develop/JetNews/app/src/main/java/com/example/jetnews/ui/Status.kt#L33
❤️ 1
👍 1
h

henrikhorbovyi

06/06/2020, 6:05 PM
@Kazemihabib1996 it worked \o/ Thanks
👍 1
I've tried this before but, I saw an error, but now it worked when I passed a
<T>
Copy code
... mutableStateOf<Screen>(...)
I think was exactly the "import error" that you mentioned @Kazemihabib1996
And @nickbutcher, I didn't see this pull request and I created the issue for
@Model
😄
Thanks to share it
n

nickbutcher

06/07/2020, 2:15 PM
No worries, thanks for filling it. We should maybe make the
develop
branch more prominent as it's the most useful.
👍 1