Skip to content

Commit

Permalink
Merge pull request #13 from Steve-Mr/dev
Browse files Browse the repository at this point in the history
[fox]
  • Loading branch information
Steve-Mr authored May 31, 2023
2 parents 99af240 + f4e3c56 commit 6159153
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 78 deletions.
15 changes: 3 additions & 12 deletions app/src/main/java/com/maary/liveinpeace/ConnectionViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,17 @@ package com.maary.liveinpeace
import androidx.lifecycle.*
import com.maary.liveinpeace.database.Connection
import com.maary.liveinpeace.database.ConnectionRepository
import com.maary.liveinpeace.database.ConnectionSummary
import java.sql.Date
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.time.LocalDate

class ConnectionViewModel(private val connectionRepository: ConnectionRepository) : ViewModel() {

// val allConnectionsOnDate: LiveData<List<Connection>> = connectionRepository.getAllConnectionsOnDate()
val allConnectionsToday: LiveData<List<Connection>> = connectionRepository.allConnectionsToday.asLiveData()

val summaryToday: LiveData<List<Connection>> = connectionRepository.summaryToday.asLiveData()

fun getAllConnectionsOnDate(queryDate: String): LiveData<List<Connection>> {
return connectionRepository.getAllConnectionsOnDate(queryDate).asLiveData()
}

fun insert(connection: Connection) = viewModelScope.launch(Dispatchers.IO) {
connectionRepository.insert(connection)
fun getSummaryOnDate(queryDate: String): LiveData<List<Connection>> {
return connectionRepository.getSummaryOnDate(queryDate).asLiveData()
}

}

class ConnectionViewModelFactory(private val connectionRepository: ConnectionRepository) : ViewModelProvider.Factory {
Expand Down
10 changes: 6 additions & 4 deletions app/src/main/java/com/maary/liveinpeace/HistoryActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.core.view.WindowCompat
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.maary.liveinpeace.databinding.ActivityHistoryBinding
import java.time.LocalDate


class HistoryActivity : AppCompatActivity() {
Expand All @@ -32,20 +33,21 @@ class HistoryActivity : AppCompatActivity() {
finish()
}

connectionViewModel.allConnectionsToday.observe(this) { connections ->
connectionViewModel.getAllConnectionsOnDate(LocalDate.now().toString()).observe(this) { connections ->
connections.let { connectionAdapter.submitList(it) }
}

binding.toggleHistory.addOnButtonCheckedListener { group, checkedId, isChecked ->
binding.toggleHistory.addOnButtonCheckedListener { _, checkedId, isChecked ->
if (!isChecked) return@addOnButtonCheckedListener
if (checkedId == R.id.button_timeline) {
connectionViewModel.allConnectionsToday.observe(this) { connections ->
connectionViewModel.getAllConnectionsOnDate(LocalDate.now().toString()).observe(this) { connections ->
connections.let { connectionAdapter.submitList(it) }
}
}
if (checkedId == R.id.button_summary) {
connectionViewModel.summaryToday.observe(this) { connections ->
connectionViewModel.getSummaryOnDate(LocalDate.now().toString()).observe(this) { connections ->
connections.let { connectionAdapter.submitList(it) }

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@ package com.maary.liveinpeace.database

import androidx.annotation.WorkerThread
import kotlinx.coroutines.flow.Flow
import java.sql.Date
import java.time.LocalDate

class ConnectionRepository(private val connectionDao: ConnectionDao) {

// val allConnectionsOnDate: Flow<List<Connection>> = connectionDao.loadAllConnectionsOnDate(queryDate)

val allConnectionsToday = connectionDao.loadAllConnectionsOnDate(LocalDate.now().toString())

val summaryToday:Flow<List<Connection>> = connectionDao.loadSummaryOnDate(LocalDate.now().toString())

fun getAllConnectionsOnDate(queryDate: String): Flow<List<Connection>> {
return connectionDao.loadAllConnectionsOnDate(queryDate)
}

fun getSummaryOnDate(queryDate: String): Flow<List<Connection>> {
return connectionDao.loadSummaryOnDate(queryDate)
}

@WorkerThread
suspend fun insert(connection: Connection){
connectionDao.insert(connection)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -384,62 +384,24 @@ class ForegroundService: Service() {
}
}

class DeviceTimer(private val context: Context, private val deviceName: String) : Runnable {
class DeviceTimer(private val context: Context, private val deviceName: String) {
private val handler = Handler(Looper.getMainLooper())
private var isRunning = false
private var notifyCount = 0

@SuppressLint("MissingPermission")
override fun run() {
// 延迟一定时间后再执行任务
val delayMillis = ALERT_TIME

if (notifyCount == 0) {
// 第一次执行,延迟 ALERT_TIME 时间后执行 notify
handler.postDelayed({
with(NotificationManagerCompat.from(context)) {
notify(ID_NOTIFICATION_ALERT, createTimerNotification(context = context, deviceName = deviceName))
}
notifyCount++
// 继续执行后续任务
handler.postDelayed(this, delayMillis)
}, delayMillis)
} else if (notifyCount < MAX_NOTIFY_COUNT) {
// 后续执行,达到通知次数后停止任务
with(NotificationManagerCompat.from(context)) {
notify(ID_NOTIFICATION_ALERT, createTimerNotification(context = context, deviceName = deviceName))
}
notifyCount++
if (notifyCount < MAX_NOTIFY_COUNT) {
// 继续执行后续任务
handler.postDelayed(this, delayMillis)
}
} else {
// 达到通知次数后停止任务执行
stop()
return
private val runnable = Runnable {
with(NotificationManagerCompat.from(context)) {
notify(ID_NOTIFICATION_ALERT, createTimerNotification(context = context, deviceName = deviceName))
}
}

fun start() {
if (!isRunning) {
isRunning = true
notifyCount = 0
handler.post(this)
Log.v("MUTE_TIMER", "TIMER_STARTED")
}
handler.postDelayed(runnable, ALERT_TIME)
Log.v("MUTE_TIMER", "TIMER_STARTED")
}

fun stop() {
if (isRunning) {
isRunning = false
notifyCount = 0
handler.removeCallbacks(this)
}
}
handler.removeCallbacks(runnable)

companion object {
private const val MAX_NOTIFY_COUNT = 1
}

private fun createTimerNotification(context: Context, deviceName: String) : Notification {
Expand Down

0 comments on commit 6159153

Please sign in to comment.