diff --git a/android/app/src/main/java/com/metallic/chiaki/common/LogManager.kt b/android/app/src/main/java/com/metallic/chiaki/common/LogManager.kt index 26fbfb5..3907451 100644 --- a/android/app/src/main/java/com/metallic/chiaki/common/LogManager.kt +++ b/android/app/src/main/java/com/metallic/chiaki/common/LogManager.kt @@ -24,11 +24,11 @@ import java.text.SimpleDateFormat import java.util.* import java.util.regex.Pattern -private val dateFormat = SimpleDateFormat("yyyy-MM-dd_HH-mm-ss-SSSSSS", Locale.US) -private val filePrefix = "chiaki_session_" -private val filePostfix = ".log" +private val dateFormat = SimpleDateFormat("yyyy-MM-dd_HH-mm-ss-SSS", Locale.US) +private const val filePrefix = "chiaki_session_" +private const val filePostfix = ".log" private val fileRegex = Regex("$filePrefix(.*)$filePostfix") -private val keepLogFilesCount = 5 +private const val keepLogFilesCount = 5 class LogFile private constructor(val logManager: LogManager, val filename: String) { diff --git a/android/app/src/main/java/com/metallic/chiaki/settings/SettingsLogsAdapter.kt b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsLogsAdapter.kt index 3bc6d1c..41cd916 100644 --- a/android/app/src/main/java/com/metallic/chiaki/settings/SettingsLogsAdapter.kt +++ b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsLogsAdapter.kt @@ -21,15 +21,21 @@ import android.view.View import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView import com.metallic.chiaki.R -import com.metallic.chiaki.common.RegisteredHost +import com.metallic.chiaki.common.LogFile import com.metallic.chiaki.common.ext.inflate -import kotlinx.android.synthetic.main.item_registered_host.view.* +import kotlinx.android.synthetic.main.item_log_file.view.* +import java.text.DateFormat +import java.text.SimpleDateFormat +import java.util.* class SettingsLogsAdapter: RecyclerView.Adapter() { + val dateFormat: DateFormat = DateFormat.getDateInstance(DateFormat.SHORT) + val timeFormat = SimpleDateFormat("HH:mm:ss:SSS", Locale.getDefault()) + class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) - var logFiles: List = listOf() + var logFiles: List = listOf() set(value) { field = value @@ -44,6 +50,7 @@ class SettingsLogsAdapter: RecyclerView.Adapter( { val view = holder.itemView val logFile = logFiles[position] - // TODO + view.nameTextView.text = "${dateFormat.format(logFile.date)} ${timeFormat.format(logFile.date)}" + view.summaryTextView.text = logFile.filename } } \ No newline at end of file diff --git a/android/app/src/main/java/com/metallic/chiaki/settings/SettingsLogsFragment.kt b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsLogsFragment.kt index 43d5ecb..40feabe 100644 --- a/android/app/src/main/java/com/metallic/chiaki/settings/SettingsLogsFragment.kt +++ b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsLogsFragment.kt @@ -23,9 +23,13 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.appcompat.app.AppCompatDialogFragment +import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProviders +import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView import com.metallic.chiaki.R +import com.metallic.chiaki.common.LogManager import com.metallic.chiaki.common.ext.viewModelFactory import kotlinx.android.synthetic.main.fragment_settings_logs.* @@ -38,13 +42,30 @@ class SettingsLogsFragment: AppCompatDialogFragment(), TitleFragment override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + val context = context!! + viewModel = ViewModelProviders - .of(this, viewModelFactory { SettingsLogsViewModel() }) + .of(this, viewModelFactory { SettingsLogsViewModel(LogManager(context)) }) .get(SettingsLogsViewModel::class.java) val adapter = SettingsLogsAdapter() logsRecyclerView.layoutManager = LinearLayoutManager(context) logsRecyclerView.adapter = adapter + viewModel.sessionLogs.observe(this, Observer { + adapter.logFiles = it + emptyInfoGroup.visibility = if(it.isEmpty()) View.VISIBLE else View.GONE + }) + + val itemTouchSwipeCallback = object : ItemTouchSwipeCallback(context) + { + override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) + { + val pos = viewHolder.adapterPosition + val file = viewModel.sessionLogs.value?.getOrNull(pos) ?: return + viewModel.deleteLog(file) + } + } + ItemTouchHelper(itemTouchSwipeCallback).attachToRecyclerView(logsRecyclerView) } override fun getTitle(resources: Resources): String = resources.getString(R.string.preferences_logs_title) diff --git a/android/app/src/main/java/com/metallic/chiaki/settings/SettingsLogsViewModel.kt b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsLogsViewModel.kt index eb6da95..c2c2a53 100644 --- a/android/app/src/main/java/com/metallic/chiaki/settings/SettingsLogsViewModel.kt +++ b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsLogsViewModel.kt @@ -17,14 +17,31 @@ package com.metallic.chiaki.settings +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import com.metallic.chiaki.common.AppDatabase +import com.metallic.chiaki.common.LogFile +import com.metallic.chiaki.common.LogManager import com.metallic.chiaki.common.RegisteredHost import com.metallic.chiaki.common.ext.toLiveData import io.reactivex.disposables.CompositeDisposable import io.reactivex.rxkotlin.addTo import io.reactivex.schedulers.Schedulers -class SettingsLogsViewModel: ViewModel() +class SettingsLogsViewModel(val logManager: LogManager): ViewModel() { + private val _sessionLogs = MutableLiveData>(logManager.files) + val sessionLogs: LiveData> get() = _sessionLogs + + private fun updateLogs() + { + _sessionLogs.value = logManager.files + } + + fun deleteLog(file: LogFile) + { + file.file.delete() + updateLogs() + } } \ No newline at end of file diff --git a/android/app/src/main/res/layout/fragment_settings_logs.xml b/android/app/src/main/res/layout/fragment_settings_logs.xml index 30f64c7..e35072c 100644 --- a/android/app/src/main/res/layout/fragment_settings_logs.xml +++ b/android/app/src/main/res/layout/fragment_settings_logs.xml @@ -1,8 +1,8 @@ - + + + + + + diff --git a/android/app/src/main/res/layout/item_log_file.xml b/android/app/src/main/res/layout/item_log_file.xml index 4fcce3f..894685e 100644 --- a/android/app/src/main/res/layout/item_log_file.xml +++ b/android/app/src/main/res/layout/item_log_file.xml @@ -25,7 +25,7 @@ + tools:text="Name" + app:layout_constraintEnd_toStartOf="@id/shareButton" + app:layout_constraintBottom_toTopOf="@id/summaryTextView" + app:layout_constraintVertical_chainStyle="packed"/> - + tools:text="Summary" + app:layout_constraintEnd_toEndOf="@id/nameTextView" /> + + diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xml index 2316ce2..963a2d5 100644 --- a/android/app/src/main/res/values/strings.xml +++ b/android/app/src/main/res/values/strings.xml @@ -74,6 +74,7 @@ Keep Delete Edit + No Session Logs recorded. discovery_enabled