Skip to content

Commit

Permalink
Implemented customizable time format for settings
Browse files Browse the repository at this point in the history
  • Loading branch information
zhufucdev committed Mar 1, 2023
1 parent e680c8d commit 610bc71
Show file tree
Hide file tree
Showing 22 changed files with 134 additions and 40 deletions.
1 change: 1 addition & 0 deletions .idea/dictionaries/zhufu.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 21 additions & 3 deletions app/src/main/java/com/zhufucdev/motion_emulator/Utility.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.zhufucdev.motion_emulator

import android.app.Activity
import android.content.Context
import android.content.SharedPreferences
import android.content.pm.ApplicationInfo
import android.content.res.Configuration
import android.content.res.Resources
Expand All @@ -26,10 +27,12 @@ import io.ktor.client.request.*
import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
import java.math.RoundingMode
import java.text.DateFormat
import java.text.DecimalFormat
import java.text.SimpleDateFormat
import java.util.*
import kotlin.math.*
import kotlin.math.pow
import kotlin.math.sqrt

fun getAttrColor(@AttrRes id: Int, context: Context): Int {
val typedValue = TypedValue()
Expand Down Expand Up @@ -76,8 +79,23 @@ fun Float.toFixed(n: Int): String {
return df.format(this)
}

fun dateString(time: Long = System.currentTimeMillis()): String =
SimpleDateFormat.getDateTimeInstance().format(Date(time))
fun DateFormat.dateString(time: Long = System.currentTimeMillis()): String =
format(Date(time))

fun SharedPreferences.effectiveTimeFormat(): DateFormat {
val useCustom = getBoolean("customize_time_format", false)
return if (useCustom) {
val format = getString("time_format", "dd-MM-yyyy hh:mm:ss")
SimpleDateFormat(format, Locale.getDefault())
} else {
SimpleDateFormat.getDateTimeInstance()
}
}

fun Context.effectiveTimeFormat(): DateFormat {
val preferences by lazySharedPreferences()
return preferences.effectiveTimeFormat()
}

fun isDarkModeEnabled(resources: Resources) =
resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/zhufucdev/motion_emulator/data/Data.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.zhufucdev.motion_emulator.data

import java.io.OutputStream
import java.text.DateFormat

/**
* Represents something that can be referred
* with barely a string ID
*/
interface Data {
val id: String
val displayName: String
fun getDisplayName(format: DateFormat): String
fun writeTo(stream: OutputStream)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import kotlinx.serialization.json.Json
import kotlinx.serialization.json.encodeToStream
import kotlinx.serialization.serializer
import java.io.OutputStream
import java.text.DateFormat

/**
* Basic motion record unit
Expand All @@ -33,8 +34,8 @@ data class Motion(
val moments: List<MotionMoment>,
val sensorsInvolved: List<Int>
) : Data {
override val displayName: String
get() = name.takeIf { !it.isNullOrEmpty() } ?: dateString(time)
override fun getDisplayName(format: DateFormat): String =
name.takeIf { !it.isNullOrEmpty() } ?: format.dateString(time)

@OptIn(ExperimentalSerializationApi::class)
override fun writeTo(stream: OutputStream) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import kotlinx.serialization.encoding.CompositeDecoder.Companion.DECODE_DONE
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.encodeToStream
import java.io.OutputStream
import java.text.DateFormat

/**
* A snapshot taken from [TelephonyManager]
Expand All @@ -39,8 +40,8 @@ data class CellTimeline(
val time: Long,
val moments: List<CellMoment>
) : Data {
override val displayName: String
get() = name.takeIf { !it.isNullOrEmpty() } ?: dateString(time)
override fun getDisplayName(format: DateFormat): String =
name.takeIf { !it.isNullOrEmpty() } ?: format.dateString(time)

@OptIn(ExperimentalSerializationApi::class)
override fun writeTo(stream: OutputStream) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import kotlinx.serialization.json.Json
import kotlinx.serialization.json.encodeToStream
import kotlinx.serialization.serializer
import java.io.OutputStream
import java.text.DateFormat

/**
* A location on the Earth. Get it?
Expand Down Expand Up @@ -74,7 +75,7 @@ data class Trace(
}
}

override val displayName: String get() = name
override fun getDisplayName(format: DateFormat): String = name

@OptIn(ExperimentalSerializationApi::class)
override fun writeTo(stream: OutputStream) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package com.zhufucdev.motion_emulator.ui
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentTransaction.TRANSIT_FRAGMENT_FADE
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.preference.EditTextPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import com.google.android.material.appbar.MaterialToolbar
import androidx.preference.SwitchPreferenceCompat
import com.zhufucdev.motion_emulator.R
import com.zhufucdev.motion_emulator.databinding.ActivitySettingsBinding
import com.zhufucdev.motion_emulator.initializeToolbar

private const val TITLE_TAG = "settingsActivityTitle"

Expand Down Expand Up @@ -97,6 +95,17 @@ class SettingsActivity : AppCompatActivity(),
class NamingFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.naming_preferences, rootKey)
init()
}

private fun init() {
val customTimeFormatSwitch = findPreference<SwitchPreferenceCompat>("customize_time_format")!!
val timeFormat = findPreference<EditTextPreference>("time_format")!!
timeFormat.isEnabled = customTimeFormatSwitch.isChecked
customTimeFormatSwitch.setOnPreferenceChangeListener { _, use ->
timeFormat.isEnabled = use as Boolean
true
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ package com.zhufucdev.motion_emulator.ui.emulate
import android.content.Context
import android.os.Bundle
import android.view.*
import androidx.fragment.app.Fragment
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import androidx.core.os.bundleOf
import androidx.core.view.MenuProvider
import androidx.core.widget.doAfterTextChanged
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.navigation.fragment.findNavController
import com.amap.api.maps.AMap
import com.amap.api.maps.AMapUtils
import com.amap.api.maps.CameraUpdateFactory
import com.amap.api.maps.model.*
import com.amap.api.maps.model.Marker
import com.amap.api.maps.model.MarkerOptions
import com.amap.api.maps.model.Polyline
import com.amap.api.maps.model.PolylineOptions
import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
import com.google.android.material.snackbar.Snackbar
import com.google.android.material.textfield.TextInputEditText
Expand Down Expand Up @@ -51,6 +53,7 @@ class ConfigurationFragment : Fragment(), MenuProvider {
}
}
private val defaultPreferences by requireContext().lazySharedPreferences()
private val dateFormat by lazy { defaultPreferences.effectiveTimeFormat() }

private var motion: Box<Motion> = EmptyBox()
private var trace: Trace? = null
Expand Down Expand Up @@ -136,7 +139,7 @@ class ConfigurationFragment : Fragment(), MenuProvider {
val adapter = ArrayAdapter<String>(requireContext(), android.R.layout.simple_list_item_1)
val motions = Motions.list()
motions.forEach {
adapter.add(it.displayName)
adapter.add(it.getDisplayName(dateFormat))
}
adapter.addDefaults()
binding.dropdownMotion.apply {
Expand All @@ -158,7 +161,7 @@ class ConfigurationFragment : Fragment(), MenuProvider {
}
val m = motions.firstOrNull { it.id == id }
if (m != null) {
select(adapter, m.displayName)
select(adapter, m.getDisplayName(dateFormat))
motion = m.box()
}
} ?: apply {
Expand All @@ -171,7 +174,7 @@ class ConfigurationFragment : Fragment(), MenuProvider {
val adapter = ArrayAdapter<String>(requireContext(), android.R.layout.simple_list_item_1)
val timelines = Cells.list()
timelines.forEach {
adapter.add(it.displayName)
adapter.add(it.getDisplayName(dateFormat))
}
adapter.addDefaults()
binding.dropdownCells.apply {
Expand All @@ -193,7 +196,7 @@ class ConfigurationFragment : Fragment(), MenuProvider {
}
val timeline = timelines.firstOrNull { it.id == id }
if (timeline != null) {
select(adapter, timeline.displayName)
select(adapter, timeline.getDisplayName(dateFormat))
cells = timeline.box()
}
} ?: apply {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@file:OptIn(ExperimentalMaterial3Api::class)

package com.zhufucdev.motion_emulator.ui.home

import androidx.compose.foundation.layout.*
Expand Down Expand Up @@ -97,6 +95,7 @@ fun AppHome(activatedState: State<Boolean>, onClick: (AppHomeDestination) -> Uni
}
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun HomeCard(
onClick: () -> Unit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import com.zhufucdev.motion_emulator.R
import com.zhufucdev.motion_emulator.data.CellTimeline
import com.zhufucdev.motion_emulator.effectiveTimeFormat
import com.zhufucdev.motion_emulator.ui.theme.paddingCommon

@Composable
fun CellEditor(target: CellTimeline, viewModel: EditorViewModel<CellTimeline>) {
val context = LocalContext.current
val formatter = remember { context.effectiveTimeFormat() }

Box(Modifier.padding(paddingCommon)) {
BasicEdit(
id = target.id,
name = target.displayName,
name = target.getDisplayName(formatter),
onNameChanged = {
viewModel.onModify(target.copy(name = it))
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,24 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import com.zhufucdev.motion_emulator.R
import com.zhufucdev.motion_emulator.data.CellTimeline
import com.zhufucdev.motion_emulator.effectiveTimeFormat
import com.zhufucdev.motion_emulator.ui.theme.paddingCard
import com.zhufucdev.motion_emulator.ui.theme.paddingSmall

@Composable
fun CellScreen(viewModel: EditorViewModel<CellTimeline>) {
val context = LocalContext.current
val formatter = remember { context.effectiveTimeFormat() }

DataList(viewModel) {
Column(Modifier.padding(paddingCard)) {
Text(text = it.displayName, style = MaterialTheme.typography.titleMedium)
Text(text = it.getDisplayName(formatter), style = MaterialTheme.typography.titleMedium)
Spacer(modifier = Modifier.height(paddingSmall))
Text(
text = stringResource(R.string.text_pieces_of_record, it.moments.size),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import com.zhufucdev.motion_emulator.R
import com.zhufucdev.motion_emulator.data.Motion
import com.zhufucdev.motion_emulator.effectiveTimeFormat
import com.zhufucdev.motion_emulator.ui.theme.paddingCommon

@Composable
fun MotionEditor(target: Motion, viewModel: EditorViewModel<Motion>) {
val context = LocalContext.current
val formatter = remember { context.effectiveTimeFormat() }

Box(Modifier.padding(paddingCommon)) {
BasicEdit(
id = target.id,
name = target.displayName,
name = target.getDisplayName(formatter),
onNameChanged = {
viewModel.onModify(target.copy(name = it))
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import com.aventrix.jnanoid.jnanoid.NanoIdUtils
import com.zhufucdev.motion_emulator.R
import com.zhufucdev.motion_emulator.data.Motion
import com.zhufucdev.motion_emulator.effectiveTimeFormat
import com.zhufucdev.motion_emulator.hook.estimateSpeed
import com.zhufucdev.motion_emulator.hook.estimateTimespan
import com.zhufucdev.motion_emulator.toFixed
Expand All @@ -25,9 +28,12 @@ import kotlin.time.DurationUnit

@Composable
fun MotionScreen(viewModel: EditorViewModel<Motion>) {
val context = LocalContext.current
val formatter = remember { context.effectiveTimeFormat() }

DataList(viewModel) {
Column(Modifier.padding(paddingCard)) {
Text(text = it.displayName, style = MaterialTheme.typography.titleMedium)
Text(text = it.getDisplayName(formatter), style = MaterialTheme.typography.titleMedium)
Spacer(modifier = Modifier.height(paddingSmall))
Text(
text = stringResource(
Expand Down
Loading

0 comments on commit 610bc71

Please sign in to comment.