-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
39c706c
commit 089afc6
Showing
3 changed files
with
129 additions
and
83 deletions.
There are no files selected for viewing
116 changes: 33 additions & 83 deletions
116
app/src/main/java/com/kylecorry/trail_sense/tools/guide/ui/GuideListFragment.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
69 changes: 69 additions & 0 deletions
69
app/src/main/java/com/kylecorry/trail_sense/tools/guide/ui/GuideListPreferenceFragment.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,69 @@ | ||
package com.kylecorry.trail_sense.tools.guide.ui | ||
|
||
import android.os.Bundle | ||
import androidx.core.os.bundleOf | ||
import androidx.navigation.fragment.findNavController | ||
import androidx.preference.Preference | ||
import androidx.preference.PreferenceCategory | ||
import androidx.preference.PreferenceFragmentCompat | ||
import com.kylecorry.andromeda.core.tryOrNothing | ||
import com.kylecorry.andromeda.fragments.inBackground | ||
import com.kylecorry.luna.coroutines.CoroutineQueueRunner | ||
import com.kylecorry.trail_sense.R | ||
import com.kylecorry.trail_sense.tools.guide.domain.UserGuideCategory | ||
import kotlinx.coroutines.Dispatchers | ||
|
||
class GuideListPreferenceFragment : PreferenceFragmentCompat() { | ||
|
||
private val queue = CoroutineQueueRunner(dispatcher = Dispatchers.Main) | ||
|
||
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { | ||
setPreferencesFromResource(R.xml.user_guide, rootKey) | ||
preferenceScreen.setShouldUseGeneratedIds(true) | ||
} | ||
|
||
fun updateList(guides: List<UserGuideCategory>) { | ||
inBackground { | ||
queue.enqueue { | ||
preferenceScreen.removeAll() | ||
|
||
for (guideCategory in guides) { | ||
val category = PreferenceCategory(requireContext()) | ||
category.title = guideCategory.name | ||
category.isIconSpaceReserved = false | ||
category.isSingleLineTitle = false | ||
preferenceScreen.addPreference(category) | ||
|
||
for (guide in guideCategory.guides) { | ||
val guidePref = Preference(requireContext()) | ||
guidePref.title = guide.name | ||
if (guide.description != null) { | ||
guidePref.summary = guide.description | ||
} | ||
guidePref.isSingleLineTitle = false | ||
guidePref.isIconSpaceReserved = false | ||
onClick(guidePref) { | ||
tryOrNothing { | ||
findNavController().navigate( | ||
R.id.action_guideListFragment_to_guideFragment, bundleOf( | ||
"guide_name" to guide.name, | ||
"guide_contents" to guide.contents | ||
) | ||
) | ||
} | ||
} | ||
category.addPreference(guidePref) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
private fun onClick(pref: Preference?, action: () -> Unit) { | ||
pref?.setOnPreferenceClickListener { | ||
action.invoke() | ||
true | ||
} | ||
} | ||
} |
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,27 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
<com.kylecorry.trail_sense.shared.views.SearchView | ||
android:id="@+id/searchbox" | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:layout_marginStart="16dp" | ||
android:layout_marginTop="16dp" | ||
android:layout_marginEnd="16dp" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
|
||
<androidx.fragment.app.FragmentContainerView | ||
android:id="@+id/guide_fragment" | ||
android:layout_width="0dp" | ||
android:layout_height="0dp" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@id/searchbox" /> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> |