Зависимости в 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
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
You’re so awesome! I don’t believe I have read a single thing like that before. So great to find someone with some original thoughts on this topic. Really.. thank you for starting this up. This website is something that is needed on the internet, someone with a little originality!
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
탁 트인 바다를 품은 휴양지에서 숙소로 직접 방문하는 프리미엄 케어를 원하신다면, 품격 있는 해운대출장마사지 서비스를 이용해 보시기 바랍니다.
Phim Heo Hay, Phim Sex Không Che Bím Ngon Gái Xinh
장시간 업무로 뭉친 근육을 시원하게 풀어주고 싶다면, 계신 곳 어디든 신속하게 방문하는 출장안마 서비스를 통해 쾌적한 힐링 타임을 가져보세요.
Phim Sex – Xem Phim Sex Nóng Chọn Lọc Mới Nhất – Vlxvn.App