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

Clean up & documentation #424

Merged
merged 4 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,35 @@ import com.squareup.kotlinpoet.DelicateKotlinPoetApi
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.ParameterizedTypeName
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.TypeSpec
import kotlinx.serialization.Contextual
import kotlinx.serialization.Serializable

/**
* An [ApolloCompilerPlugin] to add some extra annotations and interfaces to the classes generated from the graphql schema
*/
class StashApolloCompilerPlugin : ApolloCompilerPlugin {
override fun kotlinOutputTransform(): Transform<KotlinOutput> {
return object : Transform<KotlinOutput> {
override fun transform(input: KotlinOutput): KotlinOutput {
val sampleFileSpec = input.fileSpecs.first()
val packageName = "com.github.damontecres.stashapp.api"

// Create an interface for "data" types (Performer, Scene, etc)
// It has an id property and is sealed which allows for Serializable
val stashDataInterface = ClassName("$packageName.fragment", "StashData")
val stashDataFileSpec =
FileSpec.builder(stashDataInterface)
.addType(
TypeSpec.interfaceBuilder(stashDataInterface)
.addModifiers(KModifier.SEALED)
.addProperty("id", String::class)
.addModifiers(KModifier.SEALED)
.addAnnotation(Serializable::class)
.build(),
)
.build()

// Create an interface for "filter" types (PerformerFilterType, etc)
// It is sealed which allows for Serializable
val stashFilterInterface = ClassName("$packageName.type", "StashDataFilter")
val stashFilterFileSpec =
FileSpec.builder(stashFilterInterface)
Expand All @@ -44,8 +51,11 @@ class StashApolloCompilerPlugin : ApolloCompilerPlugin {
val newFileSpecs =
input.fileSpecs.map { file ->
if (file.name.endsWith("FilterType") || file.name.endsWith("CriterionInput")) {
// Modify filter or filter input types
handleFilterInput(file, stashFilterInterface)
} else if (file.name.endsWith("Data")) {
// Modify data types
// Note that fragments for data types by convention are suffixed with "Data"
handleData(file, stashDataInterface)
} else {
file
Expand All @@ -67,13 +77,16 @@ class StashApolloCompilerPlugin : ApolloCompilerPlugin {
val builder = file.toBuilder()
builder.members.replaceAll { member ->
if (member is TypeSpec) {
// Mark as Serializable
val typeBuilder =
member.toBuilder()
.addAnnotation(Serializable::class.java)
typeBuilder.propertySpecs.replaceAll { prop ->
if (prop.type is ParameterizedTypeName &&
(prop.type as ParameterizedTypeName).rawType.canonicalName == "com.apollographql.apollo.api.Optional"
) {
// If the property is an Optional (basically all of them), then add a Contextual annotation
// This allows for runtime serialization, because the app defines a serializer for this class
prop.toBuilder()
.addAnnotation(Contextual::class)
.build()
Expand All @@ -82,55 +95,40 @@ class StashApolloCompilerPlugin : ApolloCompilerPlugin {
}
}

// If the type is a filter, add the interface
if (member.name!!.endsWith("FilterType")) {
typeBuilder.addSuperinterface(stashFilterInterface)
}

typeBuilder.build()
} else if (member is PropertySpec) {
} else {
member
}
}
return builder.build()
}

@OptIn(DelicateKotlinPoetApi::class)
private fun handleData(
file: FileSpec,
stashDataInterface: ClassName,
): FileSpec {
val builder = file.toBuilder()
builder.members.replaceAll { member ->
if (member is TypeSpec && member.propertySpecs.find { it.name == "id" } != null) {
// Mark the type with the data interface
val memberBuilder =
member.toBuilder()
.addSuperinterface(stashDataInterface)
// TODO: adding Serializable i
// .addAnnotation(Serializable::class)
memberBuilder.propertySpecs.replaceAll {
if (it.name == "id") {
// If the property is named id, need to add override due to the super interface
it.toBuilder()
.addModifiers(KModifier.OVERRIDE)
.build()
} else if (it.name == "updated_at" || it.name == "created_at") {
it.toBuilder()
// .addAnnotation(Contextual::class)
.build()
} else {
it
}
}
memberBuilder.typeSpecs.replaceAll { innerType ->
if (innerType.modifiers.contains(KModifier.DATA)) {
// Inner data class
innerType.toBuilder()
// .addAnnotation(Serializable::class)
.build()
} else {
innerType
}
}
memberBuilder.build()
} else {
member
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/graphql/FindImages.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
query FindImages($filter: FindFilterType, $image_filter: ImageFilterType) {
findImages(filter: $filter, image_filter: $image_filter) {
query FindImages($filter: FindFilterType, $image_filter: ImageFilterType, $ids: [ID!]) {
findImages(filter: $filter, image_filter: $image_filter, ids: $ids) {
images {
...ImageData
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ import com.github.damontecres.stashapp.util.StashServer
import com.github.damontecres.stashapp.util.plugin.CompanionPlugin
import kotlinx.coroutines.launch

/**
* Activity to show various debugging information
*
* Not really intended for the average user to use
*/
class DebugActivity : FragmentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import androidx.appcompat.widget.ListPopupWindow
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.commit
import androidx.lifecycle.lifecycleScope
import androidx.preference.PreferenceManager
import com.chrynan.parcelable.core.getParcelableExtra
import com.github.damontecres.stashapp.data.DataType
import com.github.damontecres.stashapp.filter.CreateFilterActivity
import com.github.damontecres.stashapp.filter.FilterOptions
Expand All @@ -27,8 +25,8 @@ import com.github.damontecres.stashapp.util.FilterParser
import com.github.damontecres.stashapp.util.QueryEngine
import com.github.damontecres.stashapp.util.StashCoroutineExceptionHandler
import com.github.damontecres.stashapp.util.StashServer
import com.github.damontecres.stashapp.util.getFilterArgs
import com.github.damontecres.stashapp.util.getMaxMeasuredWidth
import com.github.damontecres.stashapp.util.parcelable
import com.github.damontecres.stashapp.util.putDataType
import com.github.damontecres.stashapp.util.putFilterArgs
import com.github.damontecres.stashapp.views.PlayAllOnClickListener
Expand All @@ -52,8 +50,6 @@ class FilterListActivity : FragmentActivity(R.layout.filter_list) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val preferences = PreferenceManager.getDefaultSharedPreferences(this)

filterButton = findViewById(R.id.filter_button)
filterButton.setOnClickListener {
Toast.makeText(this, "Filters not loaded yet!", Toast.LENGTH_SHORT).show()
Expand Down Expand Up @@ -85,8 +81,7 @@ class FilterListActivity : FragmentActivity(R.layout.filter_list) {
}
}

val startingFilter =
intent.getParcelableExtra(INTENT_FILTER_ARGS, FilterArgs::class, 0, parcelable)!!
val startingFilter = intent.getFilterArgs(INTENT_FILTER_ARGS)!!
if (savedInstanceState == null) {
setup(startingFilter, first = true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import com.github.damontecres.stashapp.util.StashCoroutineExceptionHandler
import com.github.damontecres.stashapp.util.StashFragmentPagerAdapter
import com.github.damontecres.stashapp.util.StashGlide
import com.github.damontecres.stashapp.util.StashServer
import com.github.damontecres.stashapp.util.getParcelable
import com.github.damontecres.stashapp.util.isNotNullOrBlank
import com.github.damontecres.stashapp.util.putFilterArgs
import com.github.damontecres.stashapp.util.showSetRatingToast
Expand All @@ -45,7 +46,7 @@ class GalleryFragment : TabbedFragment() {
private lateinit var gallery: Gallery

override fun onCreate(savedInstanceState: Bundle?) {
gallery = requireActivity().intent.getParcelableExtra(INTENT_GALLERY_OBJ)!!
gallery = requireActivity().intent.getParcelable(INTENT_GALLERY_OBJ, Gallery::class)!!
super.onCreate(savedInstanceState)
viewModel.title.value = gallery.name
}
Expand All @@ -60,7 +61,7 @@ class GalleryFragment : TabbedFragment() {
) :
StashFragmentPagerAdapter(
listOf(
PagerEntry("Details", null),
PagerEntry("Details"),
PagerEntry(DataType.IMAGE),
PagerEntry(DataType.SCENE),
PagerEntry(DataType.PERFORMER),
Expand Down Expand Up @@ -148,7 +149,7 @@ class GalleryFragment : TabbedFragment() {
super.onViewCreated(view, savedInstanceState)

if (savedInstanceState != null) {
val gallery = savedInstanceState.getParcelable<Gallery>("gallery")
val gallery = savedInstanceState.getParcelable("gallery", Gallery::class)
if (gallery != null) {
this.gallery = gallery
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import androidx.fragment.app.commitNow
import androidx.lifecycle.lifecycleScope
import androidx.preference.PreferenceManager
import com.apollographql.apollo.api.Optional
import com.chrynan.parcelable.core.getParcelableExtra
import com.github.damontecres.stashapp.api.CountImagesQuery
import com.github.damontecres.stashapp.api.FindImagesQuery
import com.github.damontecres.stashapp.api.fragment.ImageData
Expand All @@ -26,15 +25,14 @@ import com.github.damontecres.stashapp.image.ImageDetailsFragment
import com.github.damontecres.stashapp.image.ImageFragment
import com.github.damontecres.stashapp.image.ImageViewModel
import com.github.damontecres.stashapp.suppliers.DataSupplierFactory
import com.github.damontecres.stashapp.suppliers.FilterArgs
import com.github.damontecres.stashapp.suppliers.ImageDataSupplier
import com.github.damontecres.stashapp.suppliers.StashPagingSource
import com.github.damontecres.stashapp.suppliers.StashSparseFilterFetcher
import com.github.damontecres.stashapp.util.QueryEngine
import com.github.damontecres.stashapp.util.StashCoroutineExceptionHandler
import com.github.damontecres.stashapp.util.StashServer
import com.github.damontecres.stashapp.util.getFilterArgs
import com.github.damontecres.stashapp.util.isImageClip
import com.github.damontecres.stashapp.util.parcelable
import kotlinx.coroutines.launch

class ImageActivity : FragmentActivity(R.layout.activity_image) {
Expand Down Expand Up @@ -106,8 +104,7 @@ class ImageActivity : FragmentActivity(R.layout.activity_image) {
}

private fun createDataSupplier(): ImageDataSupplier? {
val filterArgs: FilterArgs? =
intent.getParcelableExtra(INTENT_FILTER_ARGS, FilterArgs::class, 0, parcelable)
val filterArgs = intent.getFilterArgs(INTENT_FILTER_ARGS)
val galleryId = intent.getStringExtra(INTENT_GALLERY_ID)
if (filterArgs != null) {
return DataSupplierFactory(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import android.widget.TextView
import com.github.damontecres.stashapp.util.concatIfNotBlank
import java.io.BufferedReader

/**
* Show various licenses for third party libraries included in the app
*/
class LicenseActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class MainActivity : FragmentActivity() {
}
}

@Deprecated("Deprecated in Java")
override fun onBackPressed() {
if (!fragment.onBackPressed()) {
super.onBackPressed()
Expand Down
25 changes: 16 additions & 9 deletions app/src/main/java/com/github/damontecres/stashapp/MainFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,17 @@ class MainFragment : BrowseSupportFragment() {
StashServer.requireCurrentServer().serverPreferences
.updatePreferences(config)

val ui = config.configuration.ui
val ui = config.configuration.ui as Map<*, *>
val frontPageContent =
(ui as Map<String, *>).getCaseInsensitive("frontPageContent") as List<Map<String, *>>
ui.getCaseInsensitive("frontPageContent") as List<Map<String, *>>?
if (frontPageContent == null) {
Toast.makeText(
requireContext(),
"Unable to find front page content! Check the Web UI.",
Toast.LENGTH_LONG,
).show()
return@launch
}
val pageSize =
PreferenceManager.getDefaultSharedPreferences(requireContext())
.getInt(getString(R.string.pref_key_page_size), 25)
Expand All @@ -271,21 +279,20 @@ class MainFragment : BrowseSupportFragment() {
val jobs = frontPageParser.parse(frontPageContent)
jobs.forEachIndexed { index, job ->
job.await().let { row ->
if (row.successful) {
val rowData = row.data!!
filterList.add(rowData.filter)
if (row is FrontPageParser.FrontPageRow.Success) {
filterList.add(row.filter)

val adapter = ArrayObjectAdapter(StashPresenter.SELECTOR)
adapter.addAll(0, rowData.data)
adapter.add(rowData.filter)
adapter.addAll(0, row.data)
adapter.add(row.filter)
adapters.add(adapter)
withContext(Dispatchers.Main) {
rowsAdapter.set(
index,
ListRow(HeaderItem(rowData.name), adapter),
ListRow(HeaderItem(row.name), adapter),
)
}
} else if (row.result == FrontPageParser.FrontPageRowResult.ERROR) {
} else if (row is FrontPageParser.FrontPageRow.Error) {
withContext(Dispatchers.Main) {
Toast.makeText(
requireContext(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultCallback
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.activityViewModels
Expand Down Expand Up @@ -56,6 +55,7 @@ import com.github.damontecres.stashapp.util.StashCoroutineExceptionHandler
import com.github.damontecres.stashapp.util.StashGlide
import com.github.damontecres.stashapp.util.StashServer
import com.github.damontecres.stashapp.util.convertDpToPixel
import com.github.damontecres.stashapp.util.getParcelable
import com.github.damontecres.stashapp.util.isNotNullOrBlank
import com.github.damontecres.stashapp.views.MarkerPickerFragment
import com.github.damontecres.stashapp.views.StashItemViewClickListener
Expand All @@ -65,9 +65,10 @@ import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch

/**
* Shows details and editable actions for a scene marker
*/
class MarkerActivity : FragmentActivity() {
private val viewModel by viewModels<MarkerDetailsViewModel>()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_details)
Expand Down Expand Up @@ -172,7 +173,7 @@ class MarkerActivity : FragmentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val marker = requireActivity().intent.getParcelableExtra<Marker>("marker")!!
val marker = requireActivity().intent.getParcelable("marker", Marker::class)!!
viewModel.setMarker(marker)

primaryTagRowManager.name = getString(R.string.stashapp_primary_tag)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.github.damontecres.stashapp.util.QueryEngine
import com.github.damontecres.stashapp.util.StashCoroutineExceptionHandler
import com.github.damontecres.stashapp.util.StashGlide
import com.github.damontecres.stashapp.util.StashServer
import com.github.damontecres.stashapp.util.getParcelable
import com.github.damontecres.stashapp.views.parseTimeToString
import kotlinx.coroutines.launch
import kotlin.time.DurationUnit
Expand All @@ -39,7 +40,7 @@ class MovieDetailsFragment : Fragment(R.layout.movie_view) {

table = view.findViewById(R.id.movie_table)

val movie = requireActivity().intent.getParcelableExtra<Movie>("movie")!!
val movie = requireActivity().intent.getParcelable("movie", Movie::class)!!
if (movie.frontImagePath != null) {
StashGlide.with(requireActivity(), movie.frontImagePath)
.error(StashPresenter.glideError(requireContext()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@ import com.github.damontecres.stashapp.data.Movie
import com.github.damontecres.stashapp.data.SortAndDirection
import com.github.damontecres.stashapp.data.StashFindFilter
import com.github.damontecres.stashapp.util.StashFragmentPagerAdapter
import com.github.damontecres.stashapp.util.getParcelable

class MovieFragment : TabbedFragment() {
private lateinit var movie: Movie

override fun onCreate(savedInstanceState: Bundle?) {
movie = requireActivity().intent.getParcelableExtra<Movie>("movie")!!
movie = requireActivity().intent.getParcelable("movie", Movie::class)!!
super.onCreate(savedInstanceState)
}

override fun getPagerAdapter(fm: FragmentManager): StashFragmentPagerAdapter {
val pages =
listOf(
StashFragmentPagerAdapter.PagerEntry(getString(R.string.stashapp_details), null),
StashFragmentPagerAdapter.PagerEntry(getString(R.string.stashapp_details)),
StashFragmentPagerAdapter.PagerEntry(DataType.SCENE),
)
return object : StashFragmentPagerAdapter(pages, fm) {
Expand Down
Loading