Skip to content

Commit

Permalink
feat: cleaner version of drug records tab
Browse files Browse the repository at this point in the history
  • Loading branch information
feveral committed Mar 13, 2023
1 parent cb6a91d commit 7da4e08
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.junting.drug_android_frontend.ui.drugRecords

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ProgressBar
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.junting.drug_android_frontend.R

class DrugRecordsAllPage(context: Context, container: ViewGroup) {

val view: View
private val context: Context
private val container: ViewGroup

private val progressBar: ProgressBar
private val recyclerView: RecyclerView
private var recyclerAdapter: DrugsRecordViewAdapter

private val viewModel: DrugRecordsViewModel

init {
this.context = context
this.container = container
this.view = LayoutInflater.from(context).inflate(R.layout.drug_records_all_tab, container, false)
this.progressBar = view.findViewById(R.id.progressBar)
this.recyclerView = view.findViewById(R.id.recycler_view)
this.viewModel = DrugRecordsViewModel()
this.recyclerAdapter = DrugsRecordViewAdapter(context, viewModel)
this.initAdapter()
this.initProgressBar()
this.initRecyclerView()
}

private fun initAdapter() {
this.viewModel.fetchRecords()
this.viewModel.records.observe(context as AppCompatActivity, Observer {
recyclerAdapter!!.notifyDataSetChanged()
progressBar.visibility = View.GONE
})
}

private fun initProgressBar() {
progressBar.visibility = View.VISIBLE
}

private fun initRecyclerView() {
recyclerView.apply {
layoutManager = LinearLayoutManager(context)
adapter = recyclerAdapter
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,13 @@ import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ProgressBar
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.PagerAdapter
import com.junting.drug_android_frontend.R

class DrugRecordsPagerAdapter(context: Context): PagerAdapter() {

private val context: Context

private lateinit var viewAdapter: DrugsRecordViewAdapter
private lateinit var viewManager: RecyclerView.LayoutManager
private lateinit var viewModel: DrugRecordsViewModel

init {
this.context = context
}
Expand All @@ -37,13 +25,9 @@ class DrugRecordsPagerAdapter(context: Context): PagerAdapter() {

override fun instantiateItem(container: ViewGroup, position: Int): Any {
if (position == 0) {
val view: View =
LayoutInflater.from(context).inflate(R.layout.drug_records_all_tab, container, false)
initRecyclerViewModel(view)
initRecyclerView(view)

container.addView(view)
return view
val page = DrugRecordsAllPage(context, container)
container.addView(page.view)
return page.view
} else if (position == 1) {
val view: View =
LayoutInflater.from(context).inflate(R.layout.drug_records_hospital_tab, container, false)
Expand All @@ -60,29 +44,4 @@ class DrugRecordsPagerAdapter(context: Context): PagerAdapter() {
override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
container.removeView(`object` as View)
}

fun initRecyclerViewModel(view: View){
view.findViewById<ProgressBar>(R.id.progressBar).visibility = View.VISIBLE
viewModel = DrugRecordsViewModel()
viewModel.fetchRecords()
viewModel.records.observe(context as AppCompatActivity, Observer {
viewAdapter.notifyDataSetChanged()
view.findViewById<ProgressBar>(R.id.progressBar).visibility = View.GONE
})
}
fun initRecyclerView(view: View){
viewManager = LinearLayoutManager(context)
viewAdapter = DrugsRecordViewAdapter(context, viewModel)
view.findViewById<RecyclerView>(R.id.recycler_view).apply {
layoutManager = viewManager
adapter = viewAdapter
addItemDecoration(
DividerItemDecoration(
context,
DividerItemDecoration.VERTICAL
)
)
}
}

}
24 changes: 13 additions & 11 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/nav_host_fragment_activity_main"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="90dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
Expand All @@ -16,15 +29,4 @@
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
<fragment
android:id="@+id/nav_host_fragment_activity_main"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 7da4e08

Please sign in to comment.