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

Introduce resource visibility to BasicResourceItem #778

Merged
merged 5 commits into from
May 11, 2023
Merged
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
@@ -1,4 +1,19 @@
package app.cash.paparazzi.internal
/*
* Copyright (C) 2023 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.cash.paparazzi.internal.resources

import com.android.SdkConstants.ATTR_FORMAT
import com.android.SdkConstants.ATTR_NAME
Expand All @@ -24,26 +39,32 @@ import com.android.ide.common.rendering.api.StyleItemResourceValueImpl
import com.android.ide.common.rendering.api.StyleResourceValueImpl
import com.android.ide.common.rendering.api.StyleableResourceValueImpl
import com.android.ide.common.rendering.api.TextResourceValueImpl
import com.android.ide.common.resources.ResourceItem
import com.android.ide.common.resources.ResourceItemWithVisibility
import com.android.ide.common.resources.SingleNamespaceResourceRepository
import com.android.ide.common.resources.ValueXmlHelper
import com.android.ide.common.resources.configuration.FolderConfiguration
import com.android.ide.common.util.PathString
import com.android.resources.ResourceType
import com.android.resources.ResourceType.PUBLIC
import com.android.resources.ResourceUrl
import com.android.resources.ResourceVisibility
import com.android.utils.XmlUtils
import org.w3c.dom.Element
import java.io.File
import java.util.EnumSet

class BasicResourceItem(
type: ResourceType,
private val name: String,
visibility: ResourceVisibility,
file: File,
tag: Element?,
private val name: String,
private val type: ResourceType,
private val repository: SingleNamespaceResourceRepository
) : ResourceItem {
) : ResourceItemWithVisibility {
// Store enums as their ordinals in byte form to minimize memory footprint.
private val typeOrdinal: Byte
private val visibilityOrdinal: Byte

private val resourceValue: ResourceValue

private val folderConfiguration: FolderConfiguration =
Expand All @@ -53,46 +74,60 @@ class BasicResourceItem(

private val isFileBased = tag == null

override fun getConfiguration() = folderConfiguration
init {
typeOrdinal = type.ordinal.toByte()
visibilityOrdinal = visibility.ordinal.toByte()

override fun getName() = name
resourceValue = if (tag == null || type == PUBLIC) {
val density =
if (type == ResourceType.DRAWABLE || type == ResourceType.MIPMAP) configuration.densityQualifier?.value else null
val path = file.absolutePath
if (density != null) {
DensityBasedResourceValueImpl(namespace, type, name, path, density, null)
} else {
ResourceValueImpl(namespace, type, name, path, null)
}
} else {
parseXmlToResourceValueSafe(tag)
}
}

override fun getType() = type
override fun getType() = ResourceType.values()[typeOrdinal.toInt()]

override fun getNamespace(): ResourceNamespace = repository.namespace

override fun getName() = name

override fun getLibraryName() = null

override fun getRepository() = repository
override fun getVisibility() = ResourceVisibility.values()[visibilityOrdinal.toInt()]

override fun getReferenceToSelf(): ResourceReference =
ResourceReference(namespace, type, name)

override fun getRepository() = repository

override fun getConfiguration() = folderConfiguration

override fun getKey(): String {
val qualifiers = configuration.qualifierString
return if (qualifiers.isNotEmpty()) {
(type.getName() + '-') + qualifiers + '/' + name
} else type.getName() + '/' + name
}

init {
resourceValue = if (tag == null || type == PUBLIC) {
val density =
if (type == ResourceType.DRAWABLE || type == ResourceType.MIPMAP) configuration.densityQualifier?.value else null
val path = file.absolutePath
if (density != null) {
DensityBasedResourceValueImpl(namespace, type, name, path, density, null)
} else {
ResourceValueImpl(namespace, type, name, path, null)
}
"${type.getName()}-$qualifiers/$name"
} else {
parseXmlToResourceValueSafe(tag)
"${type.getName()}/$name"
}
}

override fun getResourceValue(): ResourceValue {
return resourceValue
}
override fun getResourceValue(): ResourceValue = resourceValue

override fun getSource() = source

override fun isFileBased() = isFileBased

/**
* Returns the text content of a given tag
*/
private fun getTextContent(tag: Element): String = tag.textContent

private fun parseXmlToResourceValueSafe(tag: Element): ResourceValue {
val value: ResourceValueImpl = when (type) {
Expand Down Expand Up @@ -272,13 +307,6 @@ class BasicResourceItem(
return XmlUtils.getPreviousCommentText(tag)
}

/**
* Returns the text content of a given tag
*/
open fun getTextContent(tag: Element): String {
return tag.textContent
}

/**
* Returns a [ResourceNamespace.Resolver] for the specified tag.
*/
Expand All @@ -291,8 +319,4 @@ class BasicResourceItem(
override fun prefixToUri(namespacePrefix: String): String? = null
}
}

override fun getSource() = source

override fun isFileBased() = isFileBased
}