Skip to content

Commit

Permalink
implement sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
newhinton committed May 16, 2024
1 parent 3ad40d2 commit 84a5679
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
15 changes: 14 additions & 1 deletion app/src/main/java/de/felixnuesse/disky/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ class MainActivity : AppCompatActivity(), ChangeFolderCallback, ScanCompleteCall

private var lastScanStarted = 0L

companion object {
const val APP_PREFERENCES = "APP_PREFERENCES"
const val APP_PREFERENCE_SORTORDER = "APP_PREFERENCE_SORTORDER"
}


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
Expand Down Expand Up @@ -269,8 +275,15 @@ class MainActivity : AppCompatActivity(), ChangeFolderCallback, ScanCompleteCall
val percentage = (it.getCalculatedSize().toFloat()/max.toFloat())
it.percent = (percentage*100).toInt()
}

val sharedPref = applicationContext.getSharedPreferences(APP_PREFERENCES, Context.MODE_PRIVATE)
val sortbySize = sharedPref.getInt(APP_PREFERENCE_SORTORDER, 0) == 0 // 0 is size. If we get more, we need to decide here how to sort.
//second, sort children
val l = currentElement!!.getChildren().sortedWith(compareBy{ list -> list.getCalculatedSize()})
val l = if (sortbySize) {
currentElement!!.getChildren().sortedWith(compareBy{ list -> list.getCalculatedSize()})
} else {
currentElement!!.getChildren().sortedWith(compareBy{ list -> list.name.lowercase()}).reversed()
}
currentElement!!.clearChildren()
currentElement!!.getChildren().addAll(l.reversed())

Expand Down
16 changes: 15 additions & 1 deletion app/src/main/java/de/felixnuesse/disky/ui/BottomSheet.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package de.felixnuesse.disky.ui

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.enableEdgeToEdge
import android.widget.AdapterView.OnItemClickListener
import android.widget.Toast
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import de.felixnuesse.disky.AboutActivity
import de.felixnuesse.disky.FAQActivity
import de.felixnuesse.disky.MainActivity
import de.felixnuesse.disky.databinding.BottomsheetBinding


Expand Down Expand Up @@ -41,6 +44,17 @@ class BottomSheet(): BottomSheetDialogFragment() {
binding.aboutButton.setOnClickListener {
startActivity(Intent(this.context, AboutActivity::class.java))
}


val sharedPref = binding.root.context.getSharedPreferences(MainActivity.APP_PREFERENCES, Context.MODE_PRIVATE)
val editor = sharedPref.edit()
val selection = sharedPref.getInt(MainActivity.APP_PREFERENCE_SORTORDER, 0)
binding.sortorderDropdownAutocomplete.setText(binding.sortorderDropdownAutocomplete.adapter.getItem(selection).toString(), false)
binding.sortorderDropdownAutocomplete.onItemClickListener =
OnItemClickListener { _, _, pos, _ ->
editor.putInt(MainActivity.APP_PREFERENCE_SORTORDER, pos)
editor.apply()
}
}

companion object {
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/layout/bottomsheet.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,11 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:startIconDrawable="@drawable/icon_app"
app:startIconDrawable="@drawable/icon_sort"
app:startIconTint="?attr/colorOnPrimaryContainer">

<AutoCompleteTextView
android:id="@+id/sortorderDropdownAutocomplete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"
Expand Down
4 changes: 1 addition & 3 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="sortoptions">
<item>@string/sortorder_aplhabet</item>
<item>@string/sortorder_size</item>
<item>@string/sortorder_aplhabet</item>
</string-array>
<string name="sortorder_aplhabet">Alphabet</string>
<string name="sortorder_size">Size</string>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,6 @@
<string name="sortorder">Sort Order</string>
<string name="about_this_app">About this app</string>
<string name="faq_description">If you have questions, they might be answered here</string>
<string name="sortorder_aplhabet">Alphabet</string>
<string name="sortorder_size">Size</string>
</resources>

0 comments on commit 84a5679

Please sign in to comment.