lab6
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
plugins {
|
||||
alias(libs.plugins.android.application)
|
||||
alias(libs.plugins.kotlin.android)
|
||||
id("org.jetbrains.kotlin.kapt")
|
||||
}
|
||||
|
||||
android {
|
||||
@@ -45,4 +46,10 @@ dependencies {
|
||||
testImplementation(libs.junit)
|
||||
androidTestImplementation(libs.androidx.junit)
|
||||
androidTestImplementation(libs.androidx.espresso.core)
|
||||
|
||||
// Room
|
||||
val room_version = "2.8.0"
|
||||
implementation("androidx.room:room-runtime:$room_version")
|
||||
kapt("androidx.room:room-compiler:$room_version")
|
||||
implementation("androidx.room:room-ktx:$room_version")
|
||||
}
|
||||
@@ -2,9 +2,6 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<uses-permission android:name="android.permission.CAMERA" />
|
||||
<uses-feature android:name="android.hardware.camera" android:required="false" />
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
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 }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,57 +1,52 @@
|
||||
package com.example.myapplication
|
||||
|
||||
import android.Manifest
|
||||
import android.content.pm.PackageManager
|
||||
import android.graphics.Bitmap
|
||||
import android.os.Bundle
|
||||
import android.widget.Button
|
||||
import android.widget.ImageView
|
||||
import android.widget.Toast
|
||||
import android.widget.EditText
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
|
||||
private lateinit var btnCamera: Button
|
||||
private lateinit var imageView: ImageView
|
||||
private lateinit var etInput: EditText
|
||||
private lateinit var btnAdd: Button
|
||||
private lateinit var recyclerView: RecyclerView
|
||||
private lateinit var adapter: NoteAdapter
|
||||
|
||||
// Новый контракт
|
||||
private val takePicturePreview = registerForActivityResult(
|
||||
ActivityResultContracts.TakePicturePreview()
|
||||
) { bitmap: Bitmap? ->
|
||||
if (bitmap != null) {
|
||||
imageView.setImageBitmap(bitmap)
|
||||
} else {
|
||||
Toast.makeText(this, "Фото не получено", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
|
||||
// Запрос разрешения на камеру
|
||||
private val requestCameraPermission = registerForActivityResult(
|
||||
ActivityResultContracts.RequestPermission()
|
||||
) { granted ->
|
||||
if (granted) {
|
||||
takePicturePreview.launch(null)
|
||||
} else {
|
||||
Toast.makeText(this, "Разрешение на камеру не выдано", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
private lateinit var db: AppDatabase
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main)
|
||||
|
||||
btnCamera = findViewById(R.id.btnCamera)
|
||||
imageView = findViewById(R.id.imageView)
|
||||
etInput = findViewById(R.id.etInput)
|
||||
btnAdd = findViewById(R.id.btnAdd)
|
||||
recyclerView = findViewById(R.id.recyclerView)
|
||||
|
||||
btnCamera.setOnClickListener {
|
||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
|
||||
== PackageManager.PERMISSION_GRANTED
|
||||
) {
|
||||
takePicturePreview.launch(null)
|
||||
} else {
|
||||
requestCameraPermission.launch(Manifest.permission.CAMERA)
|
||||
adapter = NoteAdapter(emptyList())
|
||||
recyclerView.layoutManager = LinearLayoutManager(this)
|
||||
recyclerView.adapter = adapter
|
||||
|
||||
db = AppDatabase.getDatabase(this)
|
||||
|
||||
// Добавляем заметку
|
||||
btnAdd.setOnClickListener {
|
||||
val text = etInput.text.toString()
|
||||
if (text.isNotEmpty()) {
|
||||
lifecycleScope.launch {
|
||||
db.noteDao().insert(NoteEntity(text = text))
|
||||
}
|
||||
etInput.text.clear()
|
||||
}
|
||||
}
|
||||
|
||||
// Подписываемся на изменения
|
||||
lifecycleScope.launch {
|
||||
db.noteDao().getAll().collect { notes ->
|
||||
adapter.update(notes)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
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
|
||||
)
|
||||
@@ -0,0 +1,29 @@
|
||||
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()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
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>>
|
||||
}
|
||||
@@ -4,17 +4,23 @@
|
||||
android:orientation="vertical"
|
||||
android:padding="16dp">
|
||||
|
||||
<EditText
|
||||
android:id="@+id/etInput"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="Введите заметку" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/btnCamera"
|
||||
android:id="@+id/btnAdd"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Сделать фото" />
|
||||
android:layout_marginTop="12dp"
|
||||
android:text="Добавить" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/imageView"
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/recyclerView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1"
|
||||
android:layout_marginTop="16dp"
|
||||
android:scaleType="centerCrop" />
|
||||
android:layout_weight="1" />
|
||||
</LinearLayout>
|
||||
Reference in New Issue
Block a user