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
@GET("list/{id}")
suspend fun getDetailAgentVisitWithFullResponse(
@Header("Authorization") token: String,
@Path(value = "id", encoded = true) id: Int
): BaseResponse<GetAgentDetailResponse>
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"
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 -> {
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 -> {
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) :
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>
}
@jack-webb
jack-webb / better-url-intent.kt
Created June 1, 2023 10:44
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)
@oussama-dz
oussama-dz / LocationService.kt
Created May 31, 2023 07:01
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
)
) {
@landomen
landomen / CounterButtonSample.kt
Created May 26, 2023 19:08
Animated counter button sample implemented in Jetpack Compose
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
CounterButtonTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
@giovanileitevitor
giovanileitevitor / access_placeholders.xml
Created January 9, 2023 20:54
Accessing PlaceHolders
<application
android:name=".di.Application"
android:icon="${appIcon}"
android:roundIcon="${appIcon}"
....
</aplication>