Can you rewrite this kotlin code

package com.example.talkapp

import android.content.Context
import android.speech.tts.TextToSpeech
import android.speech.tts.TextToSpeech.OnInitListener
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.*
import androidx.compose.material3.Text
import androidx.compose.material3.Button
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.text.TextAlign
import androidx.compose.ui.platform.LocalContext
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import androidx.compose.material3.Icon

class MainActivity : ComponentActivity() {

    private lateinit var tts: TextToSpeech

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        tts = TextToSpeech(this, OnInitListener { status ->
            if (status == TextToSpeech.SUCCESS) {
                // Initialize TTS successfully
            } else {
                Toast.makeText(this, "Text to Speech Initialization Failed", Toast.LENGTH_SHORT).show()
            }
        })

        setContent {
            TalkApp(tts)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        tts.stop()
        tts.shutdown()
    }
}

@Composable
fun TalkApp(tts: TextToSpeech) {
    val sharedPreferences = LocalContext.current.getSharedPreferences("TalkApp", Context.MODE_PRIVATE)
    val gson = Gson()

    var phrases by remember { mutableStateOf(loadPhrases(sharedPreferences, gson)) }
    var selectedIndex by remember { mutableStateOf(0) }
    var isMenuOpen by remember { mutableStateOf(false) }

    Scaffold(
        topBar = {
            TopAppBar(
                title = { Text("TalkApp") },
                navigationIcon = {
                    IconButton(onClick = { isMenuOpen = !isMenuOpen }) {
                        Icon(
                            painter = painterResource(id = R.drawable.ic_menu), // Ensure this resource exists
                            contentDescription = null
                        )
                    }
                }
            )
        }
    ) { paddingValues ->
        Row(
            modifier = Modifier.fillMaxSize(),
            horizontalArrangement = Arrangement.Start
        ) {
            if (isMenuOpen) {
                Column(
                    modifier = Modifier
                        .fillMaxHeight()
                        .width(250.dp)
                        .padding(8.dp)
                        .background(Color.LightGray)
                ) {
                    Text(
                        text = "Menü",
                        fontSize = 24.sp,
                        fontWeight = FontWeight.Bold,
                        modifier = Modifier.padding(16.dp)
                    )
                    Button(onClick = {
                        openAddPhraseDialog(
                            onAddPhrase = { newPhrase ->
                                phrases = phrases + newPhrase
                                savePhrases(sharedPreferences, gson, phrases)
                            }
                        )
                    }) {
                        Text("Yeni Kelime Ekle")
                    }
                }
            }

            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(paddingValues),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center
            ) {
                Text(
                    text = phrases[selectedIndex],
                    fontSize = 36.sp,
                    fontWeight = FontWeight.Bold,
                    textAlign = TextAlign.Center,
                    color = Color.Black,
                    modifier = Modifier.padding(bottom = 32.dp)
                )

                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.SpaceEvenly
                ) {
                    Button(
                        onClick = {
                            if (selectedIndex > 0) selectedIndex -= 1
                        },
                        modifier = Modifier.size(100.dp),
                        shape = CircleShape,
                        colors = ButtonDefaults.buttonColors(containerColor = Color.Gray)
                    ) {
                        Text("←", fontSize = 40.sp, fontWeight = FontWeight.Bold, color = Color.White)
                    }

                    Button(
                        onClick = { tts.speak(phrases[selectedIndex], TextToSpeech.QUEUE_FLUSH, null, null) },
                        modifier = Modifier.size(120.dp),
                        shape = CircleShape,
                        colors = ButtonDefaults.buttonColors(containerColor = Color.Red)
                    ) {
                        Text("Onayla", fontSize = 20.sp, fontWeight = FontWeight.Bold, color = Color.White)
                    }

                    Button(
                        onClick = {
                            if (selectedIndex < phrases.size - 1) selectedIndex += 1
                        },
                        modifier = Modifier.size(100.dp),
                        shape = CircleShape,
                        colors = ButtonDefaults.buttonColors(containerColor = Color.Gray)
                    ) {
                        Text("→", fontSize = 40.sp, fontWeight = FontWeight.Bold, color = Color.White)
                    }
                }
            }
        }
    }
}

@Composable
fun openAddPhraseDialog(onAddPhrase: (String) -> Unit) {
    var newPhrase by remember { mutableStateOf("") }

    AlertDialog(
        onDismissRequest = { /* Dismiss the dialog */ },
        title = { Text("Yeni Kelime Ekle") },
        text = {
            TextField(
                value = newPhrase,
                onValueChange = { newPhrase = it },
                label = { Text("Yeni Kelime") }
            )
        },
        confirmButton = {
            Button(onClick = {
                onAddPhrase(newPhrase)
            }) {
                Text("Ekle")
            }
        },
        dismissButton = {
            Button(onClick = { /* Close the dialog */ }) {
                Text("İptal")
            }
        }
    )
}

private fun loadPhrases(sharedPreferences: SharedPreferences, gson: Gson): List<String> {
    val json = sharedPreferences.getString("phrases", null)
    return if (json.isNullOrEmpty()) {
        listOf("Acıktım", "Susadım", "Başım ağrıyor", "Kendimi iyi hissetmiyorum", "Uykum geldi")
    } else {
        val type = object : TypeToken<List<String>>() {}.type
        gson.fromJson(json, type)
    }
}

private fun savePhrases(sharedPreferences: SharedPreferences, gson: Gson, phrases: List<String>) {
    sharedPreferences.edit().apply {
        putString("phrases", gson.toJson(phrases))
    }.apply()
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    TalkApp(TextToSpeech(LocalContext.current, null)) // Preview with mock context
}