From f52a69614248793ed208550f5cf5caa32f289ce5 Mon Sep 17 00:00:00 2001 From: Maary <24504742+Steve-Mr@users.noreply.github.com> Date: Tue, 13 Jun 2023 19:24:25 +0800 Subject: [PATCH 1/2] add timepicker in HistoryActivity --- .gitignore | 1 + app/build.gradle | 3 + .../java/com/maary/liveinpeace/Constants.kt | 2 + .../com/maary/liveinpeace/HistoryActivity.kt | 96 +++++++++++++++---- app/src/main/res/drawable/ic_calendar.xml | 5 + app/src/main/res/layout/activity_history.xml | 49 +++++++--- app/src/main/res/values-zh-rCN/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 8 files changed, 122 insertions(+), 36 deletions(-) create mode 100644 app/src/main/res/drawable/ic_calendar.xml diff --git a/.gitignore b/.gitignore index a2ec042..5dbc59b 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ app/src/androidTest app/src/test key.properties .idea/ +key.base64 diff --git a/app/build.gradle b/app/build.gradle index f8ae802..e63edcc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -57,6 +57,9 @@ android { proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' signingConfig signingConfigs.release } + debug { + signingConfig signingConfigs.release + } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 diff --git a/app/src/main/java/com/maary/liveinpeace/Constants.kt b/app/src/main/java/com/maary/liveinpeace/Constants.kt index 1da106c..48ffd8d 100644 --- a/app/src/main/java/com/maary/liveinpeace/Constants.kt +++ b/app/src/main/java/com/maary/liveinpeace/Constants.kt @@ -48,5 +48,7 @@ class Constants { const val ID_NOTIFICATION_GROUP_FORE = "LIP_notification_group_foreground" const val ID_NOTIFICATION_GROUP_SETTINGS = "LIP_notification_group_settings" const val ID_NOTIFICATION_GROUP_ALERTS = "LIP_notification_group_alerts" + const val PATTERN_DATE_DATABASE = "yyyy-MM-dd" + const val PATTERN_DATE_BUTTON = "MM/dd" } } \ No newline at end of file diff --git a/app/src/main/java/com/maary/liveinpeace/HistoryActivity.kt b/app/src/main/java/com/maary/liveinpeace/HistoryActivity.kt index 16fbf6d..e9ec9ee 100644 --- a/app/src/main/java/com/maary/liveinpeace/HistoryActivity.kt +++ b/app/src/main/java/com/maary/liveinpeace/HistoryActivity.kt @@ -1,17 +1,24 @@ package com.maary.liveinpeace import android.os.Bundle -import android.util.Log import android.view.View import androidx.activity.viewModels import androidx.appcompat.app.AppCompatActivity import androidx.core.view.WindowCompat import androidx.databinding.DataBindingUtil import androidx.recyclerview.widget.LinearLayoutManager +import com.google.android.material.datepicker.CalendarConstraints +import com.google.android.material.datepicker.DateValidatorPointBackward +import com.google.android.material.datepicker.MaterialDatePicker +import com.maary.liveinpeace.Constants.Companion.PATTERN_DATE_BUTTON +import com.maary.liveinpeace.Constants.Companion.PATTERN_DATE_DATABASE import com.maary.liveinpeace.database.Connection import com.maary.liveinpeace.databinding.ActivityHistoryBinding import com.maary.liveinpeace.service.ForegroundService +import java.text.SimpleDateFormat import java.time.LocalDate +import java.util.Calendar +import java.util.Locale class HistoryActivity : AppCompatActivity(), DeviceMapChangeListener { @@ -57,7 +64,44 @@ class HistoryActivity : AppCompatActivity(), DeviceMapChangeListener { binding = DataBindingUtil.setContentView(this, R.layout.activity_history) setContentView(binding.root) + var pickedDate : String = LocalDate.now().toString() + val connectionAdapter = ConnectionListAdapter() + + // Makes only dates from today forward selectable. + val constraintsBuilder = + CalendarConstraints.Builder() + .setValidator(DateValidatorPointBackward.now()) + + var datePicker = + MaterialDatePicker.Builder.datePicker() + .setTitleText(R.string.select_date) + .setCalendarConstraints(constraintsBuilder.build()) + .setSelection(MaterialDatePicker.todayInUtcMilliseconds()) + .build() + + fun updateHistoryList(checkedId: Int){ + if (checkedId == R.id.button_timeline) { + connectionViewModel.getAllConnectionsOnDate(pickedDate).observe(this) { connections -> + connections.let { connectionAdapter.submitList(it) } + } + } + if (checkedId == R.id.button_summary) { + connectionViewModel.getSummaryOnDate(pickedDate).observe(this) { connections -> + connections.let { connectionAdapter.submitList(it) } + } + } + updateCurrentAdapter() + } + + fun changeDate(dateInMilli: Long?){ + if (dateInMilli == null) return changeDate(System.currentTimeMillis()) + binding.buttonCalendar.text = formatMillisecondsToDate(dateInMilli, PATTERN_DATE_BUTTON) + pickedDate = formatMillisecondsToDate(dateInMilli, PATTERN_DATE_DATABASE) + updateHistoryList(binding.toggleHistory.checkedButtonId) + updateCurrentAdapter() + } + binding.historyList.isNestedScrollingEnabled = false binding.historyList.adapter = connectionAdapter binding.historyList.layoutManager = LinearLayoutManager(this) @@ -68,40 +112,45 @@ class HistoryActivity : AppCompatActivity(), DeviceMapChangeListener { finish() } + binding.buttonCalendar.text = formatMillisecondsToDate(System.currentTimeMillis(), PATTERN_DATE_BUTTON) + + binding.buttonCalendar.setOnClickListener { + datePicker.show(supportFragmentManager, "MATERIAL_DATE_PICKER") + } + + binding.buttonCalendar.setOnLongClickListener{ + changeDate(System.currentTimeMillis()) + datePicker = MaterialDatePicker.Builder.datePicker() + .setTitleText(R.string.select_date) + .setCalendarConstraints(constraintsBuilder.build()) + .setSelection(MaterialDatePicker.todayInUtcMilliseconds()) + .build() + true + } + binding.currentList.isNestedScrollingEnabled = false binding.currentList.adapter = currentAdapter binding.currentList.layoutManager = LinearLayoutManager(this) updateCurrentAdapter() - connectionViewModel.getAllConnectionsOnDate(LocalDate.now().toString()).observe(this) { connections -> + connectionViewModel.getAllConnectionsOnDate(pickedDate).observe(this) { connections -> connections.let { connectionAdapter.submitList(it) } } binding.toggleHistory.addOnButtonCheckedListener { _, checkedId, isChecked -> if (!isChecked) return@addOnButtonCheckedListener - if (checkedId == R.id.button_timeline) { - connectionViewModel.getAllConnectionsOnDate(LocalDate.now().toString()).observe(this) { connections -> - connections.let { connectionAdapter.submitList(it) } - } - updateCurrentAdapter() - } - if (checkedId == R.id.button_summary) { - connectionViewModel.getSummaryOnDate(LocalDate.now().toString()).observe(this) { connections -> - connections.let { connectionAdapter.submitList(it) } - - } - updateCurrentAdapter() - } + updateHistoryList(checkedId) } - + datePicker.addOnPositiveButtonClickListener { + changeDate(datePicker.selection) + } } private fun updateCurrentAdapter(){ currentAdapter.submitList(currentConnectionsDuration(ForegroundService.getConnections())) if (currentAdapter.itemCount == 0){ - Log.v("MUTE_", "GG") binding.titleCurrent.visibility = View.GONE }else{ binding.titleCurrent.visibility = View.VISIBLE @@ -110,14 +159,19 @@ class HistoryActivity : AppCompatActivity(), DeviceMapChangeListener { override fun onDeviceMapChanged(deviceMap: Map) { if (deviceMap.isEmpty()){ - Log.v("MUTE_", "GGA") - binding.titleCurrent.visibility = View.GONE }else{ - Log.v("MUTE_", "GGB") - binding.titleCurrent.visibility = View.VISIBLE } currentAdapter.submitList(currentConnectionsDuration(deviceMap.values.toMutableList())) } + + private fun formatMillisecondsToDate(milliseconds: Long?, pattern: String): String { + val dateFormat = SimpleDateFormat(pattern, Locale.getDefault()) + val calendar = Calendar.getInstance() + if (milliseconds != null) { + calendar.timeInMillis = milliseconds + } + return dateFormat.format(calendar.time) + } } \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_calendar.xml b/app/src/main/res/drawable/ic_calendar.xml new file mode 100644 index 0000000..0026136 --- /dev/null +++ b/app/src/main/res/drawable/ic_calendar.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/layout/activity_history.xml b/app/src/main/res/layout/activity_history.xml index cc6eedf..2fa8e22 100644 --- a/app/src/main/res/layout/activity_history.xml +++ b/app/src/main/res/layout/activity_history.xml @@ -37,28 +37,47 @@ - -