From 9aea8fc8013a2c8ebed5adbad9f9788a3a6632a2 Mon Sep 17 00:00:00 2001 From: Brayan Oliveira <69634269+brayandso@users.noreply.github.com> Date: Tue, 12 Dec 2023 20:43:22 -0300 Subject: [PATCH] refactor: move new previewer into a SingleFragmentActivity --- AnkiDroid/src/main/AndroidManifest.xml | 4 - .../src/main/java/com/ichi2/anki/Previewer.kt | 7 +- ...viewerActivity.kt => PreviewerFragment.kt} | 95 +++++++++---------- AnkiDroid/src/main/res/layout/previewer.xml | 4 +- 4 files changed, 49 insertions(+), 61 deletions(-) rename AnkiDroid/src/main/java/com/ichi2/anki/previewer/{PreviewerActivity.kt => PreviewerFragment.kt} (75%) diff --git a/AnkiDroid/src/main/AndroidManifest.xml b/AnkiDroid/src/main/AndroidManifest.xml index e01b8ce48a6a..73307fdb3c3d 100644 --- a/AnkiDroid/src/main/AndroidManifest.xml +++ b/AnkiDroid/src/main/AndroidManifest.xml @@ -345,10 +345,6 @@ android:label="@string/preview_title" android:exported="false" /> - (R.id.toolbar) + .menu.findItem(R.id.action_back_side_only) - override fun onCreate(savedInstanceState: Bundle?) { - if (showedActivityFailedScreen(savedInstanceState)) { - return - } - super.onCreate(savedInstanceState) - setContentView(R.layout.previewer) - setTransparentStatusBar() - - enableToolbar().apply { - setDisplayHomeAsUpEnabled(true) - setDisplayShowTitleEnabled(false) - } + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + return inflater.inflate(R.layout.previewer, container, false) + } - val selectedCardIds = intent.getLongArrayExtra(CARD_IDS_EXTRA) - ?: throw IllegalArgumentException("'cardIds' extra must be present") - val currentIndex = intent.getIntExtra(CURRENT_INDEX_EXTRA, 0) - val mediaDir = File(CollectionHelper.getCurrentAnkiDroidDirectory(this), "collection.media").path + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + val selectedCardIds = requireArguments().getLongArray(CARD_IDS_EXTRA)!! + val currentIndex = requireArguments().getInt(CURRENT_INDEX_EXTRA, 0) + val mediaDir = File(CollectionHelper.getCurrentAnkiDroidDirectory(requireContext()), "collection.media").path viewModel = ViewModelProvider( this, PreviewerViewModel.factory(mediaDir, selectedCardIds, currentIndex) )[PreviewerViewModel::class.java] - val webView = findViewById(R.id.webview) + val webView = view.findViewById(R.id.webview) CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true) with(webView) { webViewClient = object : WebViewClient() { @@ -93,22 +94,22 @@ class PreviewerActivity : AnkiActivity() { } loadDataWithBaseURL( viewModel.serverBaseUrl(), - stdHtml(this@PreviewerActivity, Themes.currentTheme.isNightMode), + stdHtml(requireContext(), Themes.currentTheme.isNightMode), "text/html", null, null ) } - val slider = findViewById(R.id.slider) - val nextButton = findViewById(R.id.show_next) - val previousButton = findViewById(R.id.show_previous) - val progressIndicator = findViewById(R.id.progress_indicator) + val slider = view.findViewById(R.id.slider) + val nextButton = view.findViewById(R.id.show_next) + val previousButton = view.findViewById(R.id.show_previous) + val progressIndicator = view.findViewById(R.id.progress_indicator) viewModel.onError .flowWithLifecycle(lifecycle) .onEach { errorMessage -> - AlertDialog.Builder(this@PreviewerActivity) + AlertDialog.Builder(requireContext()) .setTitle(R.string.vague_error) .setMessage(errorMessage) .show() @@ -169,16 +170,25 @@ class PreviewerActivity : AnkiActivity() { previousButton.setOnClickListener { viewModel.launchCatching { showAnswerOrPreviousCard() } } + + view.findViewById(R.id.toolbar).apply { + setOnMenuItemClickListener(this@PreviewerFragment) + setNavigationOnClickListener { requireActivity().onBackPressedDispatcher.onBackPressed() } + } + + super.onViewCreated(view, savedInstanceState) } - override fun onCreateOptionsMenu(menu: Menu?): Boolean { - menuInflater.inflate(R.menu.previewer2, menu) - backsideOnlyButton = menu?.findItem(R.id.action_back_side_only) + override fun onMenuItemClick(item: MenuItem): Boolean { + when (item.itemId) { + R.id.action_edit -> editCard() + R.id.action_back_side_only -> viewModel.toggleBacksideOnly() + } return true } private fun setBacksideOnlyButtonIcon(isBacksideOnly: Boolean) { - backsideOnlyButton?.apply { + backsideOnlyButton.apply { if (isBacksideOnly) { setIcon(R.drawable.ic_card_answer) setTitle(R.string.card_side_answer) @@ -189,19 +199,6 @@ class PreviewerActivity : AnkiActivity() { } } - override fun onPrepareOptionsMenu(menu: Menu?): Boolean { - setBacksideOnlyButtonIcon(viewModel.backsideOnly.value) - return super.onPrepareOptionsMenu(menu) - } - - override fun onOptionsItemSelected(item: MenuItem): Boolean { - when (item.itemId) { - R.id.action_edit -> editCard() - R.id.action_back_side_only -> viewModel.toggleBacksideOnly() - } - return super.onOptionsItemSelected(item) - } - private val editCardLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> if (result.data?.getBooleanExtra(NoteEditor.RELOAD_REQUIRED_EXTRA_KEY, false) == true || result.data?.getBooleanExtra(NoteEditor.NOTE_CHANGED_EXTRA_KEY, false) == true @@ -211,7 +208,7 @@ class PreviewerActivity : AnkiActivity() { } private fun editCard() { - val intent = Intent(this, NoteEditor::class.java).apply { + val intent = Intent(requireContext(), NoteEditor::class.java).apply { putExtra(NoteEditor.EXTRA_CALLER, NoteEditor.CALLER_PREVIEWER_EDIT) putExtra(NoteEditor.EXTRA_EDIT_FROM_CARD_ID, viewModel.cardId()) } @@ -223,10 +220,8 @@ class PreviewerActivity : AnkiActivity() { const val CARD_IDS_EXTRA = "cardIds" fun getIntent(context: Context, selectedCardIds: LongArray, currentIndex: Int): Intent { - return Intent(context, PreviewerActivity::class.java).apply { - putExtra(CURRENT_INDEX_EXTRA, currentIndex) - putExtra(CARD_IDS_EXTRA, selectedCardIds) - } + val args = bundleOf(CURRENT_INDEX_EXTRA to currentIndex, CARD_IDS_EXTRA to selectedCardIds) + return SingleFragmentActivity.getIntent(context, PreviewerFragment::class, args) } } } diff --git a/AnkiDroid/src/main/res/layout/previewer.xml b/AnkiDroid/src/main/res/layout/previewer.xml index 3d661e486195..99e9f9da871b 100644 --- a/AnkiDroid/src/main/res/layout/previewer.xml +++ b/AnkiDroid/src/main/res/layout/previewer.xml @@ -4,7 +4,7 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context=".previewer.PreviewerActivity"> + tools:context=".previewer.PreviewerFragment">