Skip to content

Instantly share code, notes, and snippets.

@kdbrian
kdbrian / ErrorHandler.kt
Created December 11, 2024 19:22
Am having an error wrapper class over the normal Exception interface, with this I can parse errors and handle them for a better response , for example the Socket timeout exception which is common in most network requests.
sealed class AppError(
override val message: String? = null
) : Exception(){
data object SocketTimeOutError : AppError("Failed to connect, please check your internet connection and retry.")
}
@kdbrian
kdbrian / MainViewModelFactory.kt
Created December 10, 2024 03:41
A viewmodel factory, can be used to provision a viewmodel from a kotlin application that has lifecycle.
@Suppress("UNCHECKED_CAST")
class MainViewModelFactory(val application: Application) : ViewModelProvider.Factory {
override fun <T : androidx.lifecycle.ViewModel> create(modelClass: Class<T>): T {
return MainViewModel(application) as T
}
}
//example of usage in a composable
LocalViewModelStoreOwner.current?.let {
@kdbrian
kdbrian / JwtService.java
Last active December 10, 2024 03:42
An abstraction of spring security methods to interact with the jwt, requires the libraries jjwt-api jjwt-impl jjwt-jackson The implementation is really similar but can be customized
public interface JwtService {
final String SECRET_KEY = "";
String generateToken(
Map<String, Object> extraClaims,
UserDetails userDetails
);