Skip to content

Commit

Permalink
refactor: move new previewer into a SingleFragmentActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
BrayanDSO committed Dec 14, 2023
1 parent 0568c9c commit 627251d
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 65 deletions.
4 changes: 0 additions & 4 deletions AnkiDroid/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,6 @@
android:label="@string/preview_title"
android:exported="false"
/>
<activity
android:name="com.ichi2.anki.previewer.PreviewerActivity"
android:exported="false"
/>
<activity
android:name="com.ichi2.anki.SingleFragmentActivity"
android:exported="false"
Expand Down
7 changes: 2 additions & 5 deletions AnkiDroid/src/main/java/com/ichi2/anki/Previewer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import androidx.annotation.VisibleForTesting
import com.ichi2.anki.cardviewer.Gesture
import com.ichi2.anki.cardviewer.PreviewLayout
import com.ichi2.anki.cardviewer.ViewerCommand
import com.ichi2.anki.previewer.PreviewerActivity
import com.ichi2.anki.previewer.PreviewerFragment
import com.ichi2.libanki.Collection
import timber.log.Timber

Expand Down Expand Up @@ -286,10 +286,7 @@ class Previewer : AbstractFlashcardViewer() {

@CheckResult
fun Previewer2Destination.toIntent(context: Context) =
Intent(context, PreviewerActivity::class.java).apply {
putExtra(PreviewerActivity.CURRENT_INDEX_EXTRA, currentIndex)
putExtra(PreviewerActivity.CARD_IDS_EXTRA, selectedCardIds)
}
PreviewerFragment.getIntent(context, selectedCardIds, currentIndex)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,62 +18,63 @@ package com.ichi2.anki.previewer
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.webkit.CookieManager
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.Toolbar
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import com.google.android.material.appbar.MaterialToolbar
import com.google.android.material.button.MaterialButton
import com.google.android.material.slider.Slider
import com.google.android.material.textview.MaterialTextView
import com.ichi2.anki.AnkiActivity
import com.ichi2.anki.CollectionHelper
import com.ichi2.anki.NoteEditor
import com.ichi2.anki.R
import com.ichi2.anki.SingleFragmentActivity
import com.ichi2.anki.previewer.PreviewerViewModel.Companion.stdHtml
import com.ichi2.themes.Themes
import com.ichi2.themes.setTransparentStatusBar
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import java.io.File

class PreviewerActivity : AnkiActivity() {
class PreviewerFragment : Fragment(), Toolbar.OnMenuItemClickListener {
private lateinit var viewModel: PreviewerViewModel

private var backsideOnlyButton: MenuItem? = null
private val backsideOnlyOption: MenuItem
get() = requireView().findViewById<Toolbar>(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<WebView>(R.id.webview)
val webView = view.findViewById<WebView>(R.id.webview)
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true)
with(webView) {
webViewClient = object : WebViewClient() {
Expand All @@ -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<Slider>(R.id.slider)
val nextButton = findViewById<MaterialButton>(R.id.show_next)
val previousButton = findViewById<MaterialButton>(R.id.show_previous)
val progressIndicator = findViewById<MaterialTextView>(R.id.progress_indicator)
val slider = view.findViewById<Slider>(R.id.slider)
val nextButton = view.findViewById<MaterialButton>(R.id.show_next)
val previousButton = view.findViewById<MaterialButton>(R.id.show_previous)
val progressIndicator = view.findViewById<MaterialTextView>(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()
Expand Down Expand Up @@ -169,16 +170,25 @@ class PreviewerActivity : AnkiActivity() {
previousButton.setOnClickListener {
viewModel.launchCatching { showAnswerOrPreviousCard() }
}

view.findViewById<MaterialToolbar>(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 {
backsideOnlyOption.apply {
if (isBacksideOnly) {
setIcon(R.drawable.ic_card_answer)
setTitle(R.string.card_side_answer)
Expand All @@ -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
Expand All @@ -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())
}
Expand All @@ -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)
}
}
}
4 changes: 2 additions & 2 deletions AnkiDroid/src/main/res/layout/previewer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
Expand All @@ -23,7 +23,7 @@
app:layout_constraintTop_toTopOf="parent"
app:navigationContentDescription="@string/abc_action_bar_up_description"
app:navigationIcon="?attr/homeAsUpIndicator"
tools:menu="@menu/previewer2"
app:menu="@menu/previewer2"
/>

</com.google.android.material.appbar.AppBarLayout>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import android.app.Activity
import android.os.Looper.getMainLooper
import com.canhub.cropper.CropImageActivity
import com.ichi2.anki.preferences.Preferences
import com.ichi2.anki.previewer.PreviewerActivity
import com.ichi2.testutils.ActivityList
import com.ichi2.testutils.ActivityList.ActivityLaunchParam
import com.ichi2.testutils.EmptyApplication
Expand Down Expand Up @@ -54,7 +53,6 @@ class ActivityStartupUnderBackupTest : RobolectricTest() {
notYetHandled(IntentHandler::class.java.simpleName, "Not working (or implemented) - inherits from Activity")
notYetHandled(Preferences::class.java.simpleName, "Not working (or implemented) - inherits from AppCompatPreferenceActivity")
notYetHandled(FilteredDeckOptions::class.java.simpleName, "Not working (or implemented) - inherits from AppCompatPreferenceActivity")
notYetHandled(PreviewerActivity::class.java.simpleName, "Implemented, but the test fails because the activity throws if a specific intent extra isn't set")
notYetHandled(SingleFragmentActivity::class.java.simpleName, "Implemented, but the test fails because the activity throws if a specific intent extra isn't set")
}

Expand Down
2 changes: 0 additions & 2 deletions AnkiDroid/src/test/java/com/ichi2/testutils/ActivityList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import com.ichi2.anki.multimediacard.activity.MultimediaEditFieldActivity
import com.ichi2.anki.notetype.ManageNotetypes
import com.ichi2.anki.pages.PagesActivity
import com.ichi2.anki.preferences.Preferences
import com.ichi2.anki.previewer.PreviewerActivity
import com.ichi2.anki.services.ReminderService.Companion.getReviewDeckIntent
import com.ichi2.anki.ui.windows.managespace.ManageSpaceActivity
import com.ichi2.anki.ui.windows.permissions.PermissionsActivity
Expand Down Expand Up @@ -76,7 +75,6 @@ object ActivityList {
get(ManageNotetypes::class.java),
get(ManageSpaceActivity::class.java),
get(PermissionsActivity::class.java),
get(PreviewerActivity::class.java),
get(SingleFragmentActivity::class.java)
)
}
Expand Down

0 comments on commit 627251d

Please sign in to comment.