CacheFlow SDK provides a robust and efficient solution for caching network responses in Android applications. It simplifies network requests by automatically handling caching, and also provides optional file download capabilities.
Add the CacheFlow SDK to your build.gradle
file:
dependencies {
implementation 'com.github.birdeveloper:CacheFlow:v1.0.0'
}
Before making any requests, initialize CacheFlow with your configuration:
import com.cacheflow.sdk.CacheFlowConfig
import com.cacheflow.sdk.CacheFlow
val cacheFlowConfig = CacheFlowConfig(
cacheDuration = 60 * 60 * 1000, // 1 hour
isOfflineModeEnabled = true,
responseType = MyResponse::class,
errorType = MyError::class
)
CacheFlow.initialize(
config = cacheFlowConfig,
context = applicationContext,
baseUrl = "https://api.example.com"
)
You can make requests using CacheFlow with Flow:
import com.cacheflow.sdk.CacheFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
val url = "https://api.example.com/data"
launch {
CacheFlow.performRequest(url) {
apiService.getData()
}.collect { result ->
when (result) {
is Result.Success -> {
val data = result.data
// Handle success
}
is Result.Failure -> {
val error = result.message
// Handle error
}
is Result.Loading -> {
// Handle loading state
}
}
}
}
Alternatively, you can use a request listener:
import com.cacheflow.sdk.CacheFlow
import com.cacheflow.sdk.ResultListener
CacheFlow.performRequest(
url = "https://api.example.com/data",
apiCall = { apiService.getData() },
listener = object : ResultListener<MyResponse> {
override fun onLoading() {
// Handle loading
}
override fun onSuccess(data: MyResponse?) {
// Handle success
}
override fun onFailure(message: String) {
// Handle failure
}
}
)
CacheFlow can automatically detect and handle downloadable content:
import com.cacheflow.sdk.CacheFlow
import com.cacheflow.sdk.ResultListener
import java.io.File
CacheFlow.performRequest(
url = "https://example.com/file.zip",
apiCall = { apiService.downloadFile() },
listener = object : ResultListener<File> {
override fun onLoading() {
// Handle loading
}
override fun onSuccess(data: File?) {
// Handle file download success
}
override fun onFailure(message: String) {
// Handle failure
}
}
)
Note: If you expect the response to be a downloadable file, ensure you provide a ResultListener<File>
to handle the download progress and completion.
For a more in-depth guide, including advanced usage and additional features, refer to the detailed documentation.