Giter Club home page Giter Club logo

rekotlinfragmentexample's Introduction

ReKotlin Fragment Example

A Fragment example, build with ReKotlin and ReKotlin-Router

Installation

  1. Clone this repository
  2. Open in android studio.
  3. Build & Run.

Details

Dispatching actions that request routing Fragments

You can dispatch the actions that invoke the Fragments similar to Activity .

val routes = arrayListOf(mainActivityRoute, backStackActivityRoute, oneFragmentRoute)
val action = SetRouteAction(route = routes)
val actionData = SetRouteSpecificData(route= routes,data = FragmentDataValue(activity, true))
mainStore.dispatch(actionData)
mainStore.dispatch(action)

The function SetRouteSpecificData passes the data to Router to create the fragment.

What kind data need to be passed?

The signature of FragmentDataValue looks like

class FragmentDataValue(val activity: WeakReference<AppCompatActivity>, val addToBackStack: Boolean)

We need to pass the WeakReference of the activity.

Routable that creates Fragments

You should override the functions to create the Fragments

 override fun pushRouteSegment(routeElementIdentifier: RouteElementIdentifier,
                                  animated: Boolean,
                                  completionHandler: RoutingCompletionHandler): Routable {
        when (routeElementIdentifier) {
            oneFragmentRoute -> {
                return RoutableHelper.backStackFragmentRoutable(fragment = OneFragment(),
                        tag = "OneFragment")
            }

            twoFragmentRoute -> {
                return RoutableHelper.backStackFragmentRoutable(fragment = TwoFragment(),
                        tag = "TwoFragment")
            }
        }
    }

Let's look at the function backStackFragmentRoutable

fun backStackFragmentRoutable(fragment: Fragment,tag: String): FragmentRoutable {
        val currentRoute = mainStore.state.navigationState.route
        val intentData: FragmentDataValue =
                mainStore.state.navigationState
                        .getRouteSpecificState<FragmentDataValue>(currentRoute)!!
        val activity = intentData.activity.get()!!
        val addToBackStack = intentData.addToBackStack
        addFragment(fragment, activity, addToBackStack, tag,R.id.container_frame_back_stack)
        //changeFragment(fragment,activity,addToBackStack,tag,R.id.container_frame_back)

        return FragmentRoutable(activity.applicationContext)
}

It does the following

  • get the current route from navigationState
  • From the current route, get the fragment's intentData
  • With the intentData, get the activity from its WeakReference and Boolean value whether to add to stack.
  • Call the function addFragment, helper method to create the fragment.

Unit Testing

  1. TBD

rekotlinfragmentexample's People

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

rekotlin

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.