Skip to content

Commit

Permalink
Update Alpine, Tailwind
Browse files Browse the repository at this point in the history
Fixes #761
  • Loading branch information
mtotschnig committed Jan 27, 2025
1 parent 68674aa commit ef10e9c
Show file tree
Hide file tree
Showing 10 changed files with 1,703 additions and 1,922 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.content.ActivityNotFoundException
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import android.text.format.Formatter
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.res.ResourcesCompat
import androidx.preference.*
Expand All @@ -18,7 +17,6 @@ import org.totschnig.myexpenses.preference.PopupMenuPreference
import org.totschnig.myexpenses.preference.PrefKey
import org.totschnig.myexpenses.util.AppDirHelper
import org.totschnig.myexpenses.viewmodel.SettingsViewModel
import timber.log.Timber
import java.util.Locale

class MainPreferenceFragment : BasePreferenceFragment(),
Expand Down Expand Up @@ -110,15 +108,8 @@ class MainPreferenceFragment : BasePreferenceFragment(),
isVisible = false
} else {
isVisible = true
entries = it.map {
"${it.first} (${
Formatter.formatFileSize(
requireContext(),
it.second
)
})"
}.toTypedArray()
entryValues = it.map { it.first }.toTypedArray()
entries = it.map { it.format(requireContext()) }.toTypedArray()
entryValues = it.map { it.name }.toTypedArray()
}
}
}
Expand Down Expand Up @@ -164,7 +155,6 @@ class MainPreferenceFragment : BasePreferenceFragment(),
AppDirHelper.ensureContentUri(it, requireContext())
}
})
Timber.d("ATTACHMENTS" + arrayList.joinToString())
putParcelableArrayListExtra(
Intent.EXTRA_STREAM,
arrayList
Expand Down Expand Up @@ -253,7 +243,7 @@ class MainPreferenceFragment : BasePreferenceFragment(),
private fun pickAppDir(appDirInfo: SettingsViewModel.AppDirInfo?) {
try {
pickFolder.launch(appDirInfo?.documentFile?.uri)
} catch (e: ActivityNotFoundException) {
} catch (_: ActivityNotFoundException) {
preferenceActivity.showSnackBar(
"No activity found for picking application directory."
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package org.totschnig.myexpenses.util
import android.content.Context
import android.net.Uri
import android.os.Build
import android.text.format.Formatter
import androidx.annotation.WorkerThread
import androidx.core.content.FileProvider
import androidx.documentfile.provider.DocumentFile
import org.totschnig.myexpenses.R
Expand All @@ -16,18 +18,32 @@ import java.text.SimpleDateFormat
import java.util.*
import kotlin.Result

data class FileInfo(val name: String, val size: Long) {
fun format(context: Context) = "$name (${Formatter.formatFileSize(context, size)})"
}

object AppDirHelper {
/**
* @return the directory user has configured in the settings, if not configured yet
* returns [android.content.ContextWrapper.getExternalFilesDir] with argument null
*/
@JvmStatic
fun getAppDir(context: Context): Result<DocumentFile> =
getAppDirFromPref(context).mapCatching {
it ?: getDefaultAppDir(context)
}.onFailure { CrashHandler.report(it) }

fun getAppDirLegacy(context: Context) = getAppDir(context).getOrNull()
@WorkerThread
fun getAppDirFiles(context: Context): Result<List<FileInfo>> = getAppDir(context).map {
it.listFiles()
.filter { (it.length() > 0) && !it.isDirectory }
.mapNotNull { file ->
file.name?.let {
Triple(it, file.length(), file.lastModified())
}
}
.sortedByDescending { it.third }
.map { FileInfo(it.first, it.second) }
}

fun getAppDirWithDefault(context: Context): Result<Pair<DocumentFile, Boolean>> =
getAppDirFromPref(context).mapCatching {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import org.totschnig.myexpenses.provider.TransactionProvider
import org.totschnig.myexpenses.provider.asSequence
import org.totschnig.myexpenses.provider.filter.Operation
import org.totschnig.myexpenses.util.AppDirHelper
import org.totschnig.myexpenses.util.FileInfo
import org.totschnig.myexpenses.util.ICurrencyFormatter
import org.totschnig.myexpenses.util.Utils
import org.totschnig.myexpenses.util.convAmount
Expand All @@ -59,8 +60,8 @@ class SettingsViewModel(
private val _appDirInfo: MutableLiveData<Result<AppDirInfo>> = MutableLiveData()
val appDirInfo: LiveData<Result<AppDirInfo>> = _appDirInfo

private val _appData: MutableLiveData<List<Pair<String, Long>>> = MutableLiveData()
val appData: LiveData<List<Pair<String, Long>>> = _appData
private val _appData: MutableLiveData<List<FileInfo>> = MutableLiveData()
val appData: LiveData<List<FileInfo>> = _appData

val hasStaleImages: Flow<Boolean>
get() = contentResolver.observeQuery(
Expand All @@ -82,18 +83,8 @@ class SettingsViewModel(

fun loadAppData() {
viewModelScope.launch(coroutineContext()) {
AppDirHelper.getAppDir(getApplication()).onSuccess { dir ->
_appData.postValue(
dir.listFiles()
.filter { (it.length() > 0) && !it.isDirectory }
.mapNotNull { file ->
file.name?.let {
Triple(it, file.length(), file.lastModified())
}
}
.sortedByDescending { it.third }
.map { it.first to it.second }
)
AppDirHelper.getAppDirFiles(getApplication()).onSuccess { list ->
_appData.postValue(list)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions myExpenses/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1047,4 +1047,5 @@
<string name="budget_import_can_be_imported">The following budgets can be imported:</string>
<string name="budget_import_cannot_be_imported">The following budgets refer to accounts that are not synchronized:</string>
<string name="budget_import_none_found">No budgets found.</string>
<string name="webui_download_no_data">After printing, exporting or creating backups in the app, files will be available from here for download.</string>
</resources>
71 changes: 71 additions & 0 deletions webui/src/main/assets/download.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Download</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
line-height: 1.6;
}
.container {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 20px;
}
.card {
border: 1px solid #ddd;
border-radius: 10px;
padding: 10px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
transition: transform 0.2s;
}
.card:hover {
transform: scale(1.05);
}
</style>
</head>
<body>
<div class="container" id="dataContainer"></div>

<p id="emptyState" class="empty-state" style="display: none;">${no_results}</p>

<template id="cardTemplate">
<div class="card">
<a class="file"></a>
</div>
</template>

<script>
// Data array
const data = ${data};

// References
const container = document.getElementById("dataContainer");
const emptyStateMessage = document.getElementById("emptyState");
const template = document.getElementById("cardTemplate");

// Populate the container with data

if (data.length === 0) {
emptyStateMessage.style.display = "block";
} else {

data.forEach(item => {
// Clone the template
const card = template.content.cloneNode(true);

// Update the template with data
const a = card.querySelector(".file")
a.textContent = item.name;
a.href = item.link;

// Append to the container
container.appendChild(card);
});
}
</script>
</body>
</html>
3 changes: 2 additions & 1 deletion webui/src/main/assets/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<link rel="icon" href="/favicon.ico">

<script defer src="https://unpkg.com/alpinejs@3.10.2/dist/cdn.min.js"></script>
<script defer src="https://unpkg.com/alpinejs@3.14.8/dist/cdn.min.js"></script>
<script src="data.js"></script>
<script src="messages.js"></script>
</head>
Expand Down Expand Up @@ -168,6 +168,7 @@
</div>

<div class="relative">
<a class="absolute bottom-0 left-0" href="download" x-text="messages.action_download">Download</a>
<button class="block bg-green-500 hover:bg-green-700 disabled:opacity-50 text-white uppercase text-lg mx-auto p-4 rounded"
type="button" :disabled="amount == ''" @click="submitForm"><span
x-text="messages.menu_save"></span><br>
Expand Down
Loading

0 comments on commit ef10e9c

Please sign in to comment.