04 grok

 //Grok

// MainActivity.kt

package com.example.textcontextapp


import android.content.ClipData

import android.content.ClipboardManager

import android.content.Context

import android.content.Intent

import android.os.Bundle

import androidx.activity.ComponentActivity

import androidx.activity.compose.setContent

import androidx.compose.foundation.ExperimentalFoundationApi

import androidx.compose.foundation.combinedClickable

import androidx.compose.foundation.layout.*

import androidx.compose.foundation.lazy.LazyColumn

import androidx.compose.foundation.lazy.items

import androidx.compose.material3.*

import androidx.compose.runtime.*

import androidx.compose.ui.Alignment

import androidx.compose.ui.Modifier

import androidx.compose.ui.platform.LocalContext

import androidx.compose.ui.text.font.FontWeight

import androidx.compose.ui.unit.dp

import androidx.compose.ui.unit.sp

import androidx.compose.ui.window.PopupProperties


class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        setContent {

            MaterialTheme {

                Surface(modifier = Modifier.fillMaxSize()) {

                    TextContextApp()

                }

            }

        }

    }

}


@OptIn(ExperimentalFoundationApi::class)

@Composable

fun TextContextApp() {

    val context = LocalContext.current

    var selectedText by remember { mutableStateOf("") }

    var showMenu by remember { mutableStateOf(false) }

    var menuPosition by remember { mutableStateOf(Offset.Zero) }


    val sampleTexts = listOf(

        "Jetpack Compose is a modern toolkit for building native Android UI.",

        "It simplifies and accelerates UI development on Android with less code.",

        "Kotlin is the preferred language for Android development.",

        "Context menus provide quick access to common actions like copy and share.",

        "Selection and interaction are key elements of great mobile user experience."

    )


    Scaffold(

        topBar = {

            TopAppBar(

                title = { Text("Text Context Menu") },

                colors = TopAppBarDefaults.topAppBarColors(

                    containerColor = MaterialTheme.colorScheme.primaryContainer

                )

            )

        }

    ) { paddingValues ->

        LazyColumn(

            modifier = Modifier

                .fillMaxSize()

                .padding(paddingValues)

                .padding(16.dp),

            verticalArrangement = Arrangement.spacedBy(12.dp)

        ) {

            items(sampleTexts) { text ->

                Card(

                    modifier = Modifier

                        .fillMaxWidth()

                        .combinedClickable(

                            onClick = { /* Optional: show full text */ },

                            onLongClick = {

                                selectedText = text

                                showMenu = true

                            }

                        ),

                    elevation = CardDefaults.cardElevation(defaultElevation = 4.dp)

                ) {

                    Text(

                        text = text,

                        modifier = Modifier.padding(16.dp),

                        fontSize = 16.sp,

                        lineHeight = 24.sp

                    )

                }

            }

        }

    }


    // Context Menu

    if (showMenu && selectedText.isNotEmpty()) {

        Box(

            modifier = Modifier.fillMaxSize(),

            contentAlignment = Alignment.TopStart

        ) {

            DropdownMenu(

                expanded = showMenu,

                onDismissRequest = { showMenu = false },

                properties = PopupProperties(focusable = true),

                modifier = Modifier.padding(8.dp)

            ) {

                DropdownMenuItem(

                    text = { Text("Copy") },

                    leadingIcon = { Icon(Icons.Default.ContentCopy, contentDescription = null) },

                    onClick = {

                        copyToClipboard(context, selectedText)

                        showMenu = false

                    }

                )

                DropdownMenuItem(

                    text = { Text("Copy All") },

                    leadingIcon = { Icon(Icons.Default.ContentCopy, contentDescription = null) },

                    onClick = {

                        val allText = sampleTexts.joinToString("\n\n")

                        copyToClipboard(context, allText)

                        showMenu = false

                    }

                )

                DropdownMenuItem(

                    text = { Text("Share") },

                    leadingIcon = { Icon(Icons.Default.Share, contentDescription = null) },

                    onClick = {

                        shareText(context, selectedText)

                        showMenu = false

                    }

                )

                DropdownMenuItem(

                    text = { Text("Translate") },

                    leadingIcon = { Icon(Icons.Default.Translate, contentDescription = null) },

                    onClick = {

                        showTranslateDialog(context, selectedText)

                        showMenu = false

                    }

                )

            }

        }

    }

}


private fun copyToClipboard(context: Context, text: String) {

    val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager

    val clip = ClipData.newPlainText("Copied Text", text)

    clipboard.setPrimaryClip(clip)

    

    // Simple toast

    android.widget.Toast.makeText(context, "Copied to clipboard", android.widget.Toast.LENGTH_SHORT).show()

}


private fun shareText(context: Context, text: String) {

    val intent = Intent().apply {

        action = Intent.ACTION_SEND

        putExtra(Intent.EXTRA_TEXT, text)

        type = "text/plain"

    }

    context.startActivity(Intent.createChooser(intent, "Share via"))

}


@Composable

private fun showTranslateDialog(context: Context, text: String) {

    var showDialog by remember { mutableStateOf(true) }

    val translatedText = "Translated: $text" // Mock translation (replace with real API later)


    if (showDialog) {

        AlertDialog(

            onDismissRequest = { showDialog = false },

            title = { Text("Translation") },

            text = {

                Text(

                    text = translatedText,

                    fontSize = 16.sp,

                    lineHeight = 24.sp

                )

            },

            confirmButton = {

                TextButton(onClick = { showDialog = false }) {

                    Text("Close")

                }

            }

        )

    }

}


// Import statements (add these at the top)

import androidx.compose.material.icons.Icons

import androidx.compose.material.icons.filled.ContentCopy

import androidx.compose.material.icons.filled.Share

import androidx.compose.material.icons.filled.Translate

import androidx.compose.ui.geometry.Offset