Skip to content

Commit

Permalink
[1.120.*] Pre-release merge (#439)
Browse files Browse the repository at this point in the history
  • Loading branch information
tramline-github[bot] authored Apr 11, 2024
2 parents e4d0804 + c6a5717 commit 93857c2
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 13 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[versions]
kotlin = "1.9.23"
android_gradle_plugin = "8.3.2"
compose = "1.6.1"
compose = "1.6.2"

android_sdk_compile = "34"
android_sdk_target = "34"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2024 Sasikanth Miriyampalli
*
* 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 dev.sasikanth.rss.reader.utils

import com.benasher44.uuid.UuidHasher
import java.security.MessageDigest

internal actual fun hasher(): UuidHasher {
return JvmHasher("SHA-1", 5)
}

// Copied from:
// https://github.com/benasher44/uuid/blob/f3768dd19fdd58ac01711733923d7db5a433ac79/src/jvmMain/kotlin/namebased.kt#L33
private class JvmHasher(
algorithmName: String,
override val version: Int,
) : UuidHasher {
private val digest = MessageDigest.getInstance(algorithmName)

override fun update(input: ByteArray) {
digest.update(input)
}

override fun digest(): ByteArray {
return digest.digest()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,19 @@ internal fun FeaturedSection(
snapAnimationSpec = spring(stiffness = Spring.StiffnessVeryLow)
)
) { page ->
val featuredPost = featuredPosts[page]

FeaturedPostItem(
item = featuredPost,
page = page,
pagerState = pagerState,
onClick = { onItemClick(featuredPost) },
onBookmarkClick = { onPostBookmarkClick(featuredPost) },
onCommentsClick = { onPostCommentsClick(featuredPost.commentsLink!!) },
onSourceClick = { onPostSourceClick(featuredPost.feedLink) },
onTogglePostReadClick = { onTogglePostReadClick(featuredPost.link, featuredPost.read) }
)
val featuredPost = featuredPosts.getOrNull(page)
if (featuredPost != null) {
FeaturedPostItem(
item = featuredPost,
page = page,
pagerState = pagerState,
onClick = { onItemClick(featuredPost) },
onBookmarkClick = { onPostBookmarkClick(featuredPost) },
onCommentsClick = { onPostCommentsClick(featuredPost.commentsLink!!) },
onSourceClick = { onPostSourceClick(featuredPost.feedLink) },
onTogglePostReadClick = { onTogglePostReadClick(featuredPost.link, featuredPost.read) }
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2024 Sasikanth Miriyampalli
*
* 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 dev.sasikanth.rss.reader.utils

import com.benasher44.uuid.Uuid
import com.benasher44.uuid.UuidHasher
import com.benasher44.uuid.uuidOf
import kotlin.experimental.and
import kotlin.experimental.or

// Number of bytes in a UUID
internal const val UUID_BYTES = 16

internal fun nameBasedUuidOf(value: String): Uuid {
val hasher = hasher()
hasher.update(value.encodeToByteArray())
val hashedBytes = hasher.digest()
hashedBytes[6] =
hashedBytes[6]
.and(0b00001111) // clear the 4 most sig bits
.or(hasher.version.shl(4).toByte())
hashedBytes[8] =
hashedBytes[8]
.and(0b00111111) // clear the 2 most sig bits
.or(-0b10000000) // set 2 most sig to 10
return uuidOf(hashedBytes.copyOf(UUID_BYTES))
}

internal expect fun hasher(): UuidHasher
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2024 Sasikanth Miriyampalli
*
* 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 dev.sasikanth.rss.reader.utils

import com.benasher44.uuid.UuidHasher
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.addressOf
import kotlinx.cinterop.reinterpret
import kotlinx.cinterop.usePinned
import platform.CoreCrypto.CC_SHA1
import platform.CoreCrypto.CC_SHA1_DIGEST_LENGTH

internal actual fun hasher(): UuidHasher {
return AppleHasher(AppleHasher.Companion::sha1Digest, 5)
}

// Copied from:
// https://github.com/benasher44/uuid/blob/f3768dd19fdd58ac01711733923d7db5a433ac79/src/appleMain/kotlin/namebased.kt#L40
@OptIn(ExperimentalForeignApi::class)
private class AppleHasher(
private val digestFunc: (ByteArray) -> ByteArray,
override val version: Int,
) : UuidHasher {
private var data = ByteArray(0)

override fun update(input: ByteArray) {
val prevLength = data.size
data = data.copyOf(data.size + input.size)
input.copyInto(data, prevLength)
}

override fun digest(): ByteArray {
return digestFunc(data)
}

companion object {
fun sha1Digest(data: ByteArray): ByteArray {
return ByteArray(CC_SHA1_DIGEST_LENGTH).also { bytes ->
bytes.usePinned { digestPin ->
data.usePinned { dataPin ->
CC_SHA1(dataPin.addressOf(0), data.size.toUInt(), digestPin.addressOf(0).reinterpret())
}
}
}
}
}
}

0 comments on commit 93857c2

Please sign in to comment.