Skip to content

Commit

Permalink
A-Z sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
hacker1024 committed Apr 9, 2018
1 parent 4e60d1e commit 47ce5bb
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 33 deletions.
38 changes: 24 additions & 14 deletions app/src/main/java/tk/superl2/xwifi/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tk.superl2.xwifi
import android.Manifest
import android.app.AlertDialog
import android.content.Intent
import android.content.SharedPreferences
import android.content.pm.PackageManager
import android.os.AsyncTask
import android.os.Build
Expand Down Expand Up @@ -34,21 +35,25 @@ class MainActivity: AppCompatActivity() {
private val wifiEntrySSIDs = ArrayList<String>()
private lateinit var loadWifiEntriesInBackgroundTask: LoadWifiEntriesInBackground
private lateinit var qrDialog: AlertDialog
private lateinit var prefs: SharedPreferences

fun sortWifiEntries(ascending: Boolean = true, updateListView: Boolean = false) {
if (ascending) {
wifiEntries.sortBy { it.title }
wifiEntrySSIDs.sort()
} else {
wifiEntries.sortByDescending { it.title }
wifiEntrySSIDs.sortDescending()
fun sortWifiEntries(updateListView: Boolean) {
when (prefs.getString("sorting", "alphabetical")) {
"alphabetical" -> {
wifiEntries.sortWith(compareBy(String.CASE_INSENSITIVE_ORDER, { it.title }))
if (!prefs.getBoolean("sorting_order", DEFAULT_SORTING_ORDER)) wifiEntries.reverse()
}
}
if (updateListView) {
wifiEntrySSIDs.clear()
wifiEntries.mapTo(wifiEntrySSIDs) { it.title }
(wifi_ListView.adapter as ArrayAdapter<*>).notifyDataSetChanged()
}
if (updateListView) (wifi_ListView.adapter as ArrayAdapter<*>).notifyDataSetChanged()
}

override fun onCreate(savedInstanceState: Bundle?) {
PreferenceManager.setDefaultValues(this, R.xml.preferences, false)
val prefs = PreferenceManager.getDefaultSharedPreferences(this)
prefs = PreferenceManager.getDefaultSharedPreferences(this)

setThemeFromSharedPrefs(prefs)
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -146,7 +151,9 @@ class MainActivity: AppCompatActivity() {

override fun doInBackground(vararg params: Unit?) {
loadWifiEntries()
sortWifiEntries(ascending = true)
sortWifiEntries(false)
wifiEntrySSIDs.clear()
wifiEntries.mapTo(wifiEntrySSIDs) { it.title }
}

override fun onPostExecute(result: Unit?) {
Expand All @@ -158,10 +165,8 @@ class MainActivity: AppCompatActivity() {
private fun loadWifiEntries() {
Log.v(TAG, "Loading wifi entries...")
wifiEntries.clear()
wifiEntrySSIDs.clear()
try {
wifiEntries.addAll(if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) readOreoFile() else readNonOreoFile())
wifiEntries.mapTo(wifiEntrySSIDs) { it.title }
Log.v(TAG, "Wifi entries loaded.")
} catch (e: WifiUnparseableException) {
if (!::errorDialogBuilder.isInitialized) {
Expand Down Expand Up @@ -200,12 +205,17 @@ class MainActivity: AppCompatActivity() {
return true
}

override fun onOptionsItemSelected(item: MenuItem?): Boolean {
return when (item!!.itemId) {
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.settingsItem -> {
startActivity(Intent(this, SettingsActivity::class.java).putExtra("xposed", false))
true
}
R.id.sortItem -> {
prefs.edit().putBoolean("sorting_order", !prefs.getBoolean("sorting_order", DEFAULT_SORTING_ORDER)).apply()
sortWifiEntries(true)
true
}
else -> super.onOptionsItemSelected(item)
}
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/tk/superl2/xwifi/SettingsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.support.v7.app.AppCompatDelegate
import android.widget.Toast

internal const val DEFAULT_QR_CODE_RESOLUTION = "300"
internal const val DEFAULT_SORTING_ORDER = true

class SettingsActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/res/drawable/ic_compare_arrows_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<vector android:autoMirrored="true"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0"
android:width="24dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:rotation="90"
android:translateX="24">
<path
android:fillColor="?attr/textColorAlertDialogListItem"
android:pathData="M9.01,14L2,14v2h7.01v3L13,15l-3.99,-4v3zM14.99,13v-3L22,10L22,8h-7.01L14.99,5L11,9l3.99,4z" />
</group>
</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_sort_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector android:autoMirrored="true"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0"
android:width="24dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<path
android:fillColor="?attr/textColorAlertDialogListItem"
android:pathData="M3,18h6v-2L3,16v2zM3,6v2h18L21,6L3,6zM3,13h12v-2L3,11v2z" />
</vector>
8 changes: 6 additions & 2 deletions app/src/main/res/menu/menu_activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:id="@+id/sortItem"
android:icon="@drawable/ic_compare_arrows_24dp"
android:title="@string/item_sort_menu_activity_main"
app:showAsAction="ifRoom" />
<item
android:id="@+id/settingsItem"
android:checkable="false"
android:icon="@drawable/ic_settings_24dp"
android:showAsAction="ifRoom"
android:title="@string/item_settings_menu_activity_main"
app:showAsAction="ifRoom" />
app:showAsAction="never" />
</menu>
7 changes: 7 additions & 0 deletions app/src/main/res/values-v21/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,11 @@
<item>Dark</item>
<!--<item>System</item> &lt;!&ndash;Disabled for now. TODO Get system theme working. &ndash;&gt;-->
</string-array>

<string-array name="sorting">
<item>A-Z</item>
</string-array>
<string-array name="sorting_values">
<item>alphabetical</item>
</string-array>
</resources>
7 changes: 7 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,11 @@
<string-array name="themes" tools:ignore="InconsistentArrays">
<item>Light</item>
</string-array>

<string-array name="sorting">
<item>A-Z</item>
</string-array>
<string-array name="sorting_values">
<item>alphabetical</item>
</string-array>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
<string name="item_settings_menu_activity_main">Settings</string>
<string name="title_activity_settings">Settings</string>
<string name="theme_restart_message">You may need to restart the app for the new theme to apply properly.</string>
<string name="item_sort_menu_activity_main">Sort order</string>
</resources>
61 changes: 44 additions & 17 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:defaultValue="Light"
android:entries="@array/themes"
android:entryValues="@array/themes"
android:icon="@drawable/ic_palette_24dp"
android:key="theme"
android:summary="%s"
android:title="App theme" />
<ListPreference
android:defaultValue="300"
android:entries="@array/qr_code_resolutions_entries"
android:entryValues="@array/qr_code_resolutions_values"
android:icon="@drawable/ic_aspect_ratio_24dp"
android:key="qr_code_resolution"
android:summary="%s"
android:title="Resolution of generated QR codes" />
<PreferenceScreen xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:iconSpaceReserved="true"
android:title="Display"
tools:ignore="UnusedAttribute" >
<ListPreference
android:defaultValue="Light"
android:entries="@array/themes"
android:entryValues="@array/themes"
android:icon="@drawable/ic_palette_24dp"
android:key="theme"
android:summary="%s"
android:title="App theme" />
<ListPreference
android:defaultValue="300"
android:entries="@array/qr_code_resolutions_entries"
android:entryValues="@array/qr_code_resolutions_values"
android:icon="@drawable/ic_aspect_ratio_24dp"
android:key="qr_code_resolution"
android:summary="%s"
android:title="Resolution of generated QR codes" />
</PreferenceCategory>

<PreferenceCategory
android:iconSpaceReserved="true"
android:title="Sorting"
tools:ignore="UnusedAttribute">
<ListPreference
android:defaultValue="alphabetical"
android:entries="@array/sorting"
android:entryValues="@array/sorting_values"
android:icon="@drawable/ic_sort_24dp"
android:key="sorting"
android:summary="%s"
android:title="Sort by" />
<SwitchPreference
android:defaultValue="true"
android:icon="@drawable/ic_compare_arrows_24dp"
android:key="sorting_order"
android:summaryOff="Descending"
android:summaryOn="Ascending"
android:title="Sort order" />
</PreferenceCategory>
</PreferenceScreen>
3 changes: 3 additions & 0 deletions app/src/main/res/xml/xposed_preferences.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Display"
android:iconSpaceReserved="true" />
<ListPreference
android:defaultValue="300"
android:entries="@array/qr_code_resolutions_entries"
Expand Down

0 comments on commit 47ce5bb

Please sign in to comment.