Skip to content

Commit

Permalink
refactor: replace PagesActivity with SingleFragmentActivity
Browse files Browse the repository at this point in the history
they were practically the same thing, so reusing the generic class is nice
  • Loading branch information
BrayanDSO committed Dec 27, 2023
1 parent 41c0499 commit 1818ab2
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 205 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import com.ichi2.anki.pages.CardInfo.Companion.toIntent
import com.ichi2.anki.pages.CardInfoDestination
import com.ichi2.anki.pages.DeckOptions
import com.ichi2.anki.pages.PageFragment
import com.ichi2.anki.pages.PagesActivity
import com.ichi2.anki.pages.Statistics
import com.ichi2.anki.tests.InstrumentedTest
import com.ichi2.annotations.NeedsTest
Expand All @@ -40,10 +39,10 @@ import org.junit.runners.Parameterized

@RunWith(Parameterized::class)
@NeedsTest("extend this for all activities - Issue 15009")
class PagesActivityTest : InstrumentedTest() {
class PagesTest : InstrumentedTest() {
@JvmField // required for Parameter
@Parameterized.Parameter
var intentBuilder: (PagesActivityTest.(Context) -> Intent)? = null
var intentBuilder: (PagesTest.(Context) -> Intent)? = null

@JvmField // required for Parameter
@Parameterized.Parameter(1)
Expand All @@ -54,7 +53,7 @@ class PagesActivityTest : InstrumentedTest() {
@Test
fun activityOpens() {
val intent = intentBuilder!!.invoke(this, testContext)
ActivityScenario.launch<PagesActivity>(intent).use { activity ->
ActivityScenario.launch<SingleFragmentActivity>(intent).use { activity ->
// this can fail on a real device if the screen is off
assertThat("state is RESUMED", activity.state == Lifecycle.State.RESUMED)
}
Expand All @@ -68,44 +67,44 @@ class PagesActivityTest : InstrumentedTest() {
@JvmStatic // required for initParameters
fun initParameters(): Collection<Array<out Any>> {
/** See [PageFragment] */
val intents = listOf<Pair<PagesActivityTest.(Context) -> Intent, String>>(
Pair(PagesActivityTest::getStatistics, "Statistics"),
Pair(PagesActivityTest::getCardInfo, "CardInfo"),
Pair(PagesActivityTest::getCongratsPage, "CongratsPage"),
Pair(PagesActivityTest::getDeckOptions, "DeckOptions"),
val intents = listOf<Pair<PagesTest.(Context) -> Intent, String>>(
Pair(PagesTest::getStatistics, "Statistics"),
Pair(PagesTest::getCardInfo, "CardInfo"),
Pair(PagesTest::getCongratsPage, "CongratsPage"),
Pair(PagesTest::getDeckOptions, "DeckOptions"),
// the following need a file path
Pair(PagesActivityTest::needsPath, "AnkiPackageImporterFragment"),
Pair(PagesActivityTest::needsPath, "CsvImporter"),
Pair(PagesActivityTest::needsPath, "ImageOcclusion")
Pair(PagesTest::needsPath, "AnkiPackageImporterFragment"),
Pair(PagesTest::needsPath, "CsvImporter"),
Pair(PagesTest::needsPath, "ImageOcclusion")
)

return intents.map { arrayOf(it.first, it.second) }
}
}
}

fun PagesActivityTest.getStatistics(context: Context): Intent {
fun PagesTest.getStatistics(context: Context): Intent {
return Statistics.getIntent(context)
}

fun PagesActivityTest.getCardInfo(context: Context): Intent {
fun PagesTest.getCardInfo(context: Context): Intent {
return addNoteUsingBasicModel().firstCard().let { card ->
this.card = card
CardInfoDestination(card.id).toIntent(context)
}
}

fun PagesActivityTest.getCongratsPage(context: Context): Intent {
fun PagesTest.getCongratsPage(context: Context): Intent {
return addNoteUsingBasicModel().firstCard().let { card ->
this.card = card
CardInfoDestination(card.id).toIntent(context)
}
}
fun PagesActivityTest.getDeckOptions(context: Context): Intent {
fun PagesTest.getDeckOptions(context: Context): Intent {
return DeckOptions.getIntent(context, col.decks.allNamesAndIds().first().id)
}

fun PagesActivityTest.needsPath(@Suppress("UNUSED_PARAMETER") context: Context): Intent {
fun PagesTest.needsPath(@Suppress("UNUSED_PARAMETER") context: Context): Intent {
assumeThat("not implemented: path needed", false, equalTo(true))
TODO()
}
4 changes: 0 additions & 4 deletions AnkiDroid/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,6 @@
android:exported="false"
android:configChanges="keyboardHidden|orientation|screenSize"
/>
<activity
android:name="com.ichi2.anki.pages.PagesActivity"
android:exported="true"
/>
<activity
android:name="com.ichi2.anki.IntentHandler"
android:configChanges="keyboardHidden|orientation|screenSize"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.activity.OnBackPressedCallback
import androidx.core.os.bundleOf
import com.ichi2.anki.CollectionManager
import com.ichi2.anki.R
import com.ichi2.anki.SingleFragmentActivity
import com.ichi2.anki.hideShowButtonCss

class AnkiPackageImporterFragment : PageFragment() {
Expand Down Expand Up @@ -86,7 +87,7 @@ class AnkiPackageImporterFragment : PageFragment() {

fun getIntent(context: Context, filePath: String): Intent {
val args = bundleOf(ARG_FILE_PATH to filePath)
return PagesActivity.getIntent(context, AnkiPackageImporterFragment::class, args)
return SingleFragmentActivity.getIntent(context, AnkiPackageImporterFragment::class, args)
}
}
}
3 changes: 2 additions & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/pages/CardInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import android.os.Bundle
import android.webkit.WebView
import androidx.core.os.bundleOf
import com.ichi2.anki.R
import com.ichi2.anki.SingleFragmentActivity
import com.ichi2.libanki.CardId

class CardInfo : PageFragment() {
Expand Down Expand Up @@ -52,7 +53,7 @@ class CardInfo : PageFragment() {
private const val ARG_CARD_ID = "cardId"

fun CardInfoDestination.toIntent(context: Context): Intent =
PagesActivity.getIntent(context, CardInfo::class, bundleOf(ARG_CARD_ID to cardId))
SingleFragmentActivity.getIntent(context, CardInfo::class, bundleOf(ARG_CARD_ID to cardId))
}
}

Expand Down
3 changes: 2 additions & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/pages/CongratsPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package com.ichi2.anki.pages

import android.content.Context
import android.content.Intent
import com.ichi2.anki.SingleFragmentActivity

class CongratsPage : PageFragment() {
override val title: String = ""
Expand All @@ -26,7 +27,7 @@ class CongratsPage : PageFragment() {

companion object {
fun getIntent(context: Context): Intent {
return PagesActivity.getIntent(context, CongratsPage::class)
return SingleFragmentActivity.getIntent(context, CongratsPage::class)
}
}
}
5 changes: 3 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/anki/pages/CsvImporter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import android.content.Intent
import android.os.Bundle
import android.webkit.WebView
import com.ichi2.anki.R
import com.ichi2.anki.SingleFragmentActivity

/**
* Anki page used to import text/csv files
Expand Down Expand Up @@ -53,13 +54,13 @@ class CsvImporter : PageFragment() {

/**
* @param filePath path of the csv file that will be imported, which should be accessible by AnkiDroid
* @return an intent to open the [CsvImporter] page on [PagesActivity]
* @return an intent to open the [CsvImporter] page on [SingleFragmentActivity]
*/
fun getIntent(context: Context, filePath: String): Intent {
val arguments = Bundle().apply {
putString(ARG_KEY_PATH, filePath)
}
return PagesActivity.getIntent(context, CsvImporter::class, arguments)
return SingleFragmentActivity.getIntent(context, CsvImporter::class, arguments)
}
}
}
3 changes: 2 additions & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/pages/DeckOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.fragment.app.FragmentActivity
import anki.collection.OpChanges
import com.ichi2.anki.CollectionManager
import com.ichi2.anki.R
import com.ichi2.anki.SingleFragmentActivity
import com.ichi2.libanki.undoableOp
import com.ichi2.libanki.updateDeckConfigsRaw
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -58,7 +59,7 @@ class DeckOptions : PageFragment() {
val arguments = Bundle().apply {
putLong(ARG_DECK_ID, deckId)
}
return PagesActivity.getIntent(context, DeckOptions::class, arguments)
return SingleFragmentActivity.getIntent(context, DeckOptions::class, arguments)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import android.os.Bundle
import android.webkit.WebView
import androidx.core.os.bundleOf
import com.ichi2.anki.CollectionManager.TR
import com.ichi2.anki.SingleFragmentActivity
import org.json.JSONObject

class ImageOcclusion : PageFragment() {
Expand Down Expand Up @@ -62,7 +63,7 @@ class ImageOcclusion : PageFragment() {

fun getIntent(context: Context, kind: String, noteOrNotetypeId: Long, imagePath: String?): Intent {
val arguments = bundleOf(ARG_KEY_KIND to kind, ARG_KEY_ID to noteOrNotetypeId, ARG_KEY_PATH to imagePath)
return PagesActivity.getIntent(context, ImageOcclusion::class, arguments)
return SingleFragmentActivity.getIntent(context, ImageOcclusion::class, arguments)
}
}
}
28 changes: 23 additions & 5 deletions AnkiDroid/src/main/java/com/ichi2/anki/pages/PageFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,23 @@ import android.view.View
import android.view.ViewGroup
import android.webkit.WebView
import androidx.fragment.app.Fragment
import com.google.android.material.appbar.MaterialToolbar
import com.ichi2.anki.R
import com.ichi2.themes.Themes
import timber.log.Timber

/**
* Base class for displaying Anki HTML pages
*
* @see [PagesActivity]
*/
abstract class PageFragment : Fragment() {
@Suppress("LeakingThis")
abstract class PageFragment : Fragment(), PostRequestHandler {
abstract val title: String
abstract val pageName: String
abstract var webViewClient: PageWebViewClient
abstract var webChromeClient: PageChromeClient

lateinit var webView: WebView
private val server = AnkiServer(this).also { it.start() }

override fun onCreateView(
inflater: LayoutInflater,
Expand All @@ -45,17 +46,34 @@ abstract class PageFragment : Fragment() {
): View? {
val view = inflater.inflate(R.layout.page_fragment, container, false)

webView = view.findViewById<WebView>(R.id.pagesWebview).apply {
webView = view.findViewById<WebView>(R.id.webview).apply {
settings.javaScriptEnabled = true
webViewClient = this@PageFragment.webViewClient
webChromeClient = this@PageFragment.webChromeClient
}
val nightMode = if (Themes.currentTheme.isNightMode) "#night" else ""
val url = (requireActivity() as PagesActivity).baseUrl() + "$pageName.html$nightMode"
val url = server.baseUrl() + "$pageName.html$nightMode"

Timber.i("Loading $url")
webView.loadUrl(url)

view.findViewById<MaterialToolbar>(R.id.toolbar).apply {
title = this@PageFragment.title
setNavigationOnClickListener {
requireActivity().onBackPressedDispatcher.onBackPressed()
}
}
return view
}

override suspend fun handlePostRequest(uri: String, bytes: ByteArray): ByteArray {
val methodName = if (uri.startsWith(AnkiServer.ANKI_PREFIX)) {
uri.substring(AnkiServer.ANKI_PREFIX.length)
} else {
throw IllegalArgumentException("unhandled request: $uri")
}
return requireActivity().handleUiPostRequest(methodName, bytes)
?: handleCollectionPostRequest(methodName, bytes)
?: throw IllegalArgumentException("unhandled method: $methodName")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ open class PageWebViewClient : WebViewClient() {

/** [PageFragment.webView] is invisible by default to avoid flashes while
* the page is loaded, and can be made visible again after it finishes loading */
view.isVisible = true
webView.isVisible = true
}
}
}
113 changes: 0 additions & 113 deletions AnkiDroid/src/main/java/com/ichi2/anki/pages/PagesActivity.kt

This file was deleted.

Loading

0 comments on commit 1818ab2

Please sign in to comment.