-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It allows switching between light and dark modes, specifying step width, and step data import/export (closes #70).
- Loading branch information
Showing
8 changed files
with
221 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
app/src/main/java/com/tiefensuche/motionmate/ui/SettingsActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* SPDX-License-Identifier: GPL-3.0-only | ||
*/ | ||
|
||
package com.tiefensuche.motionmate.ui | ||
|
||
import android.app.Activity | ||
import android.content.Intent | ||
import android.net.Uri | ||
import android.os.Bundle | ||
import android.widget.Toast | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.preference.ListPreference | ||
import androidx.preference.Preference | ||
import androidx.preference.PreferenceFragmentCompat | ||
import androidx.preference.SeekBarPreference | ||
import com.tiefensuche.motionmate.R | ||
import com.tiefensuche.motionmate.util.Database | ||
import com.tiefensuche.motionmate.util.Util | ||
import java.io.FileInputStream | ||
import java.io.FileOutputStream | ||
import java.lang.Exception | ||
|
||
class SettingsActivity : AppCompatActivity() { | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.settings_activity) | ||
if (savedInstanceState == null) { | ||
supportFragmentManager | ||
.beginTransaction() | ||
.replace(R.id.settings, SettingsFragment()) | ||
.commit() | ||
} | ||
supportActionBar?.setDisplayHomeAsUpEnabled(true) | ||
} | ||
|
||
class SettingsFragment : PreferenceFragmentCompat() { | ||
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { | ||
setPreferencesFromResource(R.xml.root_preferences, rootKey) | ||
findPreference<SeekBarPreference>("step_width")?.setOnPreferenceChangeListener { _, newValue -> | ||
Util.stepWidth = newValue as Int | ||
true | ||
} | ||
findPreference<ListPreference>("theme")?.setOnPreferenceChangeListener { _, newValue -> | ||
Util.applyTheme(newValue.toString()) | ||
true | ||
} | ||
findPreference<Preference>("import")?.setOnPreferenceClickListener { | ||
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply { | ||
addCategory(Intent.CATEGORY_OPENABLE) | ||
type = "text/*" | ||
} | ||
startActivityForResult(intent, 1) | ||
true | ||
} | ||
findPreference<Preference>("export")?.setOnPreferenceClickListener { | ||
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply { | ||
addCategory(Intent.CATEGORY_OPENABLE) | ||
type = "text/*" | ||
putExtra(Intent.EXTRA_TITLE, "step_data.csv") | ||
} | ||
startActivityForResult(intent, 2) | ||
true | ||
} | ||
} | ||
|
||
override fun onActivityResult( | ||
requestCode: Int, resultCode: Int, resultData: Intent?) { | ||
super.onActivityResult(requestCode, resultCode, resultData) | ||
if (resultCode != Activity.RESULT_OK) | ||
return | ||
resultData?.data?.also { uri -> | ||
when (requestCode) { | ||
1 -> import(uri) | ||
2 -> export(uri) | ||
} | ||
} | ||
} | ||
|
||
private fun import(uri: Uri) { | ||
val db = Database.getInstance(requireContext()) | ||
try { | ||
requireContext().contentResolver.openFileDescriptor(uri, "r")?.use { | ||
FileInputStream(it.fileDescriptor).bufferedReader().use { | ||
var entries = 0 | ||
var failed = 0 | ||
for (line in it.readLines()) { | ||
entries += 1 | ||
try { | ||
val split = line.split(",") | ||
db.addEntry(split[0].toLong(), split[1].toInt()) | ||
} catch (ex: Exception) { | ||
println("Can not import entry, ${ex.message}") | ||
failed += 1 | ||
} | ||
} | ||
Toast.makeText(context, "Imported ${entries - failed} entries ($failed failed).", Toast.LENGTH_LONG).show() | ||
} | ||
} | ||
} catch (ex: Exception) { | ||
println("Can not open file, ${ex.message}") | ||
Toast.makeText(context, "Can not open file", Toast.LENGTH_LONG).show() | ||
} | ||
} | ||
|
||
private fun export(uri: Uri) { | ||
val db = Database.getInstance(requireContext()) | ||
try { | ||
requireContext().contentResolver.openFileDescriptor(uri, "w")?.use { | ||
FileOutputStream(it.fileDescriptor).bufferedWriter().use { | ||
var entries = 0 | ||
for (entry in db.getEntries(db.firstEntry, db.lastEntry)) { | ||
it.write("${entry.timestamp},${entry.steps}\r\n") | ||
entries += 1 | ||
} | ||
Toast.makeText(context, "Exported $entries entries.", Toast.LENGTH_LONG).show() | ||
} | ||
} | ||
} catch (ex: Exception) { | ||
println("Can not open file, ${ex.message}") | ||
Toast.makeText(context, "Can not open file", Toast.LENGTH_LONG).show() | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!-- | ||
~ SPDX-License-Identifier: GPL-3.0-only | ||
--> | ||
|
||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
<FrameLayout | ||
android:id="@+id/settings" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" /> | ||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!-- | ||
~ SPDX-License-Identifier: GPL-3.0-only | ||
--> | ||
|
||
<resources> | ||
<string-array name="theme_entries"> | ||
<item>System</item> | ||
<item>Light</item> | ||
<item>Dark</item> | ||
</string-array> | ||
|
||
<string-array name="theme_values"> | ||
<item>system</item> | ||
<item>light</item> | ||
<item>dark</item> | ||
</string-array> | ||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<!-- | ||
~ SPDX-License-Identifier: GPL-3.0-only | ||
--> | ||
|
||
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
<PreferenceCategory app:title="@string/step_width"> | ||
<SeekBarPreference | ||
app:min="50" | ||
android:max="120" | ||
app:defaultValue="70" | ||
app:showSeekBarValue="true" | ||
app:key="step_width" /> | ||
</PreferenceCategory> | ||
|
||
<PreferenceCategory app:title="@string/appearance"> | ||
<ListPreference | ||
app:defaultValue="system" | ||
app:entries="@array/theme_entries" | ||
app:entryValues="@array/theme_values" | ||
app:key="theme" | ||
app:title="@string/theme" | ||
app:useSimpleSummaryProvider="true" /> | ||
</PreferenceCategory> | ||
|
||
<PreferenceCategory app:title="@string/data"> | ||
<Preference | ||
app:key="import" | ||
app:title="Import" /> | ||
|
||
<Preference | ||
app:key="export" | ||
app:title="Export" /> | ||
</PreferenceCategory> | ||
|
||
</PreferenceScreen> |