improve room database flow

master
andrekir 2022-07-29 21:28:09 -03:00
rodzic 6678a82578
commit 1196eb6cc5
3 zmienionych plików z 22 dodań i 17 usunięć

Wyświetl plik

@ -1,26 +1,20 @@
package com.geeksville.mesh.database
import android.app.Application
import androidx.room.Room
import com.geeksville.mesh.database.dao.PacketDao
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton
@InstallIn(SingletonComponent::class)
@Module
class DatabaseModule {
@Provides
fun provideDatabase(application: Application): MeshtasticDatabase {
return Room.databaseBuilder(
application.applicationContext,
MeshtasticDatabase::class.java,
"meshtastic_database"
)
.fallbackToDestructiveMigration()
.build()
}
@Singleton
fun provideDatabase(app: Application): MeshtasticDatabase =
MeshtasticDatabase.getDatabase(app)
@Provides
fun providePacketDao(database: MeshtasticDatabase): PacketDao {

Wyświetl plik

@ -1,6 +1,8 @@
package com.geeksville.mesh.database
import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.geeksville.mesh.database.dao.PacketDao
import com.geeksville.mesh.database.entity.Packet
@ -8,4 +10,17 @@ import com.geeksville.mesh.database.entity.Packet
@Database(entities = [Packet::class], version = 1, exportSchema = false)
abstract class MeshtasticDatabase : RoomDatabase() {
abstract fun packetDao(): PacketDao
companion object {
fun getDatabase(context: Context): MeshtasticDatabase {
return Room.databaseBuilder(
context.applicationContext,
MeshtasticDatabase::class.java,
"meshtastic_database"
)
.fallbackToDestructiveMigration()
.build()
}
}
}

Wyświetl plik

@ -5,7 +5,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.asLiveData
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
@ -22,19 +22,15 @@ class DebugFragment : Fragment() {
// This property is only valid between onCreateView and onDestroyView.
private val binding get() = _binding!!
// FIXME hacky way to grab packets re-creating UIViewModel instead of using activityViewModels()
private val model: UIViewModel by viewModels()
private val model: UIViewModel by activityViewModels()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
): View {
_binding = DebugFragmentBinding.inflate(inflater, container, false)
return binding.root
}
//Button to clear All log
//List all log
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)