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
@AsheeshSharma
AsheeshSharma / dynamic_aspect_ratio.kt
Last active December 27, 2022 08:24
Dynamic aspect ratio handling with glide
fun setUpOneDimenNoAspectRatioImage(imageUrl: String?, imageView: AppCompatImageView, @DimenRes defaultHeight: Int, @DimenRes marginToBeAdjusted: Int, availableWidth: Int? = null) {
if (imageUrl?.isNotEmpty() == true) {
imageView.visibility = View.VISIBLE
GlideApp.with(imageView.context)
.load(imageUrl)
.centerCrop()
.into(object : CustomTarget<Drawable>() {
override fun onLoadFailed(errorDrawable: Drawable?) {
}
@handstandsam
handstandsam / print_permissions_from_androidmanifest.py
Last active December 14, 2022 10:06
Python Script to parse permissions from an AndroidManifest.xml file, and sort them alphabetically.
from xml.dom.minidom import parseString
# Documentation on Permissions in AndroidManifest.xml
# https://developer.android.com/guide/topics/manifest/manifest-intro#perms
data = '' # string data from file
with open('AndroidManifest.xml', 'r') as f:
data = f.read()
dom = parseString(data) # parse file contents to xml dom
class LoginFragment : Fragment(){
private val viewModel: LoginViewModel by viewModels()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val binding = LoginFragmentBinding.inflate(inflater, container, false)
sealed class Result<out T>{
object Loading(): Result<Nothing>()
class Success<T>(val value: T): Result<T>()
class Error(val message:String): Result<Nothing>()
}
data class User(val id: String, val name:String, val token: String){
fun toJson(): Map<String, String> = mapOf {
"id" to id,
"name" to name,
"token" to token,
}
}
interface AuthRepository{
fun isSignedIn(): Flow<Result<Boolean>>
fun isRegistered(token: String): Flow<Result<Boolean>>
fun signIn(user: User): Flow<Result<Boolean>>
fun register(user: User): Flow<Result<Boolean>>
}
class AuthRepoImpl(val remoteService: MyService){
override fun isSignedIn(): Flow<Result<Boolean>> =
callbackFlow {
data class LoginUiState(
val loading: Boolean = false,
val navigateToHome = false,
val navigateToRegistration = false,
val errorMessages = listOf<String>()
)
class LoginViewModel(val repository: AuthRepository): ViewMode() {
private val _state = mutableStateOf(LoginUiState())
val state = _state.asStateFlow()
fun onLoginAttemp(user: User) {
viewModelScope.launch {
repository.isRegistered(user).collect { registered ->
when (registered) {
is Result.Loading -> {
_state.update { it.copy(loading = true) }
@fadhifatah
fadhifatah / AdaptiveSpacingItemDecoration.kt
Created March 8, 2022 07:59
New file: AdaptiveSpacingItemDecoration to help solve spacing problem inside RecyclerView
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
/**
* Give equal margin around each [RecyclerView] items, adaptively. Supports known direct subclasses of
@handstandsam
handstandsam / InstallReferrerExt.kt
Created February 28, 2022 20:29
Install Referrer KTX - Kotlin Coroutine friendly wrapper for the Google Play's InstallReferrerClient API. This API is used to ask Google Play about where the installation originated.
import android.content.Context
import android.os.RemoteException
import com.android.installreferrer.api.InstallReferrerClient
import com.android.installreferrer.api.InstallReferrerStateListener
import com.android.installreferrer.api.ReferrerDetails
import kotlinx.coroutines.CompletableDeferred
/**
* https://developer.android.com/google/play/installreferrer/library
*