-
Notifications
You must be signed in to change notification settings - Fork 37
DSL for navigating between activities #62
Comments
Hi implicit class IntentVisitor(value: Any) {
def visit(intent: Intent, key: String): Unit = {
value match {
case v: Short => intent.putExtra(key, v)
case v: Int => intent.putExtra(key, v)
case v: Byte => intent.putExtra(key, v)
case v: Double => intent.putExtra(key, v)
case v: Float => intent.putExtra(key, v)
case v: Long => intent.putExtra(key, v)
case v: String => intent.putExtra(key, v)
case v: Boolean => intent.putExtra(key, v)
case v: Char => intent.putExtra(key, v)
case v: CharSequence => intent.putExtra(key, v)
case v: Serializable => intent.putExtra(key, v)
case v: Bundle => intent.putExtra(key, v)
case v: Parcelable => intent.putExtra(key, v)
case v: Any => throw new IllegalArgumentException(s"Please don't put ${v.getClass} into the extra") }
}
}
private def startActivity(cls: Class[_ <: Activity], extra: (String, Any)*)(implicit context: ContextWrapper): Ui[Unit] = {
val intent = new Intent(context.getOriginal, cls)
extra.foreach(elem => elem._2.visit(intent, elem._1))
Ui(context.getOriginal.startActivity(intent))
}
private def startActivityForResult(cls: Class[_ <: Activity], extra: (String, Any)*)(requestCode: Int = 0)(implicit context: ActivityContextWrapper): Ui[Unit] = {
val intent = new Intent(context.getOriginal, cls)
extra.foreach(elem => elem._2.visit(intent, elem._1))
Ui(context.getOriginal.startActivityForResult(intent, requestCode))
} |
I think that the problem is that the array is not covered by the pattern matcher. IMHO this question is not related with the issue or even macroid, it seems related with the Scala language. Please, ask the same question in stackoverflow. I'll delete the comment in some days to keep the issue clearer. |
If you change the above method startActivity to public and rename it to "goto"
|
The problem is that your match is not exhaustive. You should cover all the cases for case class Strings(array: String*)
case class Ints(array: Int*)
.... Then you can use it at the beginning of your match: def visit(intent: Intent, key: String): Unit = {
value match {
case v: Ints => intent.putExtra(key, v.array)
case v: Strings => intent.putExtra(key, v.array)
....
case v: Short => intent.putExtra(key, v)
case v: Int => intent.putExtra(key, v)
....
}
}
} I don't have tried the code, it´s only a proof of concept. Maybe it´s not the better solution, but you can investigate around that. If you want to contribute, you can create a PR to Thanks! |
I have just created a PR. Could you review it and give me some feedback? Thanks |
remove Parcelable add a case class to wrap array in Serializable add method to startActivityForResult use method overloading
See https://groups.google.com/forum/#!topic/macroid/BFDlv0Kmohc
The text was updated successfully, but these errors were encountered: