Giter Site home page Giter Site logo

kevinnzou / compose-multiplatform-lifecycle-tracker Goto Github PK

View Code? Open in Web Editor NEW
12.0 2.0 0.0 63.09 MB

Lifecycle tracker for JetBrains Compose Multiplatform

Home Page: https://kevinnzou.github.io/compose-multiplatform-lifecycle-tracker/

License: Apache License 2.0

Shell 2.21% Kotlin 93.80% JSON 1.77% Swift 2.22%
android compose-multiplatform ios kotlin kotlin-multiplatform lifecycle

compose-multiplatform-lifecycle-tracker's Introduction

Compose Multiplatform Lifecycle Tracker

Maven Central Kotlin Compose Multiplatform

badge-android badge-ios

A library to track the lifecycle of the application in Compose Multiplatform.

Setup

To use this library, you need some setup in your project.

Android

This library uses the androidx.lifecycle:lifecycle-runtime-ktx library to track the lifecycle of the page. You need to add the following dependency to your build.gradle.kts file.

val androidMain by getting {
   dependencies {
      implementation ("androidx.lifecycle:lifecycle-runtime-ktx:2.7.0")
   }
}

Then, you need to add the AndroidLifecycleEventObserver to your Activity or Fragment to track the lifecycle of the page and provide the LifecycleTracker to the App using CompositionLocalProvider.

class MainActivity : AppCompatActivity() {
    private val lifecycleTracker = LifecycleTracker()
    private val observer = AndroidLifecycleEventObserver(lifecycleTracker)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        lifecycle.addObserver(observer)

        setContent {
            CompositionLocalProvider(LocalLifecycleTracker provides lifecycleTracker) {
                MainView()
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        lifecycle.removeObserver(observer)
    }
}

iOS

For iOS, you need to set the 'LifecycleComposeUIVCDelegate' as the delegate of the UIComposeViewController to track the lifecycle of the page and provide the LifecycleTracker to the App using CompositionLocalProvider.

fun MainViewController(): UIViewController {
    val lifecycleTracker = LifecycleTracker()
    return ComposeUIViewController({
        delegate = LifecycleComposeUIVCDelegate(lifecycleTracker)
    }) {
        CompositionLocalProvider(LocalLifecycleTracker provides lifecycleTracker) {
            App()
        }
    }
}

Usage

The usage is quite simple, you just need to create a 'LifecycleListener' and add it to the LifecycleTracker.

@Composable
private fun LifecycleTest() {
    val lifecycleTracker = LocalLifecycleTracker.current
    DisposableEffect(Unit) {
        val listener =
            object : LifecycleObserver {
                override fun onEvent(event: LifecycleEvent) {
                    println("Lifecycle: onEvent: $event")
                }
            }
        lifecycleTracker.addObserver(listener)
        onDispose {
            lifecycleTracker.removeObserver(listener)
        }
    }
}

Download

Maven Central

This library is available on Maven Central. You can add it to your project by adding the following dependency to your build.gradle.kts file.

repositories {
    mavenCentral()
}

kotlin {
    sourceSets {
        commonMain {
            dependencies {
              api("io.github.kevinnzou:compose-multiplatform-lifecycle-tracker:1.0.0")
            }
        }
    }
}

compose-multiplatform-lifecycle-tracker's People

Contributors

kevinnzou avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

compose-multiplatform-lifecycle-tracker's Issues

can't implemented to the project

Hi!

I add implementation("io.github.kevinnzou:compose-multiplatform-lifecycle-tracker:1.0.0") to the commonMain dependency in the shared module and implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.7.0 ) in the androidMain section. But MainActivity doesn't see the lifecycle package.
I don't understand what I did wrong...

OnPauseEvent doesn't work

The OnPause Event does not arrive when exiting the application on iOS

compose = "1.6.1"
compose-plugin = "1.5.12"
ios = 17.0.3

return event in compose

Hi!

I look at sample, works fine. But how I can return event in compose fun or start another compose fun from "onEvent" ?

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.