Skip to content

Instantly share code, notes, and snippets.

@jafar260698
Last active December 11, 2020 06:24
Show Gist options
  • Save jafar260698/a0d573b1ff31528f2c2a5b375d5ab178 to your computer and use it in GitHub Desktop.
Save jafar260698/a0d573b1ff31528f2c2a5b375d5ab178 to your computer and use it in GitHub Desktop.
//person
@Entity(tableName = "person")
data class Person(
val uuid:String?=null,
val name:String?=null,
val surname:String?=null,
val phone:String?=null,
val update_time:String?=null,
val image_uuid:String?=null,
val image_update_time: String?=null,
@TypeConverters(Converters::class)
val tags:List<TagUIID>?=null) :Serializable
{
@PrimaryKey(autoGenerate = true)
var id:Int?=null
}
//events
@Entity(tableName = "events")
data class Events(
val uuid : String,
val person_uuid : String,
val event_name : String,
val note : String
){
@PrimaryKey(autoGenerate = true)
var id:Int?=null
}
// combined tables
data class PersonWithEvents(
@Embedded(prefix = "per_")
val person: Person,
@Relation(
parentColumn = "per_uuid",
entityColumn = "person_uuid"
)
val events: List<Events>
)
// Dao
@Transaction
@Query("SELECT * FROM person")
fun getPerson(): LiveData<List<PersonWithEvents>>
// repository
class PeopleRepository(val db:MainDatabase) {
fun getAllPeople()=db.getPeople().getPerson()
}
//viewmodel
class PeopleViewModel(val app:Application,
val repository: PeopleRepository) : AndroidViewModel(app){
fun getAllPeople()=repository.getAllPeople()
}
// Fragment
class HomeFragment : Fragment(R.layout.fragment_home),PeopleAdapter.OnItemClickListener{
private lateinit var viewmodel : PeopleViewModel
private var homeAdapter: PeopleAdapter?=null
private var layoutManagers: RecyclerView.LayoutManager? = null
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
_binding= FragmentHomeBinding.bind(view)
val repository= PeopleRepository(MainDatabase(requireActivity()))
val viewModelProviderFactory= PeopleViewModelFactory(requireActivity().application, repository)
viewmodel= ViewModelProvider(this,viewModelProviderFactory).get(PeopleViewModel::class.java)
viewmodel.getAllPeople().observe(viewLifecycleOwner, Observer { list->
if (list.size>0&&list!=null){
Log.e("Response","$list")
// homeAdapter= PeopleAdapter(requireActivity(),list,this)
// binding.recyclerviewHome.adapter=homeAdapter
// homeAdapter!!.submitList(list)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment