Skip to content
This repository has been archived by the owner on Nov 30, 2022. It is now read-only.

DSL for navigating between activities #62

Open
stanch opened this issue Apr 30, 2015 · 5 comments
Open

DSL for navigating between activities #62

stanch opened this issue Apr 30, 2015 · 5 comments

Comments

@stanch
Copy link
Collaborator

stanch commented Apr 30, 2015

See https://groups.google.com/forum/#!topic/macroid/BFDlv0Kmohc

@rickrickrickyyy
Copy link

rickrickrickyyy commented Jul 18, 2017

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))
  }

@fedefernandez
Copy link

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.

@rickrickrickyyy
Copy link

If you change the above method startActivity to public and rename it to "goto"
you can use this code to navigate into an activity like this

<~ On.Click(goto(classOf[SomeActivity],"EXTRA"->id))

@javipacheco
Copy link
Collaborator

javipacheco commented Jul 25, 2017

The problem is that your match is not exhaustive. You should cover all the cases for List because of the Intent class has one method for every type of the list. For me, the code could be a little bit tricky but you can create classes for every type for containing the list. Something like that:

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 macroid-extras in the UIActionsExtras class

Thanks!

rickrickrickyyy added a commit to rickrickrickyyy/macroid that referenced this issue Jul 27, 2017
rickrickrickyyy added a commit to rickrickrickyyy/macroid that referenced this issue Jul 27, 2017
@rickrickrickyyy
Copy link

I have just created a PR. Could you review it and give me some feedback?

Thanks

rickrickrickyyy added a commit to rickrickrickyyy/macroid that referenced this issue Jul 28, 2017
remove Parcelable
add a case class to wrap array in Serializable
add method to startActivityForResult
use method overloading
rickrickrickyyy added a commit to rickrickrickyyy/macroid that referenced this issue Aug 9, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants