Зависимости в 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() {
}
Hello, I admire your works and want to repeat one of them. I already have experience building metal detectors and am going to repeat your model Spirit Pi 4 Pro A because I liked it. You can modify the code a little and discriminate between metals with an inscription on the screen and a different sound for non-ferrous metal and ferrous metal
Phim Sex Hay, Xem Phim Heo Mới Tuyển Chọn Mới Nhất
websitem için çok işime yaradı teşekkür ederim
Çok yararlı bi yazı olmuş hocam teşekkür ederim .Sizin yazılarınızı beğenerek okuyorum elinize sağlık.
Really love how you make learning feel natural and enjoyable for everyone 🌈
Chúng tôi đăng tải những phim heo cực hay nhất, chọn lọc những phim sex, phim heo, javhd hấp dẫn và full chất lượng sắc nét nhất.
Get ready for some fast-paced action! site:hypackelgames.com delivers intense combat with satisfying combos and a cool progression system. If you’re looking for a challenging roguelike with plenty of replayability, this is definitely one to check out. The dynamic gameplay keeps you on your toes!
귀사의 온라인 판매 확대를 위해 라이브커머스 협업을 제안드립니다.
Deneme bonusu