Skip to content

Commit

Permalink
android app: add option to additional truncate text width
Browse files Browse the repository at this point in the history
  • Loading branch information
Toni500github committed Dec 14, 2024
1 parent dbd5fc8 commit 772f6f3
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,16 @@ private fun copyDirectory(
return

val destDir = File(destinationDir, sourceDir)
if (!destDir.exists() && !destDir.mkdirs()) {
if (!destDir.exists() && !destDir.mkdirs())
throw IOException("Failed to create directory: " + destDir.absolutePath)
}

for (fileName in files) {
val assetPath = "$sourceDir/$fileName"
val destPath = destDir.path + "/" + fileName
if (isDirectory(assetManager, assetPath)) {
if (isDirectory(assetManager, assetPath))
copyDirectory(assetManager, assetPath, destinationDir)
} else {
else
copyFile(assetManager, assetPath, destPath)
}
}
}

Expand All @@ -102,9 +100,9 @@ private fun copyFile(assetManager: AssetManager, assetPath: String, destPath: St
FileOutputStream(destPath).use { out ->
val buffer = ByteArray(8192)
var bytesRead: Int
while ((`in`.read(buffer).also { bytesRead = it }) != -1) {
while ((`in`.read(buffer).also { bytesRead = it }) != -1)
out.write(buffer, 0, bytesRead)
}

Log.d(TAG, "File copied: $destPath")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ internal fun updateAppWidget(
val textSizePx = textSizeSp * context.resources.displayMetrics.scaledDensity
textPaint.textSize = textSizePx

val width = WidgetSizeProvider(context).getWidgetsSize(appWidgetId).first.toFloat()
val additionalTruncateWidth = loadTruncateWidthPref(context, appWidgetId).toFloat()
var width = WidgetSizeProvider(context).getWidgetsSize(appWidgetId).first.toFloat()
if (additionalTruncateWidth > 0.20)
width *= additionalTruncateWidth

Log.d("widthTesting", "textSizePx = $textSizePx")
Log.d("widthTesting", "width = $width")
Log.d("wrappingTest", "disableLineWrap = $disableLineWrap")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.toni.customfetch_android.databinding.CustomfetchConfigureBinding
import java.nio.file.Files
import kotlin.io.path.Path

// truncate text
var disableLineWrap = false

/**
Expand All @@ -28,15 +29,15 @@ var disableLineWrap = false
class customfetchConfigureActivity : Activity() {
private var appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID
private lateinit var argumentsConfig: EditText
private lateinit var additionalTruncateWidth: EditText
private lateinit var argsHelp: TextView
private lateinit var showModulesList: CheckBox
private lateinit var disableWrapLines: CheckBox
private var onClickListener = View.OnClickListener {
val context = this@customfetchConfigureActivity

// When the button is clicked, store the string locally
val widgetText = argumentsConfig.text.toString()
saveTitlePref(context, appWidgetId, widgetText)
saveTitlePref(context, appWidgetId, argumentsConfig.text.toString(), additionalTruncateWidth.text.toString())

// It is the responsibility of the configuration activity to update the app widget
val appWidgetManager = AppWidgetManager.getInstance(context)
Expand Down Expand Up @@ -64,6 +65,7 @@ class customfetchConfigureActivity : Activity() {
copyToAssetFolder(assets, filesDir.absolutePath, "ascii")

argumentsConfig = binding.argumentsConfigure
additionalTruncateWidth = binding.additionalTruncateN
argsHelp = binding.argsHelp
showModulesList = binding.showModulesList
disableWrapLines = binding.disableWrapLines
Expand All @@ -84,6 +86,7 @@ class customfetchConfigureActivity : Activity() {
}

argumentsConfig.setText(loadTitlePref(this@customfetchConfigureActivity, appWidgetId))
additionalTruncateWidth.setText("0.81")
argsHelp.text = customfetchRender.mainAndroid("customfetch --help")

showModulesList.setOnCheckedChangeListener { _, isChecked ->
Expand Down Expand Up @@ -137,10 +140,11 @@ private const val PREFS_NAME = "org.toni.customfetch_android.customfetch"
private const val PREF_PREFIX_KEY = "appwidget_"

// Write the prefix to the SharedPreferences object for this widget
internal fun saveTitlePref(context: Context, appWidgetId: Int, text: String) {
internal fun saveTitlePref(context: Context, appWidgetId: Int, text: String, truncateWidth: String) {
val prefs = context.getSharedPreferences(PREFS_NAME, 0).edit()
prefs.putString(PREF_PREFIX_KEY + appWidgetId, text)
prefs.putBoolean(PREF_PREFIX_KEY + "bool_" + appWidgetId, disableLineWrap)
prefs.putString(PREF_PREFIX_KEY + "truncate_" + appWidgetId, truncateWidth)
prefs.apply()
}

Expand All @@ -153,6 +157,12 @@ internal fun loadTitlePref(context: Context, appWidgetId: Int): String {
return titleValue ?: "-D ${context.filesDir.absolutePath} -a small"
}

internal fun loadTruncateWidthPref(context: Context, appWidgetId: Int): String {
val prefs = context.getSharedPreferences(PREFS_NAME, 0)
val truncateWidthValue = prefs.getString(PREF_PREFIX_KEY + "truncate_" + appWidgetId, null)
return truncateWidthValue ?: "0"
}

internal fun deleteTitlePref(context: Context, appWidgetId: Int) {
val prefs = context.getSharedPreferences(PREFS_NAME, 0).edit()
prefs.remove(PREF_PREFIX_KEY + appWidgetId)
Expand Down
14 changes: 14 additions & 0 deletions android/app/src/main/res/layout/customfetch_configure.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@
android:labelFor="@+id/arguments_configure"
android:text="@string/disable_wrap_lines" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:labelFor="@id/arguments_configure"
android:text="@string/additional_truncate" />

<EditText
android:id="@+id/additional_truncate_n"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="0.81"
android:inputType="text" />

<Button
android:id="@+id/add_button"
android:layout_width="wrap_content"
Expand Down
1 change: 1 addition & 0 deletions android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<string name="attachment_summary_off">Only download attachments when manually requested</string>
<string name="appwidget_text">EXAMPLE</string>
<string name="configure">Configure (insert command line options)</string>
<string name="additional_truncate">Additional truncate text width (insert number bigger than 0.20, or smaller to ignore)</string>
<string name="add_widget">Add widget</string>
<string name="app_widget_description">Neofetch like program, which its focus point is the performance and customizability</string>
<string name="modules_list">Display modules list</string>
Expand Down

0 comments on commit 772f6f3

Please sign in to comment.