Skip to content

Commit

Permalink
Merge pull request #16 from Steve-Mr/dev
Browse files Browse the repository at this point in the history
[feat] 连接历史界面增加日期选择
  • Loading branch information
Steve-Mr authored Jun 13, 2023
2 parents 823a352 + 46e1ede commit c573776
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ app/src/androidTest
app/src/test
key.properties
.idea/
key.base64
7 changes: 5 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ android {
applicationId "com.maary.liveinpeace"
minSdk 31
targetSdk 33
versionCode 3
versionName "2.0_beta"
versionCode 4
versionName "2.1_beta"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/maary/liveinpeace/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
96 changes: 75 additions & 21 deletions app/src/main/java/com/maary/liveinpeace/HistoryActivity.kt
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -110,14 +159,19 @@ class HistoryActivity : AppCompatActivity(), DeviceMapChangeListener {

override fun onDeviceMapChanged(deviceMap: Map<String, Connection>) {
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)
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_calendar.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="M20,3h-1L19,1h-2v2L7,3L7,1L5,1v2L4,3c-1.1,0 -2,0.9 -2,2v16c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,5c0,-1.1 -0.9,-2 -2,-2zM20,21L4,21L4,10h16v11zM20,8L4,8L4,5h16v3z"/>
</vector>
49 changes: 34 additions & 15 deletions app/src/main/res/layout/activity_history.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,47 @@

</com.google.android.material.appbar.CollapsingToolbarLayout>

<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggle_history"
android:layout_width="wrap_content"
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:background="@android:color/transparent"
app:selectionRequired="true"
app:singleSelection="true">
<Button
android:id="@+id/button_timeline"
style="?attr/materialButtonOutlinedStyle"
android:layout_margin="16dp">

<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggle_history"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/timeline" />
android:background="@android:color/transparent"
app:selectionRequired="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:singleSelection="true">
<Button
android:id="@+id/button_timeline"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/timeline" />

<Button
android:id="@+id/button_summary"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/summary" />
</com.google.android.material.button.MaterialButtonToggleGroup>


<Button
android:id="@+id/button_summary"
style="?attr/materialButtonOutlinedStyle"
android:id="@+id/button_calendar"
style="@style/Widget.Material3.Button.Icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/summary" />
</com.google.android.material.button.MaterialButtonToggleGroup>
app:icon = "@drawable/ic_calendar"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:text="06/13"/>

</androidx.constraintlayout.widget.ConstraintLayout>

</com.google.android.material.appbar.AppBarLayout>

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@
<string name="connections_history">历史连接</string>
<string name="has_already_connected_for">已连接</string>
<string name="device_connection_state">设备连接状态指示</string>
<string name="select_date">选择日期</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<string name="connections_history">Connections History</string>
<string name="has_already_connected_for">Already connected</string>
<string name="device_connection_state">Device connection state indicator</string>
<string name="select_date">Select a Date</string>
<string-array name="array_volume_comment">
<item>Shhhhhhh…</item>
<item>Perfectly Balanced, As All Things Should Be…</item>
Expand Down

0 comments on commit c573776

Please sign in to comment.