Skip to content

Instantly share code, notes, and snippets.

View root-ansh's full-sized avatar
🔍
curiously exploring problems and their solutions

Ansh Sachdeva root-ansh

🔍
curiously exploring problems and their solutions
View GitHub Profile
private val saveFileToSystemSAF: ActivityResultLauncher<String> = registerForActivityResult(CreateDocument(mimeType = "*/*")) { result -> // or mimeType = "application/pdf" / "image/*"
val uri = result
val file = state.fileToBeSaved // the file/bitmap that needs to be writteen to user selected uri
val context = this
if (uri!=null && file!=null){
lifecycleScope.launch {
context.saveFileToUserSelectedPath(uri,file) // writing file to user selected uri
Snackbar
.make(binding.root,"Saved file :${file.name} to user selected directory", Snackbar.LENGTH_SHORT)
.setAction("show"){context.openSystemViewerForSAFUri(uri)} //check next section for info on this
data class FileInfo(
val nameWithExtension: String,
val extension: String?,
val mimeType: String?,
val size: Long?,
val sizeFormatted: String?,
val createdOn: Long?,
val modifiedOn: Long?,
val originalLocationPath: String?
){
// converts any uri(content/file) to a local temp file first in cache directory for easy read/write
suspend fun Uri.toLocalFile(context: Context,localFileLocation:File = context.cacheDir): File? {
val uri = this
return withContext(Dispatchers.IO) {
try {
val fileInfo = FileInfo.fromSAFUri(context,uri)
val fileName = fileInfo?.nameWithExtension?: "${System.currentTimeMillis()}.bin"
val tempFile = File(localFileLocation, fileName)
context.contentResolver.openInputStream(uri)?.use { inputStream ->
FileOutputStream(tempFile).use { outputStream ->
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
type = "image/*" //"application/pdf" // "*/*"
putExtra(Intent.EXTRA_MIME_TYPES, arrayOf("image/*")) // required in some android versions. also good if you set type as */* and want to filter mimetypes
addCategory(Intent.CATEGORY_OPENABLE)
addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION)// optional for our app, but required if you intend to save the SAF content uris(temporary by nature) in databases
}
getFileFromSystem.launch(intent)
private val getFileFromSystem: ActivityResultLauncher<Intent> = registerForActivityResult(StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK) {
val uri = result.data?.data
lifecycleScope.launch {
val file = uri?.toLocalFile(this@XMLActivity) // convert to local file
updateUi()// handle local file
}
}
@root-ansh
root-ansh / NSVPaginationListener.kt
Last active March 14, 2024 19:29
Pagination on a nested scroll view!
import android.view.View
import androidx.core.widget.NestedScrollView
import timber.log.Timber
/**
* when a recycler view can be in nested scroll view, its ability to recycle views is lost as its
* linear layout manager loads all the views at one go. and since all views are loaded at once,
* the layout manager or (recycelr view) don't fire the last child callbacks properly.
*
* For pagination, the basic principle is:
apply plugin: "io.gitlab.arturbosch.detekt" // detekt STEP1 applying plugin (detekt = kotlin code's static analyser)
apply plugin: "checkstyle" // checkstyle STEP1 applying plugin (checkstyle = java code's static analyser)
// detekt STEP2 plugin configuration
detekt {
parallel = true
allRules = true
ignoreFailures = true
debug = true
}
@root-ansh
root-ansh / basic.yaml
Created March 11, 2022 19:07
static code analysers
# .github/workflows/basic.yaml
- name: CodeAnalysis via detekt
run: ./gradlew detekt
- name: Upload detekt results
uses: actions/upload-artifact@v2
with:
name: detekt_results
path:app/build/reports/detekt
@root-ansh
root-ansh / build.gradle
Created August 16, 2020 18:07
Securing keys-Medium
// app/build.gradle
plugins {
...
}
android {
...
defaultConfig {
@root-ansh
root-ansh / installingLAMP_20_4_lts.md
Created May 3, 2020 01:39
Installing lamp on Ubuntu 20.4 LTS

(A little complex article, but works very fine after installing LA of LAMP : https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-ubuntu-18-04)
(Easier article : https://www.tecmint.com/install-lamp-with-phpmyadmin-in-ubuntu-18-04/)

Setting up the L:Linux

we are assuming you are on a linux machine. Although it would be good,if you have an ssh with you and have set up the machine to act as server with one of the firewalls.
More details Here

Setting up the A: Apache