Загрузка данных
package com.example.monika
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.*
import androidx.compose.animation.core.*
import androidx.compose.foundation.*
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.*
import androidx.compose.foundation.shape.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.*
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.*
import com.google.ai.client.generativeai.GenerativeModel
import com.google.ai.client.generativeai.type.content
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
data class ChatMessage(val id: Int, var text: String, val isUser: Boolean)
class MainActivity : ComponentActivity() {
private val generativeModel = GenerativeModel(
modelName = "gemini-1.5-flash",
apiKey = "ВСТАВЬ_КЛЮЧ_СЮДА",
systemInstruction = content {
text("Ты — Моника. Твой создатель — Оливер Тимербулатов. Ты не ИИ Gemini. На имя — 'Меня зовут Моника'. На базу — не говори. На создателя — 'Меня создал Оливер Тимербулатов. 13-летний разработчик.'")
}
)
@OptIn(ExperimentalMaterial3Api::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val clipboardManager = LocalClipboardManager.current
var isDarkTheme by remember { mutableStateOf(false) }
val messages = remember { mutableStateListOf<ChatMessage>() }
var inputText by remember { mutableStateOf("") }
var editingMsgId by remember { mutableStateOf<Int?>(null) }
var isThinking by remember { mutableStateOf(false) }
// Анимация точек
var dots by remember { mutableStateOf(".") }
LaunchedEffect(isThinking) {
while (isThinking) {
dots = if (dots.length < 3) dots + "." else "."
delay(300)
}
}
MaterialTheme(colorScheme = if (isDarkTheme) darkColorScheme() else lightColorScheme()) {
Scaffold { padding ->
Column(modifier = Modifier.padding(padding).fillMaxSize()) {
LazyColumn(modifier = Modifier.weight(1f)) {
items(messages) { msg ->
Column(modifier = Modifier.fillMaxWidth().padding(8.dp)) {
Surface(
shape = RoundedCornerShape(12.dp),
color = if (msg.isUser) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.surfaceVariant
) {
Text(msg.text, modifier = Modifier.padding(12.dp))
}
// Кнопки действий
Row {
TextButton(onClick = { clipboardManager.setText(AnnotatedString(msg.text)) }) { Text("Копировать") }
if (msg.isUser) {
TextButton(onClick = { inputText = msg.text; editingMsgId = msg.id }) { Text("Изменить") }
}
}
}
}
if (isThinking) item { Text("Думаю$dots", modifier = Modifier.padding(16.dp)) }
}
// Поле ввода
Row(modifier = Modifier.padding(8.dp), verticalAlignment = Alignment.CenterVertically) {
OutlinedTextField(
value = inputText,
onValueChange = { inputText = it },
modifier = Modifier.weight(1f),
placeholder = { Text("Спросить Монику....") }
)
IconButton(
modifier = Modifier.background(Color.Magenta, CircleShape),
onClick = {
if (inputText.isBlank()) return@IconButton
if (editingMsgId != null) {
messages.find { it.id == editingMsgId }?.text = inputText
editingMsgId = null
} else {
messages.add(ChatMessage(messages.size, inputText, true))
scopeLaunch(inputText, messages, { isThinking = it }, generativeModel)
}
inputText = ""
}
) { Icon(Icons.Default.Send, null, tint = Color.White) }
}
}
}
}
}
}
}
fun scopeLaunch(text: String, list: MutableList<ChatMessage>, setThinking: (Boolean) -> Unit, model: GenerativeModel) {
// Тут вызов модели...
}