Skip to content

Commit bb1ae01

Browse files
committed
feat(reminders): launch ScheduleRemindersFragment from DeckPicker and StudyOptionsFragment
Adds logic for opening ScheduleRemindersFragment in the correct fragment container hosts in DeckPicker and StudyOptionsFragment. Mirrors `openStudyOptions` and `tryShowStudyOptionsPanel` by adding `openScheduleReminders` and `tryShowScheduleRemindersPanel`. Reloads the StudyOptionsFragment and removes ScheduleRemindersFragment if an appbar menu button is clicked while the fragment is open on large screens to prevent stale state from showing.
1 parent fbed7db commit bb1ae01

4 files changed

Lines changed: 76 additions & 15 deletions

File tree

AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import androidx.core.view.doOnLayout
6464
import androidx.core.view.isVisible
6565
import androidx.draganddrop.DropHelper
6666
import androidx.fragment.app.FragmentContainerView
67+
import androidx.fragment.app.FragmentManager
6768
import androidx.fragment.app.commit
6869
import androidx.lifecycle.flowWithLifecycle
6970
import androidx.lifecycle.lifecycleScope
@@ -72,7 +73,6 @@ import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
7273
import androidx.work.WorkInfo
7374
import androidx.work.WorkManager
7475
import anki.collection.OpChanges
75-
import anki.decks.deckId
7676
import anki.sync.SyncStatusResponse
7777
import com.google.android.material.progressindicator.LinearProgressIndicator
7878
import com.google.android.material.snackbar.BaseTransientBottomBar
@@ -155,7 +155,9 @@ import com.ichi2.anki.preferences.AdvancedSettingsFragment
155155
import com.ichi2.anki.preferences.PreferencesActivity
156156
import com.ichi2.anki.preferences.sharedPrefs
157157
import com.ichi2.anki.receiver.SdCardReceiver
158+
import com.ichi2.anki.reviewreminders.ReviewReminderScope
158159
import com.ichi2.anki.reviewreminders.ReviewRemindersDatabase
160+
import com.ichi2.anki.reviewreminders.ScheduleRemindersFragment
159161
import com.ichi2.anki.servicelayer.ScopedStorageService
160162
import com.ichi2.anki.settings.Prefs
161163
import com.ichi2.anki.snackbar.BaseSnackbarBuilderProvider
@@ -582,6 +584,12 @@ open class DeckPicker :
582584
openReviewer()
583585
}
584586

587+
setFragmentResultListener(StudyOptionsFragment.REQUEST_STUDY_OPTIONS_REVIEW_REMINDERS) { _, bundle ->
588+
Timber.d("Opening review reminders screen from DeckPicker's study options panel")
589+
val did = bundle.getLong(StudyOptionsFragment.KEY_REVIEW_REMINDERS_DECK_ID)
590+
openScheduleReminders(did)
591+
}
592+
585593
pullToSyncWrapper.configureView(
586594
this,
587595
IMPORT_MIME_TYPES,
@@ -715,6 +723,7 @@ open class DeckPicker :
715723
data = deckList.data,
716724
hasSubDecks = deckList.hasSubDecks,
717725
)
726+
tryShowStudyOptionsPanel()
718727
}
719728

720729
fun onFocusedDeckChanged(deckId: DeckId?) {
@@ -898,8 +907,8 @@ open class DeckPicker :
898907
}
899908
DeckPickerContextMenuOption.SCHEDULE_REMINDERS -> {
900909
Timber.i("Scheduling review reminders for deck '%d'", deckId)
901-
viewModel.scheduleReviewReminders(deckId)
902910
dismissAllDialogFragments()
911+
openScheduleReminders(deckId)
903912
}
904913
}
905914
}
@@ -1866,12 +1875,28 @@ open class DeckPicker :
18661875
*/
18671876
private fun tryShowStudyOptionsPanel(): Boolean {
18681877
val containerId = binding.studyoptionsFragment?.id ?: return false
1878+
supportFragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
18691879
supportFragmentManager.commit {
18701880
replace(containerId, StudyOptionsFragment())
18711881
}
18721882
return true
18731883
}
18741884

1885+
private fun tryShowScheduleRemindersPanel(deckId: DeckId): Boolean {
1886+
val sidePanel = binding.studyoptionsFragment ?: return false
1887+
if (!sidePanel.isVisible) return false
1888+
val newFragment =
1889+
ScheduleRemindersFragment.newInstance(
1890+
scope = ReviewReminderScope.DeckSpecific(deckId),
1891+
host = ScheduleRemindersFragment.FragmentHost.STUDY_OPTIONS_FRAGMENT,
1892+
)
1893+
supportFragmentManager.commit {
1894+
replace(sidePanel.id, newFragment)
1895+
addToBackStack(null)
1896+
}
1897+
return true
1898+
}
1899+
18751900
val fragment: StudyOptionsFragment?
18761901
get() = supportFragmentManager.findFragmentById(R.id.studyoptions_fragment) as? StudyOptionsFragment
18771902

@@ -1905,6 +1930,14 @@ open class DeckPicker :
19051930
reviewLauncher.launch(intent)
19061931
}
19071932

1933+
private fun openScheduleReminders(deckId: DeckId) {
1934+
if (tryShowScheduleRemindersPanel(deckId)) return
1935+
1936+
// otherwise, we need to launch the activity
1937+
Timber.i("Opening Schedule Reminders")
1938+
viewModel.scheduleReviewReminders(deckId)
1939+
}
1940+
19081941
private fun openReviewerOrStudyOptions(selectionType: DeckSelectionType) {
19091942
when (selectionType) {
19101943
DeckSelectionType.DEFAULT -> {

AnkiDroid/src/main/java/com/ichi2/anki/StudyOptionsActivity.kt

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import android.os.Bundle
2020
import android.view.Menu
2121
import android.view.MenuItem
2222
import androidx.core.view.MenuItemCompat
23+
import androidx.fragment.app.Fragment
2324
import androidx.fragment.app.commit
2425
import androidx.lifecycle.lifecycleScope
2526
import anki.collection.OpChanges
@@ -29,6 +30,8 @@ import com.ichi2.anki.dialogs.customstudy.CustomStudyDialog.CustomStudyAction.Co
2930
import com.ichi2.anki.libanki.undoAvailable
3031
import com.ichi2.anki.libanki.undoLabel
3132
import com.ichi2.anki.observability.ChangeManager
33+
import com.ichi2.anki.reviewreminders.ReviewReminderScope
34+
import com.ichi2.anki.reviewreminders.ScheduleRemindersFragment
3235
import com.ichi2.anki.utils.ext.setFragmentResultListener
3336
import com.ichi2.ui.RtlCompliantActionProvider
3437
import kotlinx.coroutines.launch
@@ -58,7 +61,7 @@ class StudyOptionsActivity :
5861
CustomStudyAction.CUSTOM_STUDY_SESSION,
5962
CustomStudyAction.EXTEND_STUDY_LIMITS,
6063
->
61-
currentFragment!!.refreshInterface()
64+
(currentFragment as? StudyOptionsFragment)?.refreshInterface()
6265
}
6366
}
6467
setFragmentResultListener(StudyOptionsFragment.REQUEST_STUDY_OPTIONS_STUDY) { _, _ ->
@@ -69,6 +72,21 @@ class StudyOptionsActivity :
6972
startActivity(reviewer)
7073
finish()
7174
}
75+
setFragmentResultListener(StudyOptionsFragment.REQUEST_STUDY_OPTIONS_REVIEW_REMINDERS) { _, bundle ->
76+
Timber.d("Opening review reminders screen from study options screen")
77+
val did = bundle.getLong(StudyOptionsFragment.KEY_REVIEW_REMINDERS_DECK_ID)
78+
supportFragmentManager.commit {
79+
replace(
80+
R.id.studyoptions_frame,
81+
ScheduleRemindersFragment.newInstance(
82+
scope = ReviewReminderScope.DeckSpecific(did),
83+
host = ScheduleRemindersFragment.FragmentHost.STUDY_OPTIONS_FRAME,
84+
),
85+
)
86+
addToBackStack(null)
87+
}
88+
invalidateOptionsMenu()
89+
}
7290
}
7391

7492
private fun loadStudyOptionsFragment() {
@@ -78,16 +96,16 @@ class StudyOptionsActivity :
7896
}
7997
}
8098

81-
private val currentFragment: StudyOptionsFragment?
82-
get() = supportFragmentManager.findFragmentById(R.id.studyoptions_frame) as StudyOptionsFragment?
99+
private val currentFragment: Fragment?
100+
get() = supportFragmentManager.findFragmentById(R.id.studyoptions_frame)
83101

84102
override fun onCreateOptionsMenu(menu: Menu): Boolean {
85103
menuInflater.inflate(R.menu.activity_study_options, menu)
86104
val undoMenuItem = menu.findItem(R.id.action_undo)
87105
val undoActionProvider = MenuItemCompat.getActionProvider(undoMenuItem) as? RtlCompliantActionProvider
88106
// Set the proper click target for the undo button's ActionProvider
89107
undoActionProvider?.clickHandler = { _, menuItem -> onOptionsItemSelected(menuItem) }
90-
undoMenuItem.isVisible = undoState.hasAction
108+
undoMenuItem.isVisible = undoState.hasAction && (currentFragment is StudyOptionsFragment)
91109
undoMenuItem.title = undoState.label
92110
return true
93111
}
@@ -124,7 +142,7 @@ class StudyOptionsActivity :
124142
handler: Any?,
125143
) {
126144
refreshUndoState()
127-
currentFragment?.refreshInterface()
145+
(currentFragment as? StudyOptionsFragment)?.refreshInterface()
128146
}
129147

130148
private fun refreshUndoState() {

AnkiDroid/src/main/java/com/ichi2/anki/StudyOptionsFragment.kt

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ import com.ichi2.anki.libanki.Collection
4949
import com.ichi2.anki.libanki.Decks
5050
import com.ichi2.anki.observability.ChangeManager
5151
import com.ichi2.anki.observability.undoableOp
52-
import com.ichi2.anki.reviewreminders.ReviewReminderScope
53-
import com.ichi2.anki.reviewreminders.ScheduleRemindersFragment
5452
import com.ichi2.anki.settings.Prefs
5553
import com.ichi2.anki.ui.internationalization.sentenceCase
5654
import com.ichi2.anki.utils.ext.showDialogFragment
@@ -220,12 +218,12 @@ class StudyOptionsFragment :
220218
}
221219
R.id.action_schedule_reminders -> {
222220
Timber.i("StudyOptionsFragment:: schedule reminders button pressed")
223-
val intent =
224-
ScheduleRemindersFragment.getIntent(
225-
requireContext(),
226-
ReviewReminderScope.DeckSpecific(col!!.decks.current().id),
227-
)
228-
startActivity(intent)
221+
parentFragmentManager.setFragmentResult(
222+
REQUEST_STUDY_OPTIONS_REVIEW_REMINDERS,
223+
bundleOf(
224+
KEY_REVIEW_REMINDERS_DECK_ID to col!!.decks.current().id,
225+
),
226+
)
229227
return true
230228
}
231229
R.id.action_unbury -> {
@@ -545,6 +543,17 @@ class StudyOptionsFragment :
545543
*/
546544
const val REQUEST_STUDY_OPTIONS_STUDY = "request_study_option_study"
547545

546+
/**
547+
* Identifier for a fragment result request to open the review reminders screen for the selected deck.
548+
* Activities using this fragment need to handle this request and open the review reminders screen as they see fit.
549+
*/
550+
const val REQUEST_STUDY_OPTIONS_REVIEW_REMINDERS = "request_study_option_review_reminders"
551+
552+
/**
553+
* Bundle key for the deck ID whose study options are being displayed, used when the review reminders screen is being opened.
554+
*/
555+
const val KEY_REVIEW_REMINDERS_DECK_ID = "review_reminders_deck_id"
556+
548557
/**
549558
* Constants for selecting which content view to display
550559
*/

AnkiDroid/src/main/res/layout/activity_study_options.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<androidx.coordinatorlayout.widget.CoordinatorLayout android:id="@+id/root_layout"
33
android:layout_width="match_parent"
44
android:layout_height="match_parent"
5+
android:fitsSystemWindows="true"
56
xmlns:android="http://schemas.android.com/apk/res/android">
67
<LinearLayout
78
android:layout_width="match_parent"

0 commit comments

Comments
 (0)