Skip to content

Commit

Permalink
chore: public API tweaks
Browse files Browse the repository at this point in the history
- make `RoomReactionsOptions` and `OccupancyOptions` classes instead of objects to improve future backward compatability
- remove `LocalDateTime.now()` and associated desugaring dependencies
- do not run unit tests for release build type (to avoid running all tests twice)
ttypic committed Dec 4, 2024
1 parent 6db9646 commit b84b113
Showing 8 changed files with 67 additions and 17 deletions.
6 changes: 4 additions & 2 deletions chat-android/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -33,7 +33,6 @@ android {
}

compileOptions {
isCoreLibraryDesugaringEnabled = true
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
@@ -53,7 +52,6 @@ dependencies {
api(libs.ably.android)
implementation(libs.gson)
implementation(libs.coroutine.core)
coreLibraryDesugaring(libs.desugar.jdk.libs)

testImplementation(libs.junit)
testImplementation(libs.mockk)
@@ -68,6 +66,10 @@ tasks.withType<Test>().configureEach {
testLogging {
exceptionFormat = TestExceptionFormat.FULL
}
// Skip tests for the "release" build type so we don't run tests twice
if (name.lowercase().contains("release")) {
enabled = false
}
}

tasks.withType<SourceJarTask>().configureEach {
11 changes: 7 additions & 4 deletions chat-android/src/main/java/com/ably/chat/Logger.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.ably.chat

import android.util.Log
import java.time.LocalDateTime
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

fun interface LogHandler {
fun log(message: String, level: LogLevel, throwable: Throwable?, context: LogContext)
@@ -90,13 +92,14 @@ internal class AndroidLogger(
}

override fun log(message: String, level: LogLevel, throwable: Throwable?, newTag: String?, newStaticContext: Map<String, String>) {
if (level.logLevelValue <= minimalVisibleLogLevel.logLevelValue) return
if (level.logLevelValue < minimalVisibleLogLevel.logLevelValue) return
val finalContext = context.mergeWith(newTag, newStaticContext)
val tag = finalContext.tag
val completeContext = finalContext.staticContext + finalContext.dynamicContext.mapValues { it.value() }

val contextString = ", context: $completeContext"
val formattedMessage = "[${LocalDateTime.now()}] $tag ${level.name} ably-chat: ${message}$contextString"
val currentTime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S", Locale.US).format(Date())
val formattedMessage = "$currentTime [$tag] (${level.name.uppercase()}) ably-chat: ${message}$contextString"
when (level) {
// We use Logcat's info level for Trace and Debug
LogLevel.Trace -> Log.i(tag, formattedMessage, throwable)
@@ -124,7 +127,7 @@ internal class CustomLogger(
}

override fun log(message: String, level: LogLevel, throwable: Throwable?, newTag: String?, newStaticContext: Map<String, String>) {
if (level.logLevelValue <= minimalVisibleLogLevel.logLevelValue) return
if (level.logLevelValue < minimalVisibleLogLevel.logLevelValue) return
val finalContext = context.mergeWith(newTag, newStaticContext)
logHandler.log(
message = message,
20 changes: 16 additions & 4 deletions chat-android/src/main/java/com/ably/chat/RoomOptions.kt
Original file line number Diff line number Diff line change
@@ -39,8 +39,8 @@ data class RoomOptions(
val default = RoomOptions(
typing = TypingOptions(),
presence = PresenceOptions(),
reactions = RoomReactionsOptions,
occupancy = OccupancyOptions,
reactions = RoomReactionsOptions(),
occupancy = OccupancyOptions(),
)
}
}
@@ -80,13 +80,25 @@ data class TypingOptions(

/**
* Represents the reactions options for a chat room.
*
* Note: This class is currently empty but allows for future extensions
* while maintaining backward compatibility.
*/
object RoomReactionsOptions
class RoomReactionsOptions {
override fun equals(other: Any?) = other is RoomReactionsOptions
override fun hashCode() = javaClass.hashCode()
}

/**
* Represents the occupancy options for a chat room.
*
* Note: This class is currently empty but allows for future extensions
* while maintaining backward compatibility.
*/
object OccupancyOptions
class OccupancyOptions {
override fun equals(other: Any?) = other is OccupancyOptions
override fun hashCode() = javaClass.hashCode()
}

/**
* Throws AblyException for invalid room configuration.
30 changes: 30 additions & 0 deletions chat-android/src/test/java/com/ably/chat/RoomOptionTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.ably.chat

import org.junit.Assert.assertEquals
import org.junit.Test

class RoomOptionTest {

@Test
fun `default occupancy options should be equal`() {
assertEquals(OccupancyOptions(), OccupancyOptions())
}

@Test
fun `default room reaction options should be equal`() {
assertEquals(RoomReactionsOptions(), RoomReactionsOptions())
}

@Test
fun `default room options should be equal`() {
assertEquals(
RoomOptions.default,
RoomOptions(
typing = TypingOptions(),
presence = PresenceOptions(),
reactions = RoomReactionsOptions(),
occupancy = OccupancyOptions(),
),
)
}
}
4 changes: 2 additions & 2 deletions chat-android/src/test/java/com/ably/chat/SandboxTest.kt
Original file line number Diff line number Diff line change
@@ -56,7 +56,7 @@ class SandboxTest {
fun `should return occupancy for the client`() = runTest {
val chatClient = sandbox.createSandboxChatClient("client1")
val roomId = UUID.randomUUID().toString()
val roomOptions = RoomOptions(occupancy = OccupancyOptions)
val roomOptions = RoomOptions(occupancy = OccupancyOptions())

val chatClientRoom = chatClient.rooms.get(roomId, roomOptions)

@@ -91,7 +91,7 @@ class SandboxTest {
fun `should observe room reactions`() = runTest {
val chatClient = sandbox.createSandboxChatClient()
val roomId = UUID.randomUUID().toString()
val roomOptions = RoomOptions(reactions = RoomReactionsOptions)
val roomOptions = RoomOptions(reactions = RoomReactionsOptions())

val room = chatClient.rooms.get(roomId, roomOptions)
room.attach()
8 changes: 8 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -9,6 +9,14 @@ Ensure you have the following installed:
- Java 17 or higher
- Android SDK with API Level 34 or higher

Add your Ably key to the `local.properties` file:

```properties
sdk.dir=/path/to/android/sdk

ABLY_KEY=xxxx:yyyyyy
```

## Steps to Run the App

1. Open in Android Studio
2 changes: 0 additions & 2 deletions example/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -37,7 +37,6 @@ android {
}
}
compileOptions {
isCoreLibraryDesugaringEnabled = true
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
@@ -68,7 +67,6 @@ dependencies {
implementation(libs.androidx.ui.graphics)
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)
coreLibraryDesugaring(libs.desugar.jdk.libs)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
3 changes: 0 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -3,11 +3,9 @@

[versions]
ably = "1.2.46"
desugar-jdk-libs = "2.1.2"
junit = "4.13.2"
agp = "8.5.2"
detekt = "1.23.6"
konfetti-compose = "2.0.4"
kotlin = "2.0.10"
androidx-test = "1.6.1"
androidx-junit = "1.2.1"
@@ -23,7 +21,6 @@ build-config = "5.5.1"
ktor = "3.0.1"

[libraries]
desugar-jdk-libs = { module = "com.android.tools:desugar_jdk_libs", version.ref = "desugar-jdk-libs" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
detekt-formatting = { module = "io.gitlab.arturbosch.detekt:detekt-formatting", version.ref = "detekt" }
ably-android = { module = "io.ably:ably-android", version.ref = "ably" }

0 comments on commit b84b113

Please sign in to comment.