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

DSL for navigating between activities #62

Open

Description

@stanch

Activity

  1. rickrickrickyyy commented on Jul 18, 2017

    @rickrickrickyyy

    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))
      }
  2. fedefernandez commented on Jul 24, 2017

    @fedefernandez

    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.

  3. rickrickrickyyy commented on Jul 25, 2017

    @rickrickrickyyy

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

  4. javipacheco commented on Jul 25, 2017

    @javipacheco
    Collaborator

    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!

  5. rickrickrickyyy commented on Jul 27, 2017

    @rickrickrickyyy

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

    Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions