Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ShowkaseBrowserFragment to be used as a replacement of ShowkaseBrowserActivity #351

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add ShowkaseBrowser composable to be used as a replacement of Showkas…
…eBrowserActivity
MohamedHarmoush committed Nov 27, 2023
commit 424ee278b6cf6f157a17be62d83499429bac01e6
Original file line number Diff line number Diff line change
@@ -9,8 +9,14 @@ import com.airbnb.android.showkase.models.ShowkaseProvider

private const val AUTOGEN_CLASS_NAME = "Codegen"

/**
* composable entry point for Showkase
*
* @param classKey The name of the class with [@ShowkaseRoot] annotation
*
*/
@Composable
internal fun ShowkaseBrowserContent(
fun ShowkaseBrowser(
classKey: String,
) {
val (
Original file line number Diff line number Diff line change
@@ -5,85 +5,37 @@ import android.content.Intent
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import com.airbnb.android.showkase.exceptions.ShowkaseException
import com.airbnb.android.showkase.models.ShowkaseBrowserScreenMetadata
import com.airbnb.android.showkase.models.ShowkaseProvider
import com.airbnb.android.showkase.models.ShowkaseElementsMetadata

/**
* The activity that's responsible for showing all the UI elements that were annotated
* with the Showkase related annotations.
*/
class ShowkaseBrowserActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val classKey = intent.extras?.getString(SHOWKASE_ROOT_MODULE_KEY) ?: throw ShowkaseException(
"Missing key in bundle. Please start this activity by using the intent returned by " +
"the ShowkaseBrowserActivity.getIntent() method."
)
setContent {
val (
groupedComponentsList,
groupedColorsList,
groupedTypographyList
) = getShowkaseProviderElements(classKey)

val showkaseBrowserScreenMetadata =
remember { mutableStateOf(ShowkaseBrowserScreenMetadata()) }
when {
groupedComponentsList.isNotEmpty() || groupedColorsList.isNotEmpty() ||
groupedTypographyList.isNotEmpty() -> {
ShowkaseBrowserApp(
groupedComponentsList.groupBy { it.group },
groupedColorsList.groupBy { it.colorGroup },
groupedTypographyList.groupBy { it.typographyGroup },
showkaseBrowserScreenMetadata)
}
else -> {
ShowkaseErrorScreen(
errorText = "There were no elements that were annotated with either " +
"@ShowkaseComposable, @ShowkaseTypography or @ShowkaseColor. If " +
"you think this is a mistake, file an issue at " +
"https://github.com/airbnb/Showkase/issues"
)
}
}
}
}

private fun getShowkaseProviderElements(
classKey: String
): ShowkaseElementsMetadata {
return try {
val showkaseComponentProvider =
Class.forName("$classKey$AUTOGEN_CLASS_NAME").getDeclaredConstructor().newInstance()

val showkaseMetadata = (showkaseComponentProvider as ShowkaseProvider).metadata()

ShowkaseElementsMetadata(
componentList = showkaseMetadata.componentList,
colorList = showkaseMetadata.colorList,
typographyList = showkaseMetadata.typographyList
)
} catch (exception: ClassNotFoundException) {
ShowkaseElementsMetadata()
ShowkaseBrowser(classKey = classKey)
}
}

companion object {

private const val SHOWKASE_ROOT_MODULE_KEY = "SHOWKASE_ROOT_MODULE"
private const val AUTOGEN_CLASS_NAME = "Codegen"

/**
* Returns the intent that the users of this library need to use for starting the
* Showkase browser activity. Please make sure to use this instead of starting the
* activity directly as it sets the right value in the bundle in order for the activity
* to start correctly.
*
*
* @param context Android context
* @param rootModuleCanonicalName The canonical name of the implementation of
* @param rootModuleCanonicalName The canonical name of the implementation of
* ShowkaseRootModule.
*/
fun getIntent(context: Context, rootModuleCanonicalName: String) =
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ class ShowkaseBrowserFragment : Fragment() {

return ComposeView(requireContext()).apply {
setContent {
ShowkaseBrowserContent(classKey = classKey)
ShowkaseBrowser(classKey = classKey)
}
}
}