Skip to content

Commit

Permalink
Convert preferences screen to AndroidX API (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
dozingcat authored Sep 7, 2020
1 parent 0c8f10b commit ad95ac7
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 40 deletions.
4 changes: 2 additions & 2 deletions app/src/main/assets/about.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8">
<style>
html, body {
background: black;
background: #222;
color: #ddd;
}

Expand Down Expand Up @@ -100,7 +100,7 @@ <h3>
<img src="img/ic_settings_white_36px.svg">
<div class="desc">
Open the settings screen where you can set options of text effects, and enable
automatically creating images that you take with the standard camera app.
automatically importing images that you take with the standard camera app.
</div>
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import java.io.FileOutputStream
import android.view.*
import android.widget.FrameLayout
import android.widget.LinearLayout
import androidx.fragment.app.FragmentActivity
import androidx.appcompat.app.AppCompatActivity
import com.dozingcatsoftware.vectorcamera.effect.*


enum class ShutterMode {IMAGE, VIDEO}

class MainActivity : FragmentActivity() {
class MainActivity : AppCompatActivity() {

private val handler = Handler()
private lateinit var cameraSelector: CameraSelector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,46 @@ import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.preference.PreferenceActivity

class VCPreferencesActivity : PreferenceActivity() {
val handler = Handler()
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceFragmentCompat

class VCPreferencesActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
addPreferencesFromResource(R.xml.preferences)
// Use a layout that has only a FrameLayout that we replace with the preferences fragment.
setContentView(R.layout.preferences_layout)
supportFragmentManager
.beginTransaction()
.replace(R.id.prefs_main, VCPreferencesFragment())
.commit()
}

companion object {
// sets FLAG_ACTIVITY_NO_HISTORY so exiting and relaunching won't go back to this screen
fun startIntent(parent: Activity): Intent {
val prefsIntent = Intent(parent, VCPreferencesActivity::class.java)
prefsIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
parent.startActivity(prefsIntent)
return prefsIntent
}
}
}

class VCPreferencesFragment : PreferenceFragmentCompat() {
private val handler = Handler()

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preferences, rootKey)
val pm = this.preferenceManager

val autoConvertPref = pm.findPreference(getString(R.string.autoConvertPicturesPrefsKey))
autoConvertPref!!.setOnPreferenceChangeListener({pref, value ->
autoConvertPref!!.setOnPreferenceChangeListener { pref, value ->
// Update broadcast receivers immediately so the change takes effect even if the user
// doesn't go back to the main activity.
setAutoConvertEnabled(this@VCPreferencesActivity, java.lang.Boolean.TRUE == value)
setAutoConvertEnabled(
this@VCPreferencesFragment.context!!, java.lang.Boolean.TRUE == value)
true
})
}

// HACK: when the user updates the character set of an ASCII effect, update the current
// effect if it matches. This is so when they return to the main activity the effect will
Expand All @@ -35,9 +58,9 @@ class VCPreferencesActivity : PreferenceActivity() {
getString(R.string.ansiColorPixelCharsPrefId),
getString(R.string.fullColorPixelCharsPrefId))
for (prefId in asciiPrefIds) {
pm.findPreference(prefId).setOnPreferenceChangeListener({pref, value ->
handler.post({
val storedPrefs = VCPreferences(this)
pm.findPreference(prefId).setOnPreferenceChangeListener { pref, value ->
handler.post {
val storedPrefs = VCPreferences(this.context!!)
if (storedPrefs.effectName() == "ascii") {
val params = storedPrefs.effectParameters()
if (params["prefId"] == prefId) {
Expand All @@ -46,38 +69,38 @@ class VCPreferencesActivity : PreferenceActivity() {
storedPrefs.saveEffectInfo("ascii", newEffectParams)
}
}
})
}
true
})
}
}
// Same for number of characters.
val textEffectNames = setOf("ascii", "matrix")
val numColumnsPrefId = getString(R.string.numAsciiColumnsPrefId)
pm.findPreference(numColumnsPrefId).setOnPreferenceChangeListener({pref, value ->
handler.post({
val storedPrefs = VCPreferences(this)
pm.findPreference(numColumnsPrefId).setOnPreferenceChangeListener { pref, value ->
handler.post {
val storedPrefs = VCPreferences(this.context!!)
if (textEffectNames.contains(storedPrefs.effectName())) {
val params = storedPrefs.effectParameters()
val newEffectParams = HashMap(params)
newEffectParams["numColumns"] = Integer.parseInt(value as String)
storedPrefs.saveEffectInfo(storedPrefs.effectName(), newEffectParams)
}
})
}
true
})
}
// And update matrix text color.
val matrixColorPrefId = getString(R.string.matrixTextColorPrefId)
pm.findPreference(matrixColorPrefId).setOnPreferenceChangeListener({pref, value ->
handler.post({
val storedPrefs = VCPreferences(this)
pm.findPreference(matrixColorPrefId).setOnPreferenceChangeListener { pref, value ->
handler.post {
val storedPrefs = VCPreferences(this.context!!)
if (storedPrefs.effectName() == "matrix") {
val params = storedPrefs.effectParameters()
val newEffectParams = HashMap(params)
newEffectParams["textColor"] = value
storedPrefs.saveEffectInfo(storedPrefs.effectName(), newEffectParams)
}
})
})
}
}
}

/**
Expand All @@ -103,14 +126,4 @@ class VCPreferencesActivity : PreferenceActivity() {
PackageManager.DONT_KILL_APP)
}
}

companion object {
// sets FLAG_ACTIVITY_NO_HISTORY so exiting and relaunching won't go back to this screen
fun startIntent(parent: Activity): Intent {
val prefsIntent = Intent(parent, VCPreferencesActivity::class.java)
prefsIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
parent.startActivity(prefsIntent)
return prefsIntent
}
}
}
7 changes: 7 additions & 0 deletions app/src/main/res/layout/preferences_layout.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:key="wtf"
android:id="@+id/prefs_main">

</FrameLayout>
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<string name="noImagesFound">No pictures found.</string>

<string name="autoConvertPicturesPrefsKey">autoConvertPictures</string>
<string name="autoConvertPicturesPrefsTitle">Automatically convert camera pictures</string>
<string name="autoConvertPicturesPrefsTitle">Auto-import camera pictures</string>

<string name="numAsciiColumnsPrefId">numAsciiColumns</string>
<string name="numAsciiColumnsPrefTitle">Number of text columns</string>
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<PreferenceCategory android:title="Preferences">
<PreferenceCategory android:title="Preferences" app:iconSpaceReserved="false">
<CheckBoxPreference android:key="@string/autoConvertPicturesPrefsKey"
android:enabled="true"
android:title="@string/autoConvertPicturesPrefsTitle" />
Expand Down Expand Up @@ -35,7 +35,7 @@
android:enabled="true"
android:title="@string/fullColorPixelCharsPrefTitle"
android:dialogMessage="@string/fullColorPixelCharsPrefMessage" />
<com.jaredrummler.android.colorpicker.ColorPreference
<com.jaredrummler.android.colorpicker.ColorPreferenceCompat
android:key="@string/matrixTextColorPrefId"
android:title="@string/matrixTextColorPrefTitle"
android:defaultValue="@color/matrixDefaultColor"
Expand Down

0 comments on commit ad95ac7

Please sign in to comment.