Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion app/src/main/java/com/example/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ class MainActivity : ComponentActivity() {
private fun handleIntent(intent: android.content.Intent?, viewModel: QRViewModel) {
intent?.dataString?.let { dataStr ->
if (dataStr.contains("sayanthrock.github.io/Rock-QR-Code")) {
viewModel.importFromDeepLink(dataStr)
if (dataStr.contains("/redirect?code=local_")) {
viewModel.resolveDynamicRedirect(dataStr)
} else {
viewModel.importFromDeepLink(dataStr)
Comment thread
SayanthRock marked this conversation as resolved.
}
intent.data = null
}
}
Expand Down
52 changes: 51 additions & 1 deletion app/src/main/java/com/example/viewmodel/QRViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,27 @@ class QRViewModel(application: Application) : AndroidViewModel(application) {
}.sortedByDescending { it.timestamp }
}

fun saveScannedResult(content: String) {
fun saveScannedResult(rawContent: String) {
viewModelScope.launch(Dispatchers.IO) {
var content = rawContent

// Resolve local dynamic redirect codes instantly!
if (content.contains("sayanthrock.github.io/Rock-QR-Code/redirect?code=local_")) {
try {
val uri = android.net.Uri.parse(content)
val code = uri.getQueryParameter("code")
if (code != null) {
val currentRecords = repository.allHistory.first()
val record = currentRecords.find { it.shortCode == code }
if (record != null) {
content = record.content // swap redirect link for real target!
Comment thread
SayanthRock marked this conversation as resolved.
}
}
} catch(e: Exception) {
// Ignore
}
}

val type = detectQrType(content)
val title = when (type) {
"URL" -> {
Expand All @@ -349,6 +368,37 @@ class QRViewModel(application: Application) : AndroidViewModel(application) {
}
}

fun resolveDynamicRedirect(url: String) {
viewModelScope.launch(Dispatchers.IO) {
try {
val uri = android.net.Uri.parse(url)
val code = uri.getQueryParameter("code")
if (code != null && code.startsWith("local_")) {
val currentRecords = repository.allHistory.first()
val record = currentRecords.find { it.shortCode == code }
if (record != null) {
val targetUrl = record.content
Comment thread
SayanthRock marked this conversation as resolved.

kotlinx.coroutines.withContext(Dispatchers.Main) {
showToast("Resolved local dynamic link", CustomToastType.SUCCESS)
val intent = android.content.Intent(android.content.Intent.ACTION_VIEW, android.net.Uri.parse(targetUrl))
intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK)
getApplication<android.app.Application>().startActivity(intent)
}

saveScannedResult(targetUrl)
} else {
kotlinx.coroutines.withContext(Dispatchers.Main) {
showToast("Offline link not found on this device", CustomToastType.ERROR)
}
}
}
} catch (e: Exception) {
// Ignore
}
}
}

private fun detectQrType(content: String): String {
return when {
content.startsWith("http://", ignoreCase = true) || content.startsWith("https://", ignoreCase = true) -> "URL"
Expand Down
Loading