Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tools layout redesign #2047

Merged
merged 16 commits into from
Nov 11, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.kylecorry.trail_sense.experimentation

import android.view.LayoutInflater
import android.view.ViewGroup
import com.kylecorry.andromeda.fragments.BoundFragment
import com.kylecorry.trail_sense.databinding.FragmentExperimentationBinding

class ExperimentationFragment : BoundFragment<FragmentExperimentationBinding>() {

override fun generateBinding(
layoutInflater: LayoutInflater, container: ViewGroup?
): FragmentExperimentationBinding {
return FragmentExperimentationBinding.inflate(layoutInflater, container, false)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.kylecorry.trail_sense.quickactions

import android.widget.ImageButton
import androidx.core.view.setMargins
import com.google.android.flexbox.FlexboxLayout
import com.kylecorry.andromeda.core.system.Resources
import com.kylecorry.andromeda.fragments.AndromedaFragment
import com.kylecorry.trail_sense.R
import com.kylecorry.trail_sense.databinding.FragmentToolsBinding

class ToolsQuickActionBinder(
private val fragment: AndromedaFragment,
private val binding: FragmentToolsBinding
) : IQuickActionBinder {

private fun createButton(): ImageButton {
val size = Resources.dp(fragment.requireContext(), 40f).toInt()
val margins = Resources.dp(fragment.requireContext(), 8f).toInt()
val button = ImageButton(fragment.requireContext())
button.layoutParams = FlexboxLayout.LayoutParams(size, size).apply {
setMargins(margins)
}
button.background =
Resources.drawable(fragment.requireContext(), R.drawable.rounded_rectangle)
button.elevation = 2f

binding.quickActions.addView(button)

return button
}

override fun bind() {
binding.quickActions.removeAllViews()
// TODO: Weather monitor
// TODO: Backtrack quick action should be a toggle rather than opening the path
// QuickActionBacktrack(createButton(), fragment).bind(fragment.viewLifecycleOwner)
QuickActionFlashlight(createButton(), fragment).bind(fragment.viewLifecycleOwner)
QuickActionWhistle(createButton(), fragment).bind(fragment.viewLifecycleOwner)
LowPowerQuickAction(createButton(), fragment).bind(fragment.viewLifecycleOwner)
QuickActionSunsetAlert(createButton(), fragment).bind(fragment.viewLifecycleOwner)
QuickActionWhiteNoise(createButton(), fragment).bind(fragment.viewLifecycleOwner)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,19 @@ object CustomUiUtils {
}
}

fun oneTimeToast(
context: Context,
message: String,
shownKey: String,
short: Boolean = true
){
val prefs = PreferencesSubsystem.getInstance(context).preferences
if (prefs.getBoolean(shownKey) != true) {
Alerts.toast(context, message, short)
prefs.putBoolean(shownKey, true)
}
}

fun disclaimer(
context: Context,
title: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.kylecorry.trail_sense.tools.ui

import com.kylecorry.andromeda.preferences.IPreferences

class PinnedToolManager(private val prefs: IPreferences) {

private val pinned = mutableSetOf<Long>()
private val lock = Any()

private val key = "pinned_tools"

init {
// TODO: Listen for changes
val all = readPrefs()
synchronized(lock) {
pinned.clear()
pinned.addAll(all)
}
}

fun getPinnedToolIds(): List<Long> {
return synchronized(lock) {
pinned.toList()
}
}

fun setPinnedToolIds(toolIds: List<Long>) {
synchronized(lock) {
pinned.clear()
pinned.addAll(toolIds)
}
writePrefs(getPinnedToolIds())
}

fun pin(toolId: Long) {
synchronized(lock) {
pinned.add(toolId)
}
writePrefs(getPinnedToolIds())
}

fun unpin(toolId: Long) {
synchronized(lock) {
pinned.remove(toolId)
}
writePrefs(getPinnedToolIds())
}

fun isPinned(toolId: Long): Boolean {
return synchronized(lock) {
pinned.contains(toolId)
}
}

private fun readPrefs(): List<Long> {
val str = prefs.getString(key) ?: return listOf(
6L, // Navigation
20L, // Weather
14L // Astronomy
)
return str.split(",").mapNotNull { it.toLongOrNull() }
}

private fun writePrefs(toolIds: List<Long>) {
prefs.putString(key, toolIds.joinToString(","))
}

}
Loading
Loading