aballano
11/01/2019, 4:12 PMLuca Nicoletti
11/01/2019, 4:13 PMGiorgos Neokleous
11/01/2019, 4:14 PMLuca Nicoletti
11/01/2019, 4:14 PMaballano
11/01/2019, 4:16 PMLuca Nicoletti
11/01/2019, 4:16 PMaballano
11/01/2019, 4:18 PMLeland Richardson [G]
11/01/2019, 4:23 PMthemishkun
11/01/2019, 4:24 PMLeland Richardson [G]
11/01/2019, 4:24 PMLuca Nicoletti
11/01/2019, 4:25 PMLeland Richardson [G]
11/01/2019, 4:25 PMvar x = +memo { MyModel(...) }
+state
isaballano
11/01/2019, 4:27 PMLeland Richardson [G]
11/01/2019, 4:28 PMval x = +state { 0 }
is basically shorthand for val x = +memo { State(0) }
where State
is a value holder class annotated with @ModelMichal Bacik
11/01/2019, 4:47 PMLeland Richardson [G]
11/01/2019, 4:49 PMLuca Nicoletti
11/01/2019, 4:51 PMaballano
11/01/2019, 4:51 PMmemo
is what I was missing @Leland Richardson [G] Now it's working with lazy val + setting mutable variables inside the model
val mainModel by lazy { +model { MainModel() } }
Leland Richardson [G]
11/01/2019, 4:54 PMLuca Nicoletti
11/01/2019, 4:54 PMLeland Richardson [G]
11/01/2019, 4:54 PMunaryPlus
operator is global, and will not work once we take it away (which will be very soon)+
as something you can only use inside of a composable functionLuca Nicoletti
11/01/2019, 4:55 PMhere
in the channel when the +
will be removed?Leland Richardson [G]
11/01/2019, 4:55 PMLuca Nicoletti
11/01/2019, 4:56 PMLeland Richardson [G]
11/01/2019, 4:56 PMLuca Nicoletti
11/01/2019, 4:56 PMaballano
11/01/2019, 4:56 PMLeland Richardson [G]
11/01/2019, 5:09 PMdata class Message(
val sender: String,
val message: String
)
@Model
data class MainModel(
var messages: List<Message> = emptyList()
)
class MainActivity : AppCompatActivity() {
private val algebra = MainAlgebra()
private val mainModel = MainModel()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
mainContent(mainModel, ::onButtonClicked)
}
}
private fun onButtonClicked() {
algebra.getMessages().continueOn(UIContext).unsafeRunAsync { cb ->
cb.fold(
ifRight = {
mainModel.messages = mainModel.messages + it
},
ifLeft = { throw it }
)
}
}
}
Luca Nicoletti
11/01/2019, 5:10 PMList
now working? 🙂 Isn’t ModelList
the preferred one?Leland Richardson [G]
11/01/2019, 5:11 PMdata class Message(
val sender: String,
val message: String
)
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
mainContent()
}
}
}
@Composable fun mainContent() {
val algebra = +memo { MainAlgebra() }
val messages by +state<List<Message>> { emptyList() }
fun onButtonClicked() {
algebra.getMessages().continueOn(UIContext).unsafeRunAsync { cb ->
cb.fold(
ifRight = {
messages = messages + it
},
ifLeft = { throw it }
)
}
}
}
// whatever you were doing before...
}
aballano
11/01/2019, 5:13 PMLeland Richardson [G]
11/01/2019, 5:13 PMmessages = messages + it
works just fineList
aballano
11/01/2019, 6:28 PMvar
, right? otherwise how do you tigger an update without mutability?val (messages, messagesChanges) = +state<List<Message>> { emptyList() }
Leland Richardson [G]
11/01/2019, 6:33 PMvar
if you’re using it as a property delegateaballano
11/01/2019, 6:37 PMLeland Richardson [G]
11/01/2019, 6:39 PMdisposeComposition
in onDestroy+onDispose { ... }
aballano
11/01/2019, 6:41 PMLeland Richardson [G]
11/01/2019, 6:41 PM+onCommit { ... }
onCommit
has its own onDispose
+onCommit {
myThing.subscribe(...)
onDispose { myThing.unsubscribe(...) }
}
myThing
was a parameter+onCommit(myThing) {
myThing.subscribe(...)
onDispose { myThing.unsubscribe(...) }
}
and then this will only call the body of onCommit
whenever myThing
changesonActive
which happens on the first composition only. This is equivalent to onCommit(someConstant)
mainContent(MainModel(messages), ::onButtonClicked)
did you wrap messages
in MainModel
?aballano
11/01/2019, 6:49 PMLeland Richardson [G]
11/01/2019, 6:54 PMval model = +memo { MainModel() }
model.messages = messages + it
aballano
11/01/2019, 6:59 PMLeland Richardson [G]
11/01/2019, 7:02 PMLuca Nicoletti
11/01/2019, 7:03 PMdo you need to wrap aAnswer is yes?annotated class with@Model
ormemo
or the annotation does it for you already?state
aballano
11/01/2019, 7:06 PM+memo { MyModel("text") }
or
+state { "text" }
if you don't need the MyModel wrapperLeland Richardson [G]
11/01/2019, 7:06 PMLuca Nicoletti
11/01/2019, 7:06 PMLeland Richardson [G]
11/01/2019, 7:06 PMLuca Nicoletti
11/01/2019, 7:07 PM+state
wrap the value you provide in a State
instance, which is a @Model
annotated classLeland Richardson [G]
11/01/2019, 7:07 PMLuca Nicoletti
11/01/2019, 7:07 PMLeland Richardson [G]
11/01/2019, 7:07 PMaballano
11/01/2019, 7:07 PMLuca Nicoletti
11/01/2019, 7:07 PM