Skip to content

Commit

Permalink
feat: start date
Browse files Browse the repository at this point in the history
  • Loading branch information
JunTingLin committed Aug 12, 2023
1 parent 0cde2bf commit 88546e9
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.junting.drug_android_frontend
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Parcel
import android.util.Log
import android.view.Menu
import android.view.MenuItem
Expand All @@ -15,6 +16,8 @@ import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.google.android.material.datepicker.CalendarConstraints
import com.google.android.material.datepicker.MaterialDatePicker
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.junting.drug_android_frontend.databinding.ActivityDrugRecordBinding
import com.junting.drug_android_frontend.model.drug_record.DrugRecord
Expand All @@ -23,6 +26,9 @@ import com.junting.drug_android_frontend.model.drugbag_info.DrugbagInformation
import com.junting.drug_android_frontend.ui.libs.ExpandableListUtils
import java.util.*
import com.junting.drug_android_frontend.ui.drugRecords.DrugRecordsViewModel
import java.text.SimpleDateFormat
import java.time.LocalDate
import java.time.format.DateTimeFormatter

class DrugRecordActivity : AppCompatActivity() {

Expand Down Expand Up @@ -90,11 +96,13 @@ class DrugRecordActivity : AppCompatActivity() {
}
initOndemandCheckbox()
initTimingsCheckbox()
initButtonSheet(binding.llNotificationSetting, NotificationSettingButtonSheet(viewModel), "notificationSetting")
// initButtonSheet(binding.llNotificationSetting, NotificationSettingButtonSheet(viewModel), "notificationSetting")
initButtonSheet(binding.llReturnSetting, ReturnSettingButtonSheet(viewModel), "returnSetting")
initButtonSheet(binding.llDrugPosition, DrugPositionButtonSheet(viewModel), "drugPosition")
initButton()
initPhoneLongClickCall()
showDatePickerDialog()


drugRecordId = intent.getIntExtra("drugRecordId", 0)
if (drugRecordId == 0) {
Expand Down Expand Up @@ -221,6 +229,14 @@ class DrugRecordActivity : AppCompatActivity() {
for (i in it.timings) {
checkBoxes[i].isChecked = true
}
val dateFormat = SimpleDateFormat("yyyy/MM/dd", Locale.getDefault())
val startDate = dateFormat.parse(viewModel.record.value?.notificationSetting?.startDate)
val today = Calendar.getInstance().time
if (startDate.before(today) || startDate.compareTo(today) <= 0) {
// startDate 早于或等于今天
binding.llDate.setOnClickListener(null) //取消點擊事件
}

binding.progressBar.visibility = GONE
})
}
Expand Down Expand Up @@ -387,10 +403,14 @@ class DrugRecordActivity : AppCompatActivity() {
if (isChecked) {
binding.llOuterborderTimeSlot.visibility = GONE
binding.llTimings.visibility = GONE
binding.llReturnSetting.visibility = GONE
binding.llDate.visibility = GONE
viewModel.setOnDemand(true)
} else {
binding.llOuterborderTimeSlot.visibility = View.VISIBLE
binding.llTimings.visibility = View.VISIBLE
binding.llReturnSetting.visibility = View.VISIBLE
binding.llDate.visibility = View.VISIBLE
viewModel.setOnDemand(false)
}
}
Expand All @@ -404,6 +424,56 @@ class DrugRecordActivity : AppCompatActivity() {
binding.expandableListInteraction!!.setAdapter(adapter)
ExpandableListUtils.setupExpandHeight(binding.expandableListInteraction!!, adapter!!)
}
private fun showDatePickerDialog() {
binding.llDate.setOnClickListener(View.OnClickListener {
// 使用 MaterialDatePicker 顯示日期選擇對話框
val today = MaterialDatePicker.todayInUtcMilliseconds()
// 設置日期範圍限制,禁止用戶選擇今天以前的日期
val constraintsBuilder = CalendarConstraints.Builder()
.setValidator(object : CalendarConstraints.DateValidator {
override fun isValid(date: Long): Boolean {
return date >= today
}

override fun writeToParcel(dest: Parcel, flags: Int) {
// Not needed for this example
}

override fun describeContents(): Int {
return 0
}
})
.build()


val builder = MaterialDatePicker.Builder.datePicker()

val picker = builder
.setTitleText(resources.getString(R.string.select_start_date_for_medication))
.setPositiveButtonText(resources.getString(R.string.confirm))
.setNegativeButtonText(resources.getString(R.string.cancel))
.setCalendarConstraints(constraintsBuilder) // 使用上述的日期範圍限制
.build()

// 設置選擇日期後的回調
picker.addOnPositiveButtonClickListener { selection ->
// 取得選擇的日期
val selectedDate = Date(selection)

// 格式化日期
val dateFormat = SimpleDateFormat("yyyy/MM/dd", Locale.getDefault())
val formattedDate = dateFormat.format(selectedDate)

// 更新 tv_start_date 的文字
// binding.tvStartDate.text = formattedDate
viewModel.setNotificationSettingStartDate(formattedDate)
}

// 顯示 MaterialDatePicker 對話框
picker.show(supportFragmentManager, picker.toString())
})

}

private fun initButton() {
binding.btnCancel.setOnClickListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import java.io.Serializable
data class NotificationSetting(
var remind: Int = 0,
var repeat: Int = 0,
var startDate: String = "yyyy/MM/dd",
var startDate: String = "1999/01/01",
var status: Boolean = false
): Serializable
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import com.junting.drug_android_frontend.DrugRecordActivity
import com.junting.drug_android_frontend.R
import com.junting.drug_android_frontend.databinding.DrugItemViewBinding
import com.junting.drug_android_frontend.model.drug_record.DrugRecord
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale

class DrugRecordsViewAdapter(private val context: Context, private val viewModel: DrugRecordsViewModel) :
RecyclerView.Adapter<DrugRecordsViewAdapter.MyViewHolder>() {
Expand Down Expand Up @@ -61,10 +64,22 @@ class DrugRecordsViewAdapter(private val context: Context, private val viewModel
holder.drugItemViewBinding.chipStock.setChipBackgroundColorResource(R.color.md_theme_dark_error)
}

if( drugRecord.notificationSetting.status == true ) {
holder.drugItemViewBinding.ivNotification.setImageResource(R.drawable.ic_outline_notifications_24)
// if( drugRecord.notificationSetting.status == true ) {
// holder.drugItemViewBinding.ivNotification.setImageResource(R.drawable.ic_outline_notifications_24)
// }else{
// holder.drugItemViewBinding.ivNotification.setImageResource(R.drawable.ic_outline_notifications_off_24)
// }
val dateFormat = SimpleDateFormat("yyyy/MM/dd", Locale.getDefault())
val startDate = dateFormat.parse(drugRecord.notificationSetting?.startDate)
val today = Calendar.getInstance().time
if(drugRecord.onDemand==true){
holder.drugItemViewBinding.ivNotification.setImageResource(R.drawable.ic_outline_play_circle_24)
}
else if ( startDate.after(today) || startDate.compareTo(today) >= 0) {
// startDate 早于或等于今天
holder.drugItemViewBinding.ivNotification.setImageResource(R.drawable.outline_pause_circle_24)
}else{
holder.drugItemViewBinding.ivNotification.setImageResource(R.drawable.ic_outline_notifications_off_24)
holder.drugItemViewBinding.ivNotification.setImageResource(R.drawable.ic_outline_play_circle_24)
}


Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_outline_play_circle_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10s10,-4.48 10,-10S17.52,2 12,2zM12,20c-4.41,0 -8,-3.59 -8,-8s3.59,-8 8,-8s8,3.59 8,8S16.41,20 12,20zM9.5,16.5l7,-4.5l-7,-4.5V16.5z"/>
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/outline_pause_circle_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M9,16h2V8H9V16zM12,2C6.48,2 2,6.48 2,12s4.48,10 10,10s10,-4.48 10,-10S17.52,2 12,2zM12,20c-4.41,0 -8,-3.59 -8,-8s3.59,-8 8,-8s8,3.59 8,8S16.41,20 12,20zM13,16h2V8h-2V16z"/>
</vector>
12 changes: 9 additions & 3 deletions app/src/main/res/layout/activity_drug_record.xml
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@
android:background="@drawable/list_divider" />

<LinearLayout
android:id="@+id/ll_notification_setting"
android:id="@+id/ll_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
Expand All @@ -495,11 +495,17 @@
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_outline_notifications_24"
android:drawableLeft="@drawable/ic_baseline_calendar_month_24"
android:drawablePadding="15dp"
android:text="@string/taking_notification_setting"
android:text="@string/start_date"
android:textStyle="bold" />

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tv_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{String.valueOf(viewModel.record.notificationSetting.startDate)}"/>


</LinearLayout>

Expand Down
8 changes: 4 additions & 4 deletions app/src/main/res/layout/drug_item_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@
android:id="@+id/iv_notification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:src="@drawable/ic_outline_notifications_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>
Expand Down

0 comments on commit 88546e9

Please sign in to comment.