Skip to content

Commit

Permalink
feat: todayReminder dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
JunTingLin committed Aug 9, 2023
1 parent 5750c6c commit 8e68789
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,9 @@ import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
import androidx.cardview.widget.CardView
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.google.android.material.chip.Chip
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.junting.drug_android_frontend.databinding.FragmentPillBoxManagementBinding
import com.junting.drug_android_frontend.model.drug_record.DrugRecord
import com.junting.drug_android_frontend.services.BTServices.BluetoothSocket
import com.junting.drug_android_frontend.ui.drugRecords.DrugRecordsViewModel

Expand All @@ -23,7 +17,7 @@ class DrugPositionButtonSheet(viewModel: DrugRecordsViewModel) : BottomSheetDial
private val binding get() = _binding!!
private val viewModel: DrugRecordsViewModel
private val positions = (1..9).toList()
private lateinit var viewManager: PillBoxViewManager
private lateinit var pillBoxViewManager: PillBoxViewManager

init {
this.viewModel = viewModel
Expand All @@ -35,14 +29,14 @@ class DrugPositionButtonSheet(viewModel: DrugRecordsViewModel) : BottomSheetDial
savedInstanceState: Bundle?,
): View {
_binding = FragmentPillBoxManagementBinding.inflate(inflater, container, false)
viewManager = PillBoxViewManager(binding,requireContext()) // Initialize the view manager
pillBoxViewManager = PillBoxViewManager(binding,requireContext()) // Initialize the view manager

positions.forEach { i -> initCell(i) }
viewModel.record.observe(viewLifecycleOwner) {
if (positions.contains(it.position)) {
viewManager.showCell(it.position, it,false)
viewManager.setCellColor(it.position)
viewManager.closeProgressBar(it.position)
pillBoxViewManager.showCell(it.position, it,false)
pillBoxViewManager.setCellColor(it.position)
pillBoxViewManager.closeProgressBar(it.position)
}
}
if (viewModel.records.value == null) {
Expand All @@ -54,8 +48,8 @@ class DrugPositionButtonSheet(viewModel: DrugRecordsViewModel) : BottomSheetDial
// skip current editing record
.filter { record -> record.position != viewModel.record.value?.position }
.filter { record -> positions.contains(record.position) }
.forEach { record -> viewManager.showCell(record.position, record,false) }
positions.forEach { i -> viewManager.closeProgressBar(i) }
.forEach { record -> pillBoxViewManager.showCell(record.position, record,false) }
positions.forEach { i -> pillBoxViewManager.closeProgressBar(i) }
}

val inflater = LayoutInflater.from(context)
Expand All @@ -66,21 +60,21 @@ class DrugPositionButtonSheet(viewModel: DrugRecordsViewModel) : BottomSheetDial
}

private fun initCell(position: Int) {
val drugPositionId = viewManager.getResourceIdByPosition(position)
val drugPositionId = pillBoxViewManager.getResourceIdByPosition(position)
val cellView = binding.root.findViewById<View>(drugPositionId)
val cardView = cellView.findViewById<View>(R.id.card_view)
viewManager.hideCell(position,positions)
pillBoxViewManager.hideCell(position,positions)
cardView?.setOnClickListener {
if (!isPositionEmpty(position)) {
return@setOnClickListener
}
val selectedDrugPositionIdNumber =
resources.getResourceEntryName(cellView.id).substringAfterLast("_")
Log.d("CellClicked", "Cell ID: $selectedDrugPositionIdNumber")
viewManager.resetCellsColor(positions)
viewManager.setCellColor(selectedDrugPositionIdNumber.toInt())
pillBoxViewManager.resetCellsColor(positions)
pillBoxViewManager.setCellColor(selectedDrugPositionIdNumber.toInt())
val oldPosition = viewModel.record.value!!.position
viewManager.hideCell(oldPosition,positions)
pillBoxViewManager.hideCell(oldPosition,positions)
updateDrugRecordsPosition(oldPosition, position)
viewModel.setPosition(position)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import com.junting.drug_android_frontend.databinding.FragmentPillBoxManagementBi
import com.junting.drug_android_frontend.model.take_record.TakeRecord
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale
Expand All @@ -31,7 +30,7 @@ class OnDemandActivity : AppCompatActivity() {
private lateinit var bindingActionBarTakeRecordTodayReminder: ActionBarTakeRecordTodayReminderBinding
var drugRecordId: Int? = null
private var viewModel: OnDemandViewModel = OnDemandViewModel()
private lateinit var viewManager: PillBoxViewManager
private lateinit var pillBoxViewManager: PillBoxViewManager
private val positions = (1..9).toList()

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -42,7 +41,7 @@ class OnDemandActivity : AppCompatActivity() {
binding.lifecycleOwner = this
binding.viewModel = viewModel
setContentView(binding.root)
viewManager = PillBoxViewManager(bindingPillBox,this) // Initialize the view manager
pillBoxViewManager = PillBoxViewManager(bindingPillBox,this) // Initialize the view manager

supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setDisplayShowCustomEnabled(true)
Expand All @@ -56,7 +55,7 @@ class OnDemandActivity : AppCompatActivity() {
initViewModel()
}
// 隱藏所有藥物位置
positions.forEach { i -> viewManager.hideCell(i,positions) }
positions.forEach { i -> pillBoxViewManager.hideCell(i,positions) }
initActualTime()
initButton()
initClickableTextView()
Expand All @@ -68,7 +67,7 @@ class OnDemandActivity : AppCompatActivity() {
Log.d("Observe todayReminder", "record: ${it.toString()}")
bindingActionBarTakeRecordTodayReminder.tvDrugName.text = it.drug.name
bindingActionBarTakeRecordTodayReminder.tvStock.text = resources.getString(R.string.stock)+""+it.stock.toString()+" "+resources.getString(R.string.unit)
viewManager.setCellColor(it.position)
pillBoxViewManager.setCellColor(it.position)
binding.progressBar.visibility = View.GONE
})
viewModel.fetchDrugRecords()
Expand All @@ -77,18 +76,18 @@ class OnDemandActivity : AppCompatActivity() {
// 遍历记录并更新 UI
for (record in it) {
when (record.position) {
1 -> viewManager.showCell(1, record,false)
2 -> viewManager.showCell(2, record,false)
3 -> viewManager.showCell(3, record,false)
4 -> viewManager.showCell(4, record,false)
5 -> viewManager.showCell(5, record,false)
6 -> viewManager.showCell(6, record,false)
7 -> viewManager.showCell(7, record,false)
8 -> viewManager.showCell(8, record,false)
9 -> viewManager.showCell(9, record,false)
1 -> pillBoxViewManager.showCell(1, record,false)
2 -> pillBoxViewManager.showCell(2, record,false)
3 -> pillBoxViewManager.showCell(3, record,false)
4 -> pillBoxViewManager.showCell(4, record,false)
5 -> pillBoxViewManager.showCell(5, record,false)
6 -> pillBoxViewManager.showCell(6, record,false)
7 -> pillBoxViewManager.showCell(7, record,false)
8 -> pillBoxViewManager.showCell(8, record,false)
9 -> pillBoxViewManager.showCell(9, record,false)
}
}
positions.forEach { i -> viewManager.closeProgressBar(i) }
positions.forEach { i -> pillBoxViewManager.closeProgressBar(i) }
})

val inflater = LayoutInflater.from(this)
Expand Down Expand Up @@ -162,45 +161,47 @@ class OnDemandActivity : AppCompatActivity() {


private fun showConfirmDialog() {
val parentView = bindingPillBox.root.parent as? ViewGroup
parentView?.removeView(bindingPillBox.root)

val dialog = MaterialAlertDialogBuilder(this)
.setTitle(resources.getString(R.string.taken_drug))
.setView(bindingPillBox.root)
.setPositiveButton(resources.getString(R.string.close_pillbox)) { dialog, which ->
var takeRecord = TakeRecord(
drugRecordId = viewModel.drugRecord.value!!.id,
status = 5,
dosage = viewModel.drugRecord.value!!.dosage,
timeSlot = viewModel.actualTakingTime.get()!!
)
Log.d("TakeRecord", "takeRecord: ${takeRecord.toString()}")

viewModel.viewModelScope.launch(Dispatchers.IO) {
val responseMessage = viewModel.processTakeRecord(takeRecord).await()
if (responseMessage != null) {
runOnUiThread(Runnable {
Toast.makeText(this@OnDemandActivity, "服用成功", Toast.LENGTH_SHORT).show()
})
// 成功處理 TakeRecord
} else {
// 處理失敗
runOnUiThread(Runnable {
Toast.makeText(this@OnDemandActivity, "服用失敗", Toast.LENGTH_SHORT).show()
})
// ... 處理失敗的邏輯 ...
}
}

val intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
intent.putExtra("fragmentName", "TodayReminderFragment")
startActivity(intent)
var takeRecord = TakeRecord(
drugRecordId = viewModel.drugRecord.value!!.id,
status = 5,
dosage = viewModel.drugRecord.value!!.dosage,
timeSlot = viewModel.actualTakingTime.get()!!
)
Log.d("TakeRecord", "takeRecord: ${takeRecord.toString()}")

viewModel.viewModelScope.launch(Dispatchers.IO) {
val responseMessage = viewModel.processTakeRecord(takeRecord).await()
if (responseMessage != null) {
runOnUiThread(Runnable {
Toast.makeText(this@OnDemandActivity, "服用成功", Toast.LENGTH_SHORT).show()

val parentView = bindingPillBox.root.parent as? ViewGroup
parentView?.removeView(bindingPillBox.root)

val builder = MaterialAlertDialogBuilder(this@OnDemandActivity)
.setTitle(resources.getString(R.string.taken_drug))
.setView(bindingPillBox.root)
.setPositiveButton(resources.getString(R.string.close_pillbox)) { dialog, which ->


val intent = Intent(this@OnDemandActivity, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
intent.putExtra("fragmentName", "TodayReminderFragment")
startActivity(intent)
}
.create()

builder.show()
})
// 成功處理 TakeRecord
} else {
// 處理失敗
runOnUiThread(Runnable {
Toast.makeText(this@OnDemandActivity, "服用失敗", Toast.LENGTH_SHORT).show()
})
// ... 處理失敗的邏輯 ...
}
.create()

dialog.show()
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class TodayReminderActivity : AppCompatActivity() {
private lateinit var bindingActionBarTakeRecordTodayReminder: ActionBarTakeRecordTodayReminderBinding
var todayReminderId: Int? = null
private var viewModel: TodayReminderViewModel = TodayReminderViewModel()
private lateinit var viewManager: PillBoxViewManager
private lateinit var pillBoxViewManager: PillBoxViewManager
private val positions = (1..9).toList()

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -44,7 +44,7 @@ class TodayReminderActivity : AppCompatActivity() {
binding.lifecycleOwner = this
binding.viewModel = viewModel
setContentView(binding.root)
viewManager = PillBoxViewManager(bindingPillBox,this) // Initialize the view manager
pillBoxViewManager = PillBoxViewManager(bindingPillBox,this) // Initialize the view manager

supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setDisplayShowCustomEnabled(true)
Expand All @@ -58,7 +58,7 @@ class TodayReminderActivity : AppCompatActivity() {
initViewModel()
}
// 隱藏所有藥物位置
positions.forEach { i -> viewManager.hideCell(i,positions) }
positions.forEach { i -> pillBoxViewManager.hideCell(i,positions) }
initActualTime()
initButton()
initClickableTextView()
Expand All @@ -74,7 +74,7 @@ class TodayReminderActivity : AppCompatActivity() {
Log.d("Observe todayReminder", "record: ${it.toString()}")
bindingActionBarTakeRecordTodayReminder.tvDrugName.text = it.drug.name
bindingActionBarTakeRecordTodayReminder.tvStock.text = resources.getString(R.string.stock)+":"+it.stock.toString()+" "+resources.getString(R.string.unit)
viewManager.setCellColor(it.position)
pillBoxViewManager.setCellColor(it.position)
binding.progressBar.visibility = View.GONE
})
viewModel.fetchDrugRecords()
Expand All @@ -83,18 +83,18 @@ class TodayReminderActivity : AppCompatActivity() {
// 遍历记录并更新 UI
for (record in it) {
when (record.position) {
1 -> viewManager.showCell(1, record,false)
2 -> viewManager.showCell(2, record,false)
3 -> viewManager.showCell(3, record,false)
4 -> viewManager.showCell(4, record,false)
5 -> viewManager.showCell(5, record,false)
6 -> viewManager.showCell(6, record,false)
7 -> viewManager.showCell(7, record,false)
8 -> viewManager.showCell(8, record,false)
9 -> viewManager.showCell(9, record,false)
1 -> pillBoxViewManager.showCell(1, record,false)
2 -> pillBoxViewManager.showCell(2, record,false)
3 -> pillBoxViewManager.showCell(3, record,false)
4 -> pillBoxViewManager.showCell(4, record,false)
5 -> pillBoxViewManager.showCell(5, record,false)
6 -> pillBoxViewManager.showCell(6, record,false)
7 -> pillBoxViewManager.showCell(7, record,false)
8 -> pillBoxViewManager.showCell(8, record,false)
9 -> pillBoxViewManager.showCell(9, record,false)
}
}
positions.forEach { i -> viewManager.closeProgressBar(i) }
positions.forEach { i -> pillBoxViewManager.closeProgressBar(i) }
})
}

Expand Down Expand Up @@ -274,43 +274,40 @@ class TodayReminderActivity : AppCompatActivity() {
bottomSheetDialog.show()
}
private fun showConfirmDialog() {
val parentView = bindingPillBox.root.parent as? ViewGroup
parentView?.removeView(bindingPillBox.root)

val dialog = MaterialAlertDialogBuilder(this)
.setTitle(resources.getString(R.string.taken_drug))
.setView(bindingPillBox.root)
.setPositiveButton(resources.getString(R.string.close_pillbox)) { dialog, which ->
var takeRecord = TakeRecord(
todayReminderId = viewModel.todayReminder.value!!.id,
status = 1,
dosage = viewModel.todayReminder.value!!.dosage,
timeSlot = viewModel.actualTakingTime.get()!!
)
viewModel.viewModelScope.launch(Dispatchers.IO) {
val responseMessage = viewModel.processTakeRecord(takeRecord).await()
if (responseMessage != null) {
runOnUiThread(Runnable {
Toast.makeText(this@TodayReminderActivity, "服用成功", Toast.LENGTH_SHORT).show()
})
// 成功處理 TakeRecord
} else {
// 處理失敗
runOnUiThread(Runnable {
Toast.makeText(this@TodayReminderActivity, "系統錯誤", Toast.LENGTH_SHORT).show()
})
// ... 處理失敗的邏輯 ...
}
var takeRecord = TakeRecord(
todayReminderId = viewModel.todayReminder.value!!.id,
status = 1,
dosage = viewModel.todayReminder.value!!.dosage,
timeSlot = viewModel.actualTakingTime.get()!!
)
viewModel.viewModelScope.launch(Dispatchers.IO) {
var responseMessage = viewModel.processTakeRecord(takeRecord).await()
runOnUiThread(Runnable {
if (responseMessage != null) {
Toast.makeText(this@TodayReminderActivity, "服用成功", Toast.LENGTH_SHORT).show()

val parentView = bindingPillBox.root.parent as? ViewGroup
parentView?.removeView(bindingPillBox.root)

val dialog = MaterialAlertDialogBuilder(this@TodayReminderActivity)
.setTitle(resources.getString(R.string.taken_drug))
.setView(bindingPillBox.root)
.setPositiveButton(resources.getString(R.string.close_pillbox)) { dialog, which ->
val intent = Intent(this@TodayReminderActivity, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
intent.putExtra("fragmentName", "TodayReminderFragment")
startActivity(intent)
}
.create()

dialog.show()
} else {
Toast.makeText(this@TodayReminderActivity, "系統錯誤", Toast.LENGTH_SHORT).show()
// ... 處理失敗的邏輯 ...
}
})
}

val intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
intent.putExtra("fragmentName", "TodayReminderFragment")
startActivity(intent)
}
.create()

dialog.show()
}

private fun addMinutesToActualTime(minutes: Int) {
Expand Down
Loading

0 comments on commit 8e68789

Please sign in to comment.