Skip to content

Commit

Permalink
Working ordering for subfolders and search
Browse files Browse the repository at this point in the history
  • Loading branch information
izderadicka committed Mar 29, 2019
1 parent 17b2650 commit 5c3c048
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 11 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ android {
applicationId "eu.zderadicka.audioserve"
minSdkVersion 21
targetSdkVersion 28
versionCode 27
versionName "0.7.7"
versionCode 28
versionName "0.7.8"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
9 changes: 7 additions & 2 deletions app/src/main/java/eu/zderadicka/audioserve/AudioService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ private const val REWIND_PLAY_REPEAT = 1_000L
const val AUDIOSERVICE_ACTION_PAUSE = "eu.zderadicka.audioserve.ACTION_PAUSE"
const val AUDIOSERVICE_FORCE_RELOAD = "eu.zderadicka.audioserve.FORCE_RELOAD"
const val AUDIOSERVICE_DONT_PRELOAD_LATEST = "eu.zderadicka.audioserve.NO_PRELOAD_LATEST"
const val AUDIOSERVE_ORDERING = "eu.zderadicka.audioserve.ORDERING"
private const val AUDIOSERVICE_ACTION_SELF_START = "eu.zderadicka.audioserve.SELF_START"
private const val PAUSE_DELAYED_TASK_SAVE_POSITION = "pause_task_save_position"
private const val PAUSE_DELAYED_TASK_STOP_FOREGROUND = "pause_task_stop_fg"
Expand Down Expand Up @@ -878,7 +879,9 @@ mediaSessionConnector.setErrorMessageProvider(messageProvider);
val query = m.groups.get(2)?.value ?: ""

result.detach()
apiClient.loadSearch(query, collection, options.getBoolean(AUDIOSERVICE_FORCE_RELOAD))
apiClient.loadSearch(query, collection,
options.getBoolean(AUDIOSERVICE_FORCE_RELOAD),
options.getString(AUDIOSERVE_ORDERING))
{ it, err ->
if (it != null) {
result.sendResult(it.getMediaItems(cacheManager))
Expand All @@ -902,7 +905,9 @@ mediaSessionConnector.setErrorMessageProvider(messageProvider);
}

result.detach()
apiClient.loadFolder(folder, index, options.getBoolean(AUDIOSERVICE_FORCE_RELOAD))
apiClient.loadFolder(folder, index,
options.getBoolean(AUDIOSERVICE_FORCE_RELOAD),
options.getString(AUDIOSERVE_ORDERING))
{ it, err ->
checkError(it, err)
{
Expand Down
41 changes: 41 additions & 0 deletions app/src/main/java/eu/zderadicka/audioserve/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,24 @@ class MainActivity : AppCompatActivity(),
val searchView = menu.findItem(R.id.action_search).actionView as SearchView
searchView.setSearchableInfo(
searchManager.getSearchableInfo(componentName))
val orderingMenu = menu.findItem(R.id.submenu_ordering)
if (searchPrefix != null) {
orderingMenu.isVisible = true

val ordering = PreferenceManager
.getDefaultSharedPreferences(this)
.getString("pref_ordering", FoldersOrdering.Alphabetical.letter)
when (ordering) {
FoldersOrdering.Alphabetical.letter -> {
menu.findItem(R.id.action_ordering_alphabetical).isChecked = true
}
FoldersOrdering.RecentFirst.letter -> {
menu.findItem(R.id.action_ordering_recent).isChecked = true
}
}
} else {
orderingMenu.isVisible = false
}

return true
}
Expand Down Expand Up @@ -525,11 +543,34 @@ class MainActivity : AppCompatActivity(),
true
}

R.id.action_ordering_alphabetical -> {
item.isChecked = true
changeOrdering(FoldersOrdering.Alphabetical)
true
}
R.id.action_ordering_recent -> {
changeOrdering(FoldersOrdering.RecentFirst)
item.isChecked = true
true
}


else -> return super.onOptionsItemSelected(item)
}
}

private fun changeOrdering(ord: FoldersOrdering) {

val prefs = PreferenceManager.getDefaultSharedPreferences(this)
val current = prefs.getString("pref_ordering", FoldersOrdering.Alphabetical.letter)
if (ord.letter != current) {
Log.d(LOG_TAG, "Changed ordering to ${ord.letter}")
prefs.edit().putString("pref_ordering", ord.letter).apply()
folderFragment?.reload()
}

}

private fun openInitialFolder(folderId: String, folderName: String) {
//
for (i in 0..supportFragmentManager.backStackEntryCount) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package eu.zderadicka.audioserve.data

enum class FoldersOrdering(val letter: String) {
Alphabetical("a"),
RecentFirst("m")

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import android.text.format.DateUtils
import android.util.Log
import eu.zderadicka.audioserve.utils.ifStoppedOrDead
import android.os.Parcelable
import android.preference.PreferenceManager
import android.support.v4.content.ContextCompat
import android.view.*
import android.widget.*
Expand Down Expand Up @@ -467,6 +468,9 @@ class FolderFragment : MediaFragment(), BaseFolderFragment {

private fun startLoading(forceReload:Boolean=false) {
val options = Bundle()
val ordering = PreferenceManager.getDefaultSharedPreferences(context)
.getString("pref_ordering", FoldersOrdering.Alphabetical.letter)
options.putString(AUDIOSERVE_ORDERING, ordering)
if (willPrepare) {
willPrepare = false
options.putBoolean(AUDIOSERVICE_DONT_PRELOAD_LATEST, true)
Expand Down
32 changes: 25 additions & 7 deletions app/src/main/java/eu/zderadicka/audioserve/net/ApiClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -185,17 +185,23 @@ class ApiClient private constructor(val context: Context) {
}

fun loadFolder(folder: String = "", collection: Int, callback: (AudioFolder?, ApiError?) -> Unit) {
loadFolder(folder, collection, false, callback)
loadFolder(folder, collection, false, null, callback)
}

fun loadFolder(folder: String = "", collection: Int, forceReload: Boolean, callback: (AudioFolder?, ApiError?) -> Unit) {
fun loadFolder(folder: String = "", collection: Int, forceReload: Boolean,
ordering: String?,
callback: (AudioFolder?, ApiError?) -> Unit) {
var uri = baseUrl
if (collection > 0) {
uri += "$collection/"
}
uri += folder

sendRequest(uri, forceReload, {
val builder = Uri.parse(uri).buildUpon()
addOrdering(builder, ordering)
val folderUri = builder.build().toString()

sendRequest(folderUri, forceReload, {
val f = parseFolderfromJson(it, "", folder)
if (collection > 0) {
f.collectionIndex = collection
Expand All @@ -205,18 +211,30 @@ class ApiClient private constructor(val context: Context) {
}

fun loadSearch(query: String, collection: Int, callback: (AudioFolder?, ApiError?) -> Unit) {
loadSearch(query, collection, false, callback)
loadSearch(query, collection, false, null, callback)
}

private fun addOrdering(builder: Uri.Builder, ordering: String?) {
ordering?.let {
if (it != "a") {
builder.appendQueryParameter("ord", it)
}
}
}

fun loadSearch(query: String, collection: Int, forceReload: Boolean, callback: (AudioFolder?, ApiError?) -> Unit) {
fun loadSearch(query: String, collection: Int, forceReload: Boolean,
ordering: String?,
callback: (AudioFolder?, ApiError?) -> Unit) {
var uri = baseUrl
if (collection > 0) {
uri += "$collection/"
}
uri += "search"

val queryUri = Uri.parse(uri).buildUpon().appendQueryParameter("q", query).build()
sendRequest(queryUri.toString(), forceReload, {
val queryBuilder = Uri.parse(uri).buildUpon().appendQueryParameter("q", query)
addOrdering(queryBuilder, ordering)
val queryUri = queryBuilder.build().toString()
sendRequest(queryUri, forceReload, {
val f = parseFolderfromJson(it, "search", "")
if (collection > 0) {
f.collectionIndex = collection
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/res/menu/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@
app:showAsAction="ifRoom"
android:icon="@drawable/ic_info"
/>
<item android:id="@+id/submenu_ordering"
android:orderInCategory="200"
android:title="@string/folders_ordering"
app:showAsAction="never">
<menu>
<group android:id="@+id/group_ordering"
android:checkableBehavior="single">
<item android:id="@+id/action_ordering_alphabetical"
android:title="@string/alphabetical"/>
<item android:id="@+id/action_ordering_recent"
android:title="@string/recent_first"/>
</group>
</menu>
</item>

<item
android:id="@+id/action_reload"
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,7 @@
<string name="recently_modified">Recently Modified Folders</string>
<string name="start_sleep_timer">Start Sleep Timer</string>
<string name="press_back_toast">Press Back again to exit</string>
<string name="folders_ordering">Folders Ordering</string>
<string name="alphabetical">Alphabetical</string>
<string name="recent_first">Recent First</string>
</resources>

0 comments on commit 5c3c048

Please sign in to comment.