Skip to content

SubMob/ScopeMob

Repository files navigation

ScopeMob

badge badge badge badge

ScopeMob CI Maven Central Codacy Badge Codacy Coverage Codecov Badge

Install

ScopeMob is currently published to Maven Central, so add that to repositories.

repositories {
    mavenCentral()
}

Then, simply add the dependency to your common source-set dependencies

commonMain {
    dependencies {
        implementation("com.github.submob:scopemob:LATEST_VERSION")
    }
}

Documentation

whether/whetherNot

whether and whetherNot takes all the lambda parameters and applies and/&& operator. whether returns the caller object to let if the result is true otherwise it returns false so ?:run blocks will run.

whetherNot does the same but when the result is false.

SomeObject
    ?.whether(
        { it.someBoolean }, // `it` is SomeObject
        { this.someBoolean }, // `this` is some object, you can also use simply someBoolean
        { someOtherBoolean }
    )?.let {
        // runs if all the conditions are true
    } ?: run {
    // runs if 
}

// Syntax with one argument
SomeObject
    ?.whether { it.someBoolean } // `it` is SomeObject
    ?.whether { this.someBoolean } // `this` is some object, you can also use simply someBoolean
    ?.whether { someOtherBoolean }
    ?.let {
        // runs if all the conditions are true
    } ?: run {
    // runs if 
}
either/eitherNot

either and eitherNot takes all the lambda parameters and applies or/|| operator. either returns the caller object to let if the result is true otherwise it returns false so ?:run blocks will run.

eitherNot does the same but when the result is false.

SomeObject
    ?.either(
        { it.someBoolean }, // `it` is SomeObject
        { this.someBoolean }, // `this` is some object, you can also use simply someBoolean
        { someOtherBoolean }
    )?.let {
        // runs if all the conditions are true
    } ?: run {
    // runs if 
}
inCase/inCaseNot

inCase and inCaseNot takes a condition as parameter and if it is true runs the lambda block. It returns the caller object no matter the condition.

inCaseNot does the same but when the condition is false.

SomeObject
    ?.inCase(someCondition) { /* this will run only someCondition true */ } // you can also reach SomeObject inside the scope with `it` or `this`
    ?.let {
        // runs if the original SomeObject is not null
    } ?: run {
    // runs if the original SomeObject is null
}

// popular example from Android world
AlertDialog
    .Builder(activity, R.style.AlertDialogCustom)
    .setIcon(R.mipmap.ic_launcher)
    .setTitle(title)
    .setMessage(description)
    .setPositiveButton(positiveButton) { _, _ -> function() }
    .setCancelable(cancelable)
    .inCase(cancelable) { setNegativeButton(activity.getString(R.string.cancel), null) } // this will run only cancelable is true
    .show()
ensure

ensure takes many variables as argument and let the block work only if all the variables are not null.

ensure(variable1, variable2, variable3, ...) {
    // The block run only if all the variables are not null
}

License

Copyright 2020 Mustafa Ozhan

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

Set of useful scope and Higher-order functions

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages