Skip to content

Commit

Permalink
šŸ› fix(MainActivity, StringExt): Improve code style and add markdown sā€¦
Browse files Browse the repository at this point in the history
ā€¦tripping
  • Loading branch information
maxrave-dev committed Jan 19, 2025
1 parent 1a1595c commit 1818d87
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 29 deletions.
37 changes: 19 additions & 18 deletions app/src/main/java/com/maxrave/simpmusic/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import androidx.media3.common.util.UnstableApi
import androidx.navigation.fragment.findNavController
import androidx.navigation.ui.setupWithNavController
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.maxrave.kotlinytmusicscraper.extension.stripMarkdown
import com.maxrave.simpmusic.R
import com.maxrave.simpmusic.common.Config
import com.maxrave.simpmusic.common.FIRST_TIME_MIGRATION
Expand Down Expand Up @@ -173,10 +174,10 @@ class MainActivity : AppCompatActivity() {
// WindowCompat.setDecorFitsSystemWindows(window, false)
enableEdgeToEdge(
navigationBarStyle =
SystemBarStyle.auto(
lightScrim = Color.Transparent.toArgb(),
darkScrim = Color.Transparent.toArgb(),
),
SystemBarStyle.auto(
lightScrim = Color.Transparent.toArgb(),
darkScrim = Color.Transparent.toArgb(),
),
)
viewModel.checkIsRestoring()
viewModel.runWorker()
Expand Down Expand Up @@ -216,15 +217,15 @@ class MainActivity : AppCompatActivity() {
binding.miniplayer.visibility = View.GONE
}
binding.root.addOnLayoutChangeListener {
_,
left,
top,
right,
bottom,
oldLeft,
oldTop,
oldRight,
oldBottom,
_,
left,
top,
right,
bottom,
oldLeft,
oldTop,
oldRight,
oldBottom,
->
val rect = Rect(left, top, right, bottom)
val oldRect = Rect(oldLeft, oldTop, oldRight, oldBottom)
Expand Down Expand Up @@ -294,7 +295,7 @@ class MainActivity : AppCompatActivity() {

R.id.bottom_navigation_item_library,
R.id.favoriteFragment, R.id.localPlaylistFragment,
-> {
-> {
binding.bottomNavigationView.menu
.findItem(
R.id.bottom_navigation_item_library,
Expand All @@ -307,7 +308,7 @@ class MainActivity : AppCompatActivity() {
when (currentBackStack) {
R.id.bottom_navigation_item_library,
R.id.favoriteFragment, R.id.localPlaylistFragment,
-> {
-> {
binding.bottomNavigationView.menu
.findItem(
R.id.bottom_navigation_item_library,
Expand Down Expand Up @@ -346,7 +347,7 @@ class MainActivity : AppCompatActivity() {
"fragment_log_in",
"MusixmatchFragment",
)
).contains(destination.label)
).contains(destination.label)
) {
lifecycleScope.launch { viewModel.showOrHideMiniplayer.emit(false) }
Log.w("MainActivity", "onCreate: HIDE MINIPLAYER")
Expand Down Expand Up @@ -546,7 +547,7 @@ class MainActivity : AppCompatActivity() {
"fragment_log_in",
"MusixmatchFragment",
)
).contains(navController.currentDestination?.label) &&
).contains(navController.currentDestination?.label) &&
it.nowPlayingTitle.isNotEmpty() &&
binding.miniplayer.visibility != View.VISIBLE
) {
Expand Down Expand Up @@ -709,7 +710,7 @@ class MainActivity : AppCompatActivity() {
R.string.update_message,
response.tagName,
formatted,
response.body,
stripMarkdown(response.body ?: ""),
),
).setPositiveButton(getString(R.string.download)) { _, _ ->
val browserIntent =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,58 @@ fun String.isTwoLetterCode(): Boolean {

fun isValidProxyHost(host: String): Boolean {
// Regular expression to validate proxy host (without port)
val proxyHostRegex = Regex(
pattern = "^(?!-)[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(?<!-)\$",
options = setOf(RegexOption.IGNORE_CASE)
)
val proxyHostRegex =
Regex(
pattern = "^(?!-)[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(?<!-)\$",
options = setOf(RegexOption.IGNORE_CASE),
)

// Return true if the host matches the regex or is an IP address
return proxyHostRegex.matches(host) || isIPAddress(host)
}

private fun isIPAddress(host: String): Boolean {
// Check if the host is an IPv4 address
val ipv4Regex = Regex(
pattern = "^([0-9]{1,3}\\.){3}[0-9]{1,3}\$"
)
val ipv4Regex =
Regex(
pattern = "^([0-9]{1,3}\\.){3}[0-9]{1,3}\$",
)
if (ipv4Regex.matches(host)) {
return host.split('.').all { it.toInt() in 0..255 }
}

// Check if the host is an IPv6 address
val ipv6Regex = Regex(
pattern = "^[0-9a-fA-F:]+$"
)
val ipv6Regex =
Regex(
pattern = "^[0-9a-fA-F:]+$",
)
return ipv6Regex.matches(host)
}
}

fun stripMarkdown(markdown: String): String =
markdown
// Remove headings (e.g., # Heading)
.replace(Regex("""(?m)^#{1,6}\s*"""), "")
// Remove bold (**text**)
.replace(Regex("""\*\*(.*?)\*\*"""), "$1")
// Remove italic (*text*)
.replace(Regex("""\*(.*?)\*"""), "$1")
// Remove strikethrough (~~text~~)
.replace(Regex("""~~(.*?)~~"""), "$1")
// Remove inline code (`code`)
.replace(Regex("""`([^`]*)`"""), "$1")
// Remove images (![alt](url))
.replace(Regex("""!$\begin:math:display$.*?$\end:math:display$$\begin:math:text$.*?$\end:math:text$"""), "")
// Remove links ([text](url))
.replace(Regex("""\[.*?]$\begin:math:text$.*?$\end:math:text$"""), "")
// Remove horizontal rules (---, ***, ___)
.replace(Regex("""(?m)^\s*[-*_]{3,}\s*$"""), "\n")
// Remove unordered list markers (- item, * item)
.replace(Regex("""(?m)^\s*[-*+]\s+"""), " - ")
// Remove ordered list markers (1. item, 2. item)
.replace(Regex("""(?m)^\s*\d+\.\s+"""), " - ")
// Replace multiple newlines with a single newline
.replace(Regex("""\n{2,}"""), "\n\n")
// Trim each line
.lines()
.joinToString("\n") { it.trim() }

0 comments on commit 1818d87

Please sign in to comment.