Skip to content

Commit d0caf23

Browse files
authored
Merge pull request #8 from algolia/feat/persistFragmentState
feat(VoiceDialogFragment): Persist suggestions
2 parents 6943080 + 1bc4ca0 commit d0caf23

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

app/src/main/java/com/algolia/instantsearch/voice/demo/MainActivity.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ class MainActivity : AppCompatActivity(), VoiceInput.VoiceResultsListener {
1515
super.onCreate(savedInstanceState)
1616
setContentView(R.layout.activity_main)
1717

18+
val voiceFragment = VoiceDialogFragment()
19+
1820
button.setOnClickListener { _ ->
1921
if (!Voice.hasRecordPermission(this)) {
2022
PermissionDialogFragment().let {
2123
it.arguments = PermissionDialogFragment.buildArguments(title = "Voice Search.")
2224
it.show(supportFragmentManager, "perm")
2325
}
2426
} else {
25-
val voiceFragment = VoiceDialogFragment() //FIXME: Handle orientation changes, storing state properly
2627
voiceFragment.setSuggestions("Something", "Something else")
2728
voiceFragment.input.language = "en-US"
2829
voiceFragment.input.maxResults = 2

instantsearch.voice/src/main/java/com/algolia/instantsearch/voice/ui/VoiceDialogFragment.kt

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,28 @@ class VoiceDialogFragment : DialogFragment(), VoiceInput.VoiceInputPresenter {
1818

1919
val input: VoiceInput = VoiceInput(this)
2020

21-
private var suggestions: List<String> = emptyList()
21+
private var suggestions: Array<out String> = arrayOf()
2222

2323
fun setSuggestions(vararg suggestions: String) {
24-
this.suggestions = listOf(*suggestions)
24+
this.suggestions = suggestions
2525
}
2626

2727
//region Lifecycle
28-
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
29-
inflater.inflate(R.layout.layout_voice_overlay, null)
28+
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
29+
return inflater.inflate(R.layout.layout_voice_overlay, null)
30+
}
31+
32+
override fun onSaveInstanceState(outState: Bundle) {
33+
super.onSaveInstanceState(outState)
34+
outState.putStringArray(keySuggestions, suggestions)
35+
}
3036

3137
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
3238
closeButton.setOnClickListener { dismiss() }
3339
micButton.setOnClickListener { input.toggleVoiceRecognition() }
40+
savedInstanceState?.let {
41+
suggestions = it.getStringArray(keySuggestions)
42+
}
3443
}
3544

3645
override fun onAttach(context: Context?) {
@@ -53,7 +62,8 @@ class VoiceDialogFragment : DialogFragment(), VoiceInput.VoiceInputPresenter {
5362
//region Helpers
5463
companion object {
5564

56-
const val SEPARATOR = ""
65+
const val separator = ""
66+
const val keySuggestions = "keySuggestions"
5767
}
5868
//endregion
5969

@@ -66,7 +76,7 @@ class VoiceDialogFragment : DialogFragment(), VoiceInput.VoiceInputPresenter {
6676
hint.visibility = View.GONE
6777
} else {
6878
hint.visibility = View.VISIBLE
69-
suggestionText.text = suggestions.fold("") { acc, it -> "$acc$SEPARATOR$it\n" }
79+
suggestionText.text = suggestions.fold("") { acc, it -> "$acc$separator$it\n" }
7080
}
7181
}
7282

0 commit comments

Comments
 (0)