Загрузка данных


package com.example.monika

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.icons.filled.Send
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.KeyboardCapitalization
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.google.ai.client.generativeai.GenerativeModel
import kotlinx.coroutines.launch

data class ChatMessage(val text: String, val isUser: Boolean)

class MainActivity : ComponentActivity() {

    private val modelName = "gemini-pro"
    private val apiKey = "ВСТАВЬ_КЛЮЧ_СЮДА"

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

        setContent {
            var isDarkTheme by remember { mutableStateOf(false) }
            
            MaterialTheme(colorScheme = if (isDarkTheme) darkColorScheme() else lightColorScheme()) {
                val generativeModel = GenerativeModel(modelName, apiKey)
                val scope = rememberCoroutineScope()
                var mainInputText by remember { mutableStateOf("") }
                val messages = remember { mutableStateListOf<ChatMessage>() }
                var isChatStarted by remember { mutableStateOf(false) }

                Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
                    Column(modifier = Modifier.fillMaxSize().padding(16.dp)) {
                        
                        // Топ-бар: [Меню]   Monika   [Шестеренка]
                        Row(
                            modifier = Modifier.fillMaxWidth(),
                            verticalAlignment = Alignment.CenterVertically,
                            horizontalArrangement = Arrangement.SpaceBetween
                        ) {
                            IconButton(onClick = { /* Логика меню */ }) {
                                Icon(Icons.Default.Menu, contentDescription = "Меню")
                            }
                            Text("Monika", fontSize = 20.sp, fontWeight = FontWeight.Bold)
                            IconButton(onClick = { isDarkTheme = !isDarkTheme }) {
                                Icon(Icons.Default.Settings, contentDescription = "Настройки темы")
                            }
                        }

                        if (!isChatStarted) {
                            Spacer(modifier = Modifier.height(100.dp))
                            Text("Здравствуйте!", fontSize = 42.sp, fontWeight = FontWeight.Bold)
                            Text("Начнем общение?", fontSize = 18.sp, fontWeight = FontWeight.W600, color = Color.Gray)
                        }

                        LazyColumn(modifier = Modifier.weight(1f)) {
                            items(messages) { msg ->
                                Box(modifier = Modifier.fillMaxWidth().padding(8.dp), contentAlignment = if (msg.isUser) Alignment.CenterEnd else Alignment.CenterStart) {
                                    Text(
                                        text = msg.text,
                                        modifier = if (msg.isUser) 
                                            Modifier.background(MaterialTheme.colorScheme.primary, RoundedCornerShape(12.dp)).padding(12.dp)
                                        else 
                                            Modifier.padding(4.dp),
                                        // Жирный текст для Моники (чуть-чуть)
                                        fontWeight = if (msg.isUser) FontWeight.Normal else FontWeight.Medium,
                                        color = if (msg.isUser) Color.White else MaterialTheme.colorScheme.onSurface
                                    )
                                }
                            }
                        }

                        // Поле ввода
                        Row(modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
                            IconButton(onClick = { /* Логика фото */ }) {
                                Icon(Icons.Default.Add, contentDescription = "Прикрепить")
                            }
                            OutlinedTextField(
                                value = mainInputText,
                                onValueChange = { mainInputText = it },
                                modifier = Modifier.weight(1f),
                                placeholder = { Text("Спросить Монику....") },
                                shape = RoundedCornerShape(25.dp),
                                keyboardOptions = KeyboardOptions(capitalization = KeyboardCapitalization.Sentences, autoCorrect = true),
                                singleLine = true
                            )
                            IconButton(onClick = {
                                if (mainInputText.isNotBlank()) {
                                    isChatStarted = true
                                    val userText = mainInputText
                                    messages.add(ChatMessage(userText, true))
                                    mainInputText = ""
                                    
                                    scope.launch {
                                        try {
                                            val response = generativeModel.generateContent(userText)
                                            messages.add(ChatMessage(response.text ?: "...", false))
                                        } catch (e: Exception) {
                                            messages.add(ChatMessage("Ой, что-то сломалось! Но ничего, Оливер уже всё исправляет!", false))
                                        }
                                    }
                                }
                            }) {
                                Icon(Icons.Default.Send, contentDescription = "Отправить")
                            }
                        }
                    }
                }
            }
        }
    }
}