Зависимости в 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() {
}
Watch latest movies here, Bollywood, hollywood, tv series, web series, all hindi bengali english tamil movies watch here
Watch latest movies here, Bollywood, hollywood, tv series, web series, all hindi bengali english tamil movies watch here
다양한 정보를 확인할 수 있는 오피 사이트를 통해 필요한 내용을 편리하게 살펴보세요.
오피가이드를 통해 다양한 제휴 업체 정보와 생생한 이용 후기를 한눈에 확인해 보세요.
다양한 정보를 확인할 수 있는 오피 사이트를 통해 필요한 내용을 편리하게 살펴보세요.
I really like reading through a post that can make men and women think. Also, thank you for allowing me to comment!
For the reason that the admin of this site is working, no uncertainty very quickly it will be renowned, due to its quality contents.
Awesome! Its genuinely remarkable post, I have got much clear idea regarding from this post
Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
Awesome! Its genuinely remarkable post, I have got much clear idea regarding from this post
Kompetisi sains untuk tingkat sekolah Islam makin berkembang pesat dari tahun ke tahun. Siswa-siswi di daerah sekarang punya wadah yang tepat untuk menunjukkan bakat riset mereka. Catat jadwal lengkap Olimpiade Madrasah Indonesia OMI 2026 agar tidak ketinggalan informasi pendaftarannya.
I do not even understand how I ended up here, but I assumed this publish used to be great
I’m often to blogging and i really appreciate your content. The article has actually peaks my interest. I’m going to bookmark your web site and maintain checking for brand spanking new information.
This was beautiful Admin. Thank you for your reflections.
For local businesses looking to improve their online presence, this is worth exploring. Kansas City Web Design Pros is a company to consider for web design. They serve the Olathe area and list their address as 12232 S Blackfoot Dr, Olathe, KS 66062, USA; call 913-448-1315.
Pretty! This has been a really wonderful post. Many thanks for providing these details.
Great article! More people should understand the importance of checking ingredient information before purchasing nutritional products. Details such as ingredient quantity, extract ratios, testing practices, and manufacturer transparency can all help consumers evaluate available options more carefully.
Woven by hand in our own workshop — no factory, no mass production.
Уведомление: iphone duo specs
Hi guys thank you so much for like me, you can date with me.
Hi guys thank you so much for like me, you can date with me.
Watch or download movies and tv web series here you can check it.
Watch or download movies and tv web series here you can check it.
Watch or download movies and tv web series here you can check it.
Watch or download movies and tv web series here you can check it.
Watch or download movies and tv web series here you can check it.
Watch or download movies and tv web series here you can check it.