Coder Social home page Coder Social logo

mirego / killswitch-mobile Goto Github PK

View Code? Open in Web Editor NEW
1.0 22.0 0.0 335 KB

Multiplatform library for using the Mirego Killswitch inside mobile applications

License: BSD 3-Clause "New" or "Revised" License

Kotlin 100.00%
killswitch kotlin-multiplatform

killswitch-mobile's Introduction

Killswitch is a clever control panel built by Mirego that allows mobile developers to apply
runtime version-specific behaviors to their iOS or Android application.

What is Killswitch?

Killswitch is a clever control panel built by Mirego that allows mobile developers to apply runtime version-specific behaviors to their iOS or Android application.

Setup

The library is published to Mirego's public Maven repository, so make sure you have it included in your settings.gradle.kts dependencyResolutionManagement block.

dependencyResolutionManagement {
    repositories {
        // ...
        maven("https://s3.amazonaws.com/mirego-maven/public")
    }
}

You can then add the dependency to your common source set dependencies in your build.gradle.kts.

commonMain {
    dependencies {
        // ...
        api("com.mirego.killswitch-mobile:killswitch:x.y.z")
    }
}

To make the code available on iOS, don't forget to export the library in the framework.

kotlin {
    androidTarget()
    listOf(
        iosX64(),
        iosArm64(),
        iosSimulatorArm64()
    ).forEach {
        it.binaries {
            framework {
                // ...
                export("com.mirego.killswitch-mobile:killswitch:x.y.z")
            }
        }
    }
}

On iOS, if the Killswitch is engaged from another thread than the main one, you may need to add this line in your project's gradle.properties to prevent your app from crashing.

kotlin.native.binary.objcExportSuspendFunctionLaunchThreadRestriction=none

Reference: https://youtrack.jetbrains.com/issue/KT-51297/Native-allow-calling-Kotlin-suspend-functions-on-non-main-thread-from-Swift

Usage

There is two ways of using the Killswitch: by letting the library display a native dialog or by implementing a custom UI.

Native dialog

With this approach it is easy to display a native dialog on the launch of your app.

Android

In the onCreate() function of your main Activity, you can engage the Killswitch in a CoroutineScope and let the library handle the response in order to display the native dialog.

lifecycleScope.launch {
    try {
        AndroidKillswitch.showDialog(
            AndroidKillswitch.engage(KILLSWITCH_API_KEY, this@MainActivity, KILLSWITCH_URL),
            this@MainActivity,
            android.R.style.Theme_DeviceDefault_Light_Dialog_Alert
        )
    } catch (e: KillswitchException) {
        Log.e(TAG, "Killswitch exception", e)
    }
}

iOS

In the application() function of your AppDelegate, you can engage the Killswitch in a Task and let the library handle the response in order to display the native dialog.

Task {
    do {
        let viewData = try await IOSKillswitch().engage(key: KILLSWITCH_API_KEY, url: KILLSWITCH_URL)
        DispatchQueue.main.async {
            IOSKillswitch().showDialog(viewData: viewData)
        }
    } catch {
        print("Killswitch error: \(error)")
    }
}

Custom UI

With this approach you are free to customize the Killswitch UI and UX the way you want. It could either occupy the whole screen with an opaque background, or be displayed on top of the main view with some alpha.

Android

In the root view of your application, you can engage the Killswitch inside a LaunchedEffect and decide which view to display depending on the state of the view data.

var viewData by remember { mutableStateOf<KillswitchViewData?>(null) }

when (val localViewData = viewData) {
    is KillswitchViewData -> CustomDialog(
        viewData = localViewData,
        dismiss = { viewData = null },
        navigateToUrl = this@MainActivity::navigateToKillswitchUrl
    )
    else -> MainView()
}

val lifecycleOwner = LocalLifecycleOwner.current
val context = LocalContext.current

LaunchedEffect(true) {
    lifecycleOwner.lifecycle.coroutineScope.launch {
        try {
            viewData = AndroidKillswitch.engage(KILLSWITCH_API_KEY, context, KILLSWITCH_URL)
        } catch (e: KillswitchException) {
            Log.e(TAG, "Killswitch exception", e)
        }
    }
}

You can find a sample CustomDialog Compose view here.

iOS

In the root view of your application, you can engage the Killswitch inside a task modifier and decide which view to display depending on the state of the view data.

@State private var viewData: KillswitchViewData? = nil

var body: some View {
    ZStack {
        if let viewData = viewData {
            CustomDialog(viewData: viewData) {
                self.viewData = nil
            }
        } else {
            MainView()
        }
    }
    .task {
        do {
            self.viewData = try await IOSKillswitch().engage(key: KILLSWITCH_API_KEY, url: KILLSWITCH_URL)
        } catch {
            print("Killswitch error: \(error)")
        }
    }
}

You can find a sample CustomDialog SwiftUI view here.

License

Killswitch is © 2013-2023 Mirego and may be freely distributed under the New BSD license. See the LICENSE.md file.

The shield logo is based on this lovely icon by Kimmi Studio, from The Noun Project. Used under a Creative Commons BY 3.0 license.

About Mirego

Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun. We’re a team of talented people who imagine and build beautiful Web and mobile applications. We come together to share ideas and change the world.

We also love open-source software and we try to give back to the community as much as we can.

killswitch-mobile's People

Contributors

christophetremblay avatar mathieularue avatar remi avatar

Stargazers

 avatar

Watchers

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

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.