This commit is contained in:
Ваше Имя
2025-09-24 16:21:42 +04:00
parent 3c9f2eaf15
commit 9008e9b95f
8 changed files with 54 additions and 123 deletions
+3 -6
View File
@@ -1,7 +1,7 @@
plugins { plugins {
alias(libs.plugins.android.application) alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android) alias(libs.plugins.kotlin.android)
id("org.jetbrains.kotlin.kapt") id("com.google.gms.google-services")
} }
android { android {
@@ -47,9 +47,6 @@ dependencies {
androidTestImplementation(libs.androidx.junit) androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core) androidTestImplementation(libs.androidx.espresso.core)
// Room implementation(platform("com.google.firebase:firebase-bom:34.3.0"))
val room_version = "2.8.0" implementation("com.google.firebase:firebase-database")
implementation("androidx.room:room-runtime:$room_version")
kapt("androidx.room:room-compiler:$room_version")
implementation("androidx.room:room-ktx:$room_version")
} }
@@ -1,25 +0,0 @@
package com.example.myapplication
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import android.content.Context
@Database(entities = [NoteEntity::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun noteDao(): NoteDao
companion object {
@Volatile private var INSTANCE: AppDatabase? = null
fun getDatabase(context: Context): AppDatabase {
return INSTANCE ?: synchronized(this) {
Room.databaseBuilder(
context.applicationContext,
AppDatabase::class.java,
"my_database"
).build().also { INSTANCE = it }
}
}
}
}
@@ -3,50 +3,47 @@ package com.example.myapplication
import android.os.Bundle import android.os.Bundle
import android.widget.Button import android.widget.Button
import android.widget.EditText import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
import androidx.lifecycle.lifecycleScope import com.google.firebase.database.FirebaseDatabase
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.launch
class MainActivity : ComponentActivity() { class MainActivity : ComponentActivity() {
private lateinit var etInput: EditText private lateinit var etInput: EditText
private lateinit var btnAdd: Button private lateinit var btnSave: Button
private lateinit var recyclerView: RecyclerView private lateinit var btnLoad: Button
private lateinit var adapter: NoteAdapter private lateinit var tvResult: TextView
private lateinit var db: AppDatabase private val db = FirebaseDatabase.getInstance().reference
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
etInput = findViewById(R.id.etInput) etInput = findViewById(R.id.etInput)
btnAdd = findViewById(R.id.btnAdd) btnSave = findViewById(R.id.btnSave)
recyclerView = findViewById(R.id.recyclerView) btnLoad = findViewById(R.id.btnLoad)
tvResult = findViewById(R.id.tvResult)
adapter = NoteAdapter(emptyList()) // Сохранение данных
recyclerView.layoutManager = LinearLayoutManager(this) btnSave.setOnClickListener {
recyclerView.adapter = adapter
db = AppDatabase.getDatabase(this)
// Добавляем заметку
btnAdd.setOnClickListener {
val text = etInput.text.toString() val text = etInput.text.toString()
if (text.isNotEmpty()) { if (text.isNotEmpty()) {
lifecycleScope.launch { val noteId = db.push().key!! // генерируем уникальный id
db.noteDao().insert(NoteEntity(text = text)) db.child("notes").child(noteId).setValue(text)
} Toast.makeText(this, "Сохранено", Toast.LENGTH_SHORT).show()
etInput.text.clear() etInput.text.clear()
} }
} }
// Подписываемся на изменения // Загрузка данных
lifecycleScope.launch { btnLoad.setOnClickListener {
db.noteDao().getAll().collect { notes -> db.child("notes").get().addOnSuccessListener { snapshot ->
adapter.update(notes) val list = snapshot.children.mapNotNull { it.getValue(String::class.java) }
tvResult.text = list.joinToString("\n")
}.addOnFailureListener {
Toast.makeText(this, "Ошибка загрузки", Toast.LENGTH_SHORT).show()
} }
} }
} }
@@ -1,10 +0,0 @@
package com.example.myapplication
import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity(tableName = "notes")
data class NoteEntity(
@PrimaryKey(autoGenerate = true) val id: Int = 0,
val text: String
)
@@ -1,29 +0,0 @@
package com.example.myapplication
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
class NoteAdapter(private var notes: List<NoteEntity>) :
RecyclerView.Adapter<NoteAdapter.NoteViewHolder>() {
class NoteViewHolder(val textView: TextView) : RecyclerView.ViewHolder(textView)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NoteViewHolder {
val textView = LayoutInflater.from(parent.context)
.inflate(android.R.layout.simple_list_item_1, parent, false) as TextView
return NoteViewHolder(textView)
}
override fun onBindViewHolder(holder: NoteViewHolder, position: Int) {
holder.textView.text = notes[position].text
}
override fun getItemCount() = notes.size
fun update(newNotes: List<NoteEntity>) {
notes = newNotes
notifyDataSetChanged()
}
}
@@ -1,15 +0,0 @@
package com.example.myapplication
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import kotlinx.coroutines.flow.Flow
@Dao
interface NoteDao {
@Insert
suspend fun insert(note: NoteEntity)
@Query("SELECT * FROM notes")
fun getAll(): Flow<List<NoteEntity>>
}
+25 -10
View File
@@ -8,19 +8,34 @@
android:id="@+id/etInput" android:id="@+id/etInput"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:hint="Введите заметку" /> android:hint="Введите текст" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="16dp">
<Button <Button
android:id="@+id/btnAdd" android:id="@+id/btnSave"
android:layout_width="wrap_content" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="12dp" android:layout_weight="1"
android:text="Добавить" /> android:text="Сохранить" />
<androidx.recyclerview.widget.RecyclerView <Button
android:id="@+id/recyclerView" android:id="@+id/btnLoad"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Загрузить" />
</LinearLayout>
<TextView
android:id="@+id/tvResult"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_height="wrap_content"
android:layout_marginTop="16dp" android:layout_marginTop="24dp"
android:layout_weight="1" /> android:text="Здесь появятся данные"
android:textSize="18sp" />
</LinearLayout> </LinearLayout>
+1
View File
@@ -2,4 +2,5 @@
plugins { plugins {
alias(libs.plugins.android.application) apply false alias(libs.plugins.android.application) apply false
alias(libs.plugins.kotlin.android) apply false alias(libs.plugins.kotlin.android) apply false
id("com.google.gms.google-services") version "4.4.3" apply false
} }