|
| 1 | +package org.fptn.vpn.ui.components |
| 2 | + |
| 3 | +import android.app.AlertDialog |
| 4 | +import android.content.Context |
| 5 | +import android.content.Intent |
| 6 | +import android.graphics.Bitmap |
| 7 | +import android.text.Html |
| 8 | +import android.text.method.LinkMovementMethod |
| 9 | +import android.widget.ImageView |
| 10 | +import android.widget.TextView |
| 11 | +import androidx.compose.material3.Icon |
| 12 | +import androidx.compose.material3.NavigationBar |
| 13 | +import androidx.compose.material3.NavigationBarItem |
| 14 | +import androidx.compose.material3.NavigationBarItemDefaults |
| 15 | +import androidx.compose.material3.Text |
| 16 | +import androidx.compose.runtime.Composable |
| 17 | +import androidx.compose.ui.graphics.Color |
| 18 | +import androidx.compose.ui.res.painterResource |
| 19 | +import androidx.compose.ui.res.stringResource |
| 20 | +import com.google.zxing.BarcodeFormat |
| 21 | +import com.google.zxing.MultiFormatWriter |
| 22 | +import org.fptn.vpn.R |
| 23 | +import org.fptn.vpn.ui.theme.Primary |
| 24 | +import org.fptn.vpn.ui.theme.Secondary |
| 25 | +import org.fptn.vpn.ui.theme.White |
| 26 | +import org.fptn.vpn.ui.theme.White10 |
| 27 | + |
| 28 | +enum class BottomNavTab { HOME, SETTINGS, SHARE } |
| 29 | + |
| 30 | +@Composable |
| 31 | +fun BottomNavBar( |
| 32 | + current: BottomNavTab, |
| 33 | + onHome: () -> Unit, |
| 34 | + onSettings: () -> Unit, |
| 35 | + context: Context, |
| 36 | +) { |
| 37 | + NavigationBar( |
| 38 | + containerColor = Primary, |
| 39 | + contentColor = White, |
| 40 | + ) { |
| 41 | + NavigationBarItem( |
| 42 | + selected = current == BottomNavTab.HOME, |
| 43 | + onClick = { if (current != BottomNavTab.HOME) onHome() }, |
| 44 | + icon = { |
| 45 | + Icon( |
| 46 | + painter = painterResource(R.drawable.ic_baseline_home_24), |
| 47 | + contentDescription = stringResource(R.string.menu_home) |
| 48 | + ) |
| 49 | + }, |
| 50 | + label = { Text(stringResource(R.string.menu_home)) }, |
| 51 | + colors = navItemColors(), |
| 52 | + ) |
| 53 | + NavigationBarItem( |
| 54 | + selected = current == BottomNavTab.SETTINGS, |
| 55 | + onClick = { if (current != BottomNavTab.SETTINGS) onSettings() }, |
| 56 | + icon = { |
| 57 | + Icon( |
| 58 | + painter = painterResource(R.drawable.ic_baseline_settings_24), |
| 59 | + contentDescription = stringResource(R.string.menu_settings) |
| 60 | + ) |
| 61 | + }, |
| 62 | + label = { Text(stringResource(R.string.menu_settings)) }, |
| 63 | + colors = navItemColors(), |
| 64 | + ) |
| 65 | + NavigationBarItem( |
| 66 | + selected = false, |
| 67 | + onClick = { showShareDialog(context) }, |
| 68 | + icon = { |
| 69 | + Icon( |
| 70 | + painter = painterResource(R.drawable.ic_baseline_share_24), |
| 71 | + contentDescription = stringResource(R.string.menu_share) |
| 72 | + ) |
| 73 | + }, |
| 74 | + label = { Text(stringResource(R.string.menu_share)) }, |
| 75 | + colors = navItemColors(), |
| 76 | + ) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +@Composable |
| 81 | +private fun navItemColors() = NavigationBarItemDefaults.colors( |
| 82 | + selectedIconColor = Secondary, |
| 83 | + selectedTextColor = Secondary, |
| 84 | + unselectedIconColor = White10, |
| 85 | + unselectedTextColor = White10, |
| 86 | + indicatorColor = Color.Transparent, |
| 87 | +) |
| 88 | + |
| 89 | +private fun showShareDialog(context: Context) { |
| 90 | + val qrBitmap = generateQRCode(context.getString(R.string.play_market_link), 500, 500) |
| 91 | + val inflater = android.view.LayoutInflater.from(context) |
| 92 | + val dialogView = inflater.inflate(R.layout.share_dialog, null) |
| 93 | + |
| 94 | + (dialogView.findViewById<ImageView>(R.id.qr_code_image)).setImageBitmap(qrBitmap) |
| 95 | + val textView = dialogView.findViewById<TextView>(R.id.link_text) |
| 96 | + textView.text = Html.fromHtml(context.getString(R.string.info_message_html), Html.FROM_HTML_MODE_LEGACY) |
| 97 | + textView.movementMethod = LinkMovementMethod.getInstance() |
| 98 | + |
| 99 | + AlertDialog.Builder(context) |
| 100 | + .setView(dialogView) |
| 101 | + .setMessage(R.string.menu_share) |
| 102 | + .setPositiveButton(R.string.share_via_message) { _, _ -> shareViaMessage(context) } |
| 103 | + .setNeutralButton(R.string.close_button_text, null) |
| 104 | + .create() |
| 105 | + .show() |
| 106 | +} |
| 107 | + |
| 108 | +private fun shareViaMessage(context: Context) { |
| 109 | + val shareIntent = Intent(Intent.ACTION_SEND).apply { |
| 110 | + type = "text/plain" |
| 111 | + putExtra(Intent.EXTRA_TEXT, context.getString(R.string.share_message)) |
| 112 | + } |
| 113 | + context.startActivity(Intent.createChooser(shareIntent, context.getString(R.string.share_title))) |
| 114 | +} |
| 115 | + |
| 116 | +private fun generateQRCode(text: String, width: Int, height: Int): Bitmap { |
| 117 | + val bitMatrix = MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height) |
| 118 | + val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) |
| 119 | + for (x in 0 until width) { |
| 120 | + for (y in 0 until height) { |
| 121 | + bitmap.setPixel(x, y, if (bitMatrix[x, y]) 0xFF000000.toInt() else 0xFFFFFFFF.toInt()) |
| 122 | + } |
| 123 | + } |
| 124 | + return bitmap |
| 125 | +} |
0 commit comments