Skip to content

Instantly share code, notes, and snippets.

View virendersran01's full-sized avatar
💻
Working from home

Virender Srxn virendersran01

💻
Working from home
  • India
View GitHub Profile
@virendersran01
virendersran01 / Circles.kt
Created September 21, 2023 08:07 — forked from nirbhayph/Circles.kt
Google Maps and Jetpack Compose
@Composable
fun CircleMap() {
// Define the coordinates for circle centers and their associated information
val circleData = listOf(
CircleInfo("Park A", LatLng(37.7749, -122.4194), "This is Park A"),
CircleInfo("Park B", LatLng(36.7783, -119.4179), "This is Park B"),
CircleInfo("Park C", LatLng(34.0522, -118.2437), "This is Park C")
)
// Create a mutable state to track the selected circle
@virendersran01
virendersran01 / better-url-intent.kt
Created September 13, 2023 13:04 — forked from jack-webb/better-url-intent.kt
A better way to open a link in the user's browser
val url = Uri.parse("https://www.asos.com/")
val browserSelectorIntent = Intent()
  .setAction(Intent.ACTION_VIEW)
  .addCategory(Intent.CATEGORY_BROWSABLE)
  .setData(Uri.parse("http:"))
val targetIntent = Intent()
  .setAction(Intent.ACTION_VIEW)
  .addCategory(Intent.CATEGORY_BROWSABLE)
@virendersran01
virendersran01 / .kt
Created August 24, 2023 03:19 — forked from ZaqueuLima3/Fixture.kt
Fixture
import kotlin.random.Random
import kotlin.reflect.KClass
import kotlin.reflect.KFunction
import kotlin.reflect.KType
/**
* You can find the method explanation here on this post:
* https://zaqueusantos.medium.com/an-easy-way-to-create-fixture-or-dummy-classes-for-your-android-tests-with-kotlin-9d6c619237d4
*/
@virendersran01
virendersran01 / LocationService.kt
Created July 23, 2023 14:40 — forked from oussama-dz/LocationService.kt
A Location service class that get the current user location, and handle any exceptions that maybe thrown.
class LocationService {
@SuppressLint("MissingPermission")
suspend fun getCurrentLocation(context: Context): Location {
if (!context.hasPermissions(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
)
) {
sealed interface SallyResponseResource<out T> {
data class Success<T>(val data: T) : SallyResponseResource<T>
data class Error(val exception: AppException, val errorCode: String? = null) :
SallyResponseResource<Nothing>
data class Loading(val status: Boolean) : SallyResponseResource<Nothing>
}
open class AppException(message: String? = null, cause: Throwable? = null) :
Throwable(message, cause)
class NetworkException(message: String? = null, cause: Throwable? = null) :
AppException(message, cause)
class ServerException(message: String? = null, cause: Throwable? = null) :
AppException(message, cause)
class ClientException(message: String? = null, cause: Throwable? = null) :
suspend fun <T> asSallyResponseResourceSuspend(apiCall: suspend () -> T): SallyResponseResource<T> {
return try {
SallyResponseResource.Loading(true)
val response = apiCall.invoke()
SallyResponseResource.Success(response)
} catch (error: Throwable) {
val exception = when (error) {
is HttpException -> {
when (error.code()) {
in 400..499 -> {
fun <T> Flow<T>.asSallyResponseResourceFlow(): Flow<SallyResponseResource<T>> {
return this
.map<T, SallyResponseResource<T>> {
SallyResponseResource.Success(it)
}
.onStart { emit(SallyResponseResource.Loading(true)) }
.onCompletion { emit(SallyResponseResource.Loading(false)) }
.catch { error ->
val exception = when (error) {
is HttpException -> {
const val CLIENT_ERROR = "Terjadi kesalahan, mohon periksa masukan anda"
const val SERVER_ERROR = "Terjadi kesalahan pada Server, coba lagi nanti"
const val NETWORK_ERROR = "Koneksi internet bermasalah, coba lagi nanti"
const val HTTP_UNKNOWN_ERROR = "HTTP Error tidak diketahui (exc: 4xx/5xx)"
const val UNKNOWN_ERROR = "Error tidak diketahui"