Skip to content

Commit

Permalink
refactor: add SingleFragmentActivity class
Browse files Browse the repository at this point in the history
  • Loading branch information
BrayanDSO committed Dec 14, 2023
1 parent b4db9f8 commit 16f9d35
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 1 deletion.
4 changes: 4 additions & 0 deletions AnkiDroid/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,10 @@
android:label="@string/preview_title"
android:exported="false"
/>
<activity
android:name="com.ichi2.anki.SingleFragmentActivity"
android:exported="false"
/>
<activity
android:name="com.ichi2.anki.CardTemplatePreviewer"
android:label="@string/preview_title"
Expand Down
66 changes: 66 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/anki/SingleFragmentActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2023 Brayan Oliveira <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.ichi2.anki

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentContainerView
import androidx.fragment.app.commit
import com.ichi2.themes.setTransparentStatusBar
import com.ichi2.utils.getInstanceFromClassName
import kotlin.reflect.KClass
import kotlin.reflect.jvm.jvmName

/**
* Activity aimed to host a fragment on the entire screen.
* For that, it uses [R.layout.single_fragment_activity], which has only a [FragmentContainerView]
*
* Useful to avoid creating a Activity for every new screen
* while being able to reuse the fragment on other places.
*
* [getIntent] can be used as an easy way to build a [SingleFragmentActivity]
*/
class SingleFragmentActivity : AnkiActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
if (showedActivityFailedScreen(savedInstanceState)) {
return
}
super.onCreate(savedInstanceState)
setContentView(R.layout.single_fragment_activity)
setTransparentStatusBar()

val fragmentClassName = requireNotNull(intent.getStringExtra(FRAGMENT_NAME_EXTRA)) { "'fragmentName' should be provided" }
val fragment = getInstanceFromClassName<Fragment>(fragmentClassName).apply {
arguments = intent.getBundleExtra(FRAGMENT_ARGS_EXTRA)
}
supportFragmentManager.commit {
replace(R.id.fragment_container, fragment)
}
}
companion object {
private const val FRAGMENT_NAME_EXTRA = "fragmentName"
private const val FRAGMENT_ARGS_EXTRA = "fragmentArgs"

fun getIntent(context: Context, fragmentClass: KClass<out Fragment>, arguments: Bundle? = null): Intent {
return Intent(context, SingleFragmentActivity::class.java).apply {
putExtra(FRAGMENT_NAME_EXTRA, fragmentClass.jvmName)
putExtra(FRAGMENT_ARGS_EXTRA, arguments)
}
}
}
}
14 changes: 14 additions & 0 deletions AnkiDroid/src/main/res/layout/single_fragment_activity.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/root_layout"
tools:context=".SingleFragmentActivity">

<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ 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(SingleFragmentActivity::class.java.simpleName, "Implemented, but the test fails because the activity throws if a specific intent extra isn't set")
}

/**
Expand Down
3 changes: 2 additions & 1 deletion AnkiDroid/src/test/java/com/ichi2/testutils/ActivityList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ object ActivityList {
get(IntroductionActivity::class.java),
get(ManageNotetypes::class.java),
get(ManageSpaceActivity::class.java),
get(PermissionsActivity::class.java)
get(PermissionsActivity::class.java),
get(SingleFragmentActivity::class.java)
)
}

Expand Down

0 comments on commit 16f9d35

Please sign in to comment.