Room + Hilt + Zxing | #1

Зависимости в build.gradle (Project):

id ("com.google.dagger.hilt.android") version ("2.46.1") apply false

Зависимости в build.gradle (App) plugins:

id("com.google.dagger.hilt.android")
id("kotlin-kapt")

Зависимости в build.gradle (App) dependencies:

implementation("com.google.dagger:hilt-android:2.46.1")
kapt("com.google.dagger:hilt-compiler:2.46.1")
implementation("androidx.room:room-ktx:2.6.1")
kapt("androidx.room:room-compiler:2.6.1")
implementation ("com.journeyapps:zxing-android-embedded:4.3.0")

Module:

import android.app.Application
import androidx.room.Room
import com.neco_desarrollo.qrcodescanerjetpack.data.MainDb
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object Module {

@Provides
@Singleton
fun provideMainDb(app: Application) : MainDb{
return Room.databaseBuilder(
app,
MainDb::class.java,
"products.db"
).build()
}
}

MainDb:

import androidx.room.Database
import androidx.room.RoomDatabase

@Database(
entities = [Product::class],
version = 1
)
abstract class MainDb : RoomDatabase() {
abstract val dao: Dao
}

Dao:

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import kotlinx.coroutines.flow.Flow

@Dao
interface Dao {
@Insert
suspend fun insertProduct(product: Product)

@Query("SELECT * FROM products")
fun getAllProducts() : Flow<List<Product>>
}

Product:

import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "products")
data class Product(
@PrimaryKey(autoGenerate = true)
val id: Int? = null,
val name: String,
val numberQR: String
)

MainActivity:


import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.journeyapps.barcodescanner.ScanContract
import com.journeyapps.barcodescanner.ScanOptions
import com.neco_desarrollo.qrcodescanerjetpack.data.MainDb
import com.neco_desarrollo.qrcodescanerjetpack.data.Product
import com.neco_desarrollo.qrcodescanerjetpack.ui.theme.QRcodeScanerJetpackTheme
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import javax.inject.Inject

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
@Inject
lateinit var mainDb: MainDb
var counter = 0

private val scanLauncher = registerForActivityResult(
ScanContract()
) { result ->
if (result.contents == null) {

} else {
Toast.makeText(
this,
"Scan data: ${result.contents}", Toast.LENGTH_SHORT
).show()
}
}


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

val productStateList = mainDb.dao.getAllProducts()
.collectAsState(initial = emptyList())
val coroutineScope = rememberCoroutineScope()

QRcodeScanerJetpackTheme {
Column(
Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally
) {
LazyColumn(
modifier = Modifier
.padding(top = 15.dp)
.fillMaxWidth()
.fillMaxHeight(0.9f),
) {
items(productStateList.value) { product ->
Text(
modifier = Modifier.fillMaxWidth(),
text = product.name,
textAlign = TextAlign.Center
)
Spacer(modifier = Modifier.height(10.dp))
}
}
Button(onClick = {
coroutineScope.launch {
mainDb.dao.insertProduct(
Product(
null,
"Product ${counter++}",
"asldfkjasldkfasldkfj"
)
)
}
}) {
Text(text = "Create data")
}
}
}
}
}

private fun scan() {
val options = ScanOptions()
options.setDesiredBarcodeFormats(ScanOptions.QR_CODE)
options.setPrompt("Scan a barcode")
options.setCameraId(0)
options.setBeepEnabled(false)
options.setBarcodeImageEnabled(true)
scanLauncher.launch(options)
}
}

App:

import android.app.Application
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class App : Application() {
}

Добавить комментарий

Ваш адрес email не будет опубликован.