Giter VIP home page Giter VIP logo

navgen's Introduction

NavGen

A sample compose navigation destination code generation with kotlin symbols processing.

Usage/Examples

There are two screens , ScreenA (start destination) and ScreenB

@Destination("screen_a")
class ScreenA {
}
@Destination("screen_b")
data class ScreenB(
    val message : String
)

ScreenA want to pass message string to ScreenB.

Generated Code

For ScreenA

public object ScreenADestination {
  public fun destination(): String = "screen_a"

  public fun route(): String = "screen_a"
}

For ScreenB

public object ScreenBDestination {
  public val MESSAGE: String = "message"

  public fun destination(): String = "screen_b?message={message}"

  public fun route(message: String): String = "screen_b?message=${message}"

  public fun message(bundle: Bundle): String = bundle.getString("message")!!

  public fun types() = listOf(androidx.navigation.navArgument("message"){
  type = androidx.navigation.NavType.StringType
  nullable = false
  })
}

Example Usage

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposeNavGenTheme {
                val navController = rememberNavController()
                NavHost(
                    navController = navController,
                    startDestination = ScreenADestination.destination()
                ) {
                    composable(
                        route = ScreenADestination.destination()
                    ) {
                        ScreenA {
                            navController.navigate(
                                ScreenBDestination.route("Hello i am from A")
                            )
                        }
                    }
                    composable(
                        arguments = ScreenBDestination.types(),
                        route = ScreenBDestination.destination()
                    ) {
                        ScreenB(
                            ScreenBDestination.message(it.arguments!!)
                        )
                    }

                }
            }
        }
    }
}

@Composable
fun ScreenA(onClick: () -> Unit) {
    Box(modifier = Modifier.fillMaxSize()) {
        Button(
            onClick = onClick, modifier = Modifier.align(
                Alignment.Center
            )
        ) {
            Text(text = "Go to B")
        }
    }
}

@Composable
fun ScreenB(message: String) {
    Box(modifier = Modifier.fillMaxSize()) {
        Text(
            text = message, modifier = Modifier.align(
                Alignment.Center
            )
        )

    }
}

Limination

Currently not available default parameters and only available primitive types but not a Double , because of NavType.DoubleType not support by compose navigation.

navgen's People

Contributors

zayphyo01 avatar naingaungluu avatar zayyarpyaephyoe-codigo avatar

Stargazers

Thaw Zin Toe avatar  avatar

Watchers

 avatar

Forkers

naingaungluu

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.