Skip to content

Latest commit

 

History

History
108 lines (79 loc) · 3.38 KB

README.md

File metadata and controls

108 lines (79 loc) · 3.38 KB

SnackController

Lifecycle-aware library to show fully customizable snacks.
Inspired by Cicerone and Surf-Navigation.

Connection

// TODO

Documentation

The main idea is to use fragments as snacks. In that way a user of this library has full controll of creating, rendering, showing and hiding snacks.

Quick Start

Create SnackController

class App: Application() {
    val snackController = SnackController.create(this)
}

Create a fragment file which you want to use as a snack

class ExampleSnackFragment: Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_snack_example, container, false)
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        // TODO extract your data from arguments here
    }
}

Create a snack class and implement Snack interface

data class ExampleSnack(val text: String): Snack {
    override val containerId: Int = android.R.id.content  
    override val timeoutMs: Long = 1500L 
    override val animations: Animations = FromBottomToBottomAnimations
    override val className: String = "ru.sharipov.example.ExampleSnackFragment"
    override val tag: String = "ExampleSnackFragment $text"

    override fun prepareBundle(bundle: Bundle) {
        bundle.putString(SNACK_TEXT_ARGS, text)
    }

    companion object {
        const val SNACK_TEXT_ARGS = "SNACK_TEXT_ARGS"
    }
}

Show and hide snack

val snack = ExampleSnack(text = "Some useful information for the user here")
snackController.open(snack)
//snackController.close(snack)

Tips

  1. Use custom containerId if you want snacks to appear in some particular container
  2. Set timeoutMs field as null if you don't want your snacks to hide automatically
  3. Be sure to keep tags unique throughout all snacks, especially if they appear on the screen at the same time
  4. Check out the example module to see more examples of usage
  5. You can override animations field if you want something extraordinary ;)
  6. You can pass custom fragment factory to SnackNavigationLifecycleCallbacks if you want to gain full access over fragment creation

Roadmap

1. Migrate to FragmentFactory for fragment creation instead of using reflection.
2. Implement snack queue and config with strategies of adding snacks to queue when the navigator is not able to show them, drop oldest or even show only last added.
3. Add tests.
4. Add documentation.
5. More examples.
6. Deploy artefacts to have the ability of using the library with the gradle.

Videos

Top snack with fade animations
Screenrecorder-2022-03-27-15-53-55-359.mp4
Bottom snack with slide animations
Screenrecorder-2022-03-27-15-54-00-556.mp4
Complicated snack

It has it's own state, no timeout and it is shown in custom container

Screenrecorder-2022-03-27-15-54-05-163.mp4