/Android-Phase

An Android library to hold any data within states.

Primary LanguageKotlinApache License 2.0Apache-2.0

Android Phase

Table of Contents

About The Library

An Android library to hold any data within states. For the example use of this library, you can look at my Movee project on Github. If you want, you can also use this class GetPhaseFlow.

Adding The Dependency

Add jitpack into the repositories

maven { url 'https://jitpack.io' }

Add the dependency in build.gradle file.

implementation 'com.github.mutkuensert:Android-Phase:v2.0.0'

You can also add the library locally. To do that:

Clone the project.

Run in the terminal:

./gradlew build

Find generated aar file under build folder and copy & paste it into your project. You can create a directory called "libs" under your app module and paste the file into there. Then you can add the dependency:

implementation files('libs/AndroidPhase-release.aar')

Example Use

suspend fun getTvShowDetails(tvShowId: Int): Phase<TvShowDetails> {
    val response = tvShowsApi.getTvShowDetails(tvShowId)

    return if (response.isSuccessful) {
        Phase.Success(data = response.body()?.mapToDomainModel())
    } else {
        Phase.Error(message = "Unsuccessful response.")
    }
}

You can use Compose extension: Phase Compose Extension

@Composable
private fun TvShowDetails(
    phase: Phase<TvShowDetails>,
    loadTvCastIfSuccessful: () -> Unit
) {
    phase.Execute(
        onLoading = { Loading() },
        onSuccess = { //it: TvShowDetails
            TvDetailsItem(it)
            LaunchedEffect(Unit) { loadTvCastIfSuccessful() }
        },
        onError = { //it: Phase.Error<TvShowDetails>
            LocalContext.current.showToastIfNotNull(it.message)
        })
}

License

Copyright 2023 Mustafa Utku Ensert

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.

Contact

ensertyazilim@gmail.com