Skip to content

Instantly share code, notes, and snippets.

View labibmuhajir's full-sized avatar

Labib Muhajir labibmuhajir

View GitHub Profile
@labibmuhajir
labibmuhajir / DefaultLinkMovementMethod.java
Created January 17, 2024 08:45 — forked from alexzaitsev/DefaultLinkMovementMethod.java
How to display HTML using Android Compose
/**
* Set this on a textview and then you can potentially open links locally if applicable
*/
public class DefaultLinkMovementMethod extends LinkMovementMethod {
private OnLinkClickedListener mOnLinkClickedListener;
public DefaultLinkMovementMethod(OnLinkClickedListener onLinkClickedListener) {
mOnLinkClickedListener = onLinkClickedListener;
}
@labibmuhajir
labibmuhajir / Compose.kt
Created October 19, 2023 08:28
compose in xml xml in compose
//If you want to use a Compose in your XML file, you can add this to your layout file:
<androidx.compose.ui.platform.ComposeView
android:id="@+id/my_composable"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
//and then, set the content:
@labibmuhajir
labibmuhajir / .bash_profile
Created April 11, 2023 06:50
.bash_profile
export NVM_DIR=~/.nvm
export PATH=~/Library/Android/sdk/tools:$PATH
export PATH=~/Library/Android/sdk/platform-tools:$PATH
export PATH=/Library/flutter/bin:$PATH
source $(brew --prefix nvm)/nvm.sh
@labibmuhajir
labibmuhajir / .htaccess
Created March 23, 2023 04:48
htaccess for react
RewriteBase /
RewriteCond %{REQUEST_URI} !^/(assets/?|$)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
@labibmuhajir
labibmuhajir / Sha256.kt
Created October 13, 2022 08:12
sha-256
fun String.getSha256(): String {
val digest = MessageDigest.getInstance("SHA-256").apply { reset() }
val byteData: ByteArray = digest.digest(toByteArray())
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Base64.getEncoder().encodeToString(byteData)
} else {
String(
android.util.Base64.encode(byteData, android.util.Base64.DEFAULT),
StandardCharsets.UTF_8
)
@labibmuhajir
labibmuhajir / storage.kt
Last active January 14, 2022 02:27
storage
val path = ContextCompat.getExternalFilesDirs(requireContext(), null).first()
val bytesAvailable = path.freeSpace
val bytesSize = path.totalSpace
fun formatSize(size: Long): String? {
var size = size
var suffix: String? = null
if (size >= 1024) {
suffix = " KB"
size /= 1024
@labibmuhajir
labibmuhajir / JsonWorker.kt
Last active December 1, 2021 08:14
read big json file
import android.content.Context
import android.net.Uri
import androidx.hilt.work.HiltWorker
import androidx.work.*
import com.google.gson.Gson
import com.google.gson.stream.JsonReader
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@labibmuhajir
labibmuhajir / Gson.kt
Created December 1, 2021 03:11
convert json to object with Gson
inline fun <reified T> String.jsonToObject(): T {
val gson = GsonBuilder().create()
return gson.fromJson(this, object: TypeToken<T>(){}.type)
}
@labibmuhajir
labibmuhajir / Retrofit.kt
Created November 11, 2021 07:45
retrofit success data or throw
inline fun <reified T : Any> Response<*>.bodyOrThrow(): T {
if (isSuccessful && (code() == 200 || code() == 201)) return body() as T
else throw Exception() //or with custom exception
}
@labibmuhajir
labibmuhajir / NetworkMonitor.kt
Last active September 1, 2021 04:18
android network monitor
class NetworkMonitor(context: Context) : ConnectivityManager.NetworkCallback() {
private val weakContext = WeakReference(context)
private val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
private val networkRequest = NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET)
.build()