Skip to content

Commit ef19471

Browse files
authored
Implement operation to clear local data created by the SDK (#6090)
* Refactor cache management. * Refactor external storage management. * Introduce ChatClient#clearCacheAndTemporaryFiles method. * Update CHANGELOG.md. * Update clean-up logic. * Update clean-up logic. * Fix remarks * Fix remarks * Fix remarks * Solidify external storage filename matching.
1 parent e95043a commit ef19471

22 files changed

Lines changed: 1810 additions & 615 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
### ⬆️ Improved
1818

1919
### ✅ Added
20+
- Add `ChatClient.clearCacheAndTemporaryFiles(context: Context)` method for clearing cache and temporary files created by the SDK. [#6090](https://github.com/GetStream/stream-chat-android/pull/6090)
2021

2122
### ⚠️ Changed
2223

stream-chat-android-client/api/stream-chat-android-client.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public final class io/getstream/chat/android/client/ChatClient {
2525
public final fun castPollVote (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Lio/getstream/result/call/Call;
2626
public final fun channel (Ljava/lang/String;)Lio/getstream/chat/android/client/channel/ChannelClient;
2727
public final fun channel (Ljava/lang/String;Ljava/lang/String;)Lio/getstream/chat/android/client/channel/ChannelClient;
28+
public final fun clearCacheAndTemporaryFiles (Landroid/content/Context;)Lio/getstream/result/call/Call;
2829
public final fun clearPersistence ()Lio/getstream/result/call/Call;
2930
public final fun closePoll (Ljava/lang/String;)Lio/getstream/result/call/Call;
3031
public final fun connectAnonymousUser ()Lio/getstream/result/call/Call;

stream-chat-android-client/src/main/java/io/getstream/chat/android/client/ChatClient.kt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ import io.getstream.chat.android.client.helpers.AppSettingManager
107107
import io.getstream.chat.android.client.helpers.CallPostponeHelper
108108
import io.getstream.chat.android.client.interceptor.SendMessageInterceptor
109109
import io.getstream.chat.android.client.interceptor.message.internal.PrepareMessageLogicImpl
110+
import io.getstream.chat.android.client.internal.file.StreamFileManager
110111
import io.getstream.chat.android.client.logger.ChatLogLevel
111112
import io.getstream.chat.android.client.logger.ChatLoggerConfigImpl
112113
import io.getstream.chat.android.client.logger.ChatLoggerHandler
@@ -231,6 +232,7 @@ import io.getstream.result.call.retry.RetryPolicy
231232
import io.getstream.result.call.share
232233
import io.getstream.result.call.toUnitCall
233234
import io.getstream.result.call.withPrecondition
235+
import io.getstream.result.flatMap
234236
import io.getstream.result.flatMapSuspend
235237
import io.getstream.result.onErrorSuspend
236238
import kotlinx.coroutines.CoroutineScope
@@ -290,6 +292,7 @@ internal constructor(
290292
internal val messageReceiptManager: MessageReceiptManager,
291293
) {
292294
private val logger by taggedLogger(TAG)
295+
private val fileManager = StreamFileManager()
293296
private val waitConnection = MutableSharedFlow<Result<ConnectionData>>()
294297
public val clientState: ClientState = mutableClientState
295298

@@ -1479,6 +1482,40 @@ internal constructor(
14791482
Result.Success(Unit)
14801483
}.doOnStart(clientScope) { setUserWithoutConnectingIfNeeded() }
14811484

1485+
/**
1486+
* Clears all cache and temporary files created by the Stream Chat SDK.
1487+
*
1488+
* This method removes:
1489+
* - All cached files from the default cache directory
1490+
* - All cached images from the image cache directory
1491+
* - All cached files during the upload/download process
1492+
* - All temporary files stored in external storage by the SDK (Photos and videos captured using the SDK)
1493+
*
1494+
* **Note**: This method does NOT clear database persistence. Use [clearPersistence] to clear
1495+
* database data, or call both methods if you need to clear all SDK data.
1496+
*
1497+
* **Note**: This method does NOT clear downloads made by the SDK to the file system. Those files are
1498+
* stored outside of the SDK's control and cannot be removed automatically.
1499+
*
1500+
* @param context The Android [Context] for accessing cache and external storage directories
1501+
* @return Executable async [Call] which performs the cleanup
1502+
*/
1503+
@CheckResult
1504+
public fun clearCacheAndTemporaryFiles(context: Context): Call<Unit> =
1505+
CoroutineCall(clientScope) {
1506+
logger.d { "[clearCacheAndTemporaryFiles] Clearing all cache and temporary files" }
1507+
// Clear all cache directories
1508+
val cacheResult = fileManager.clearAllCache(context)
1509+
// Clear external (temporary) storage files - always run regardless of cache result
1510+
val externalStorageResult = fileManager.clearExternalStorage(context)
1511+
// Return the first failure if any, otherwise success
1512+
when {
1513+
cacheResult is Result.Failure -> cacheResult
1514+
externalStorageResult is Result.Failure -> externalStorageResult
1515+
else -> Result.Success(Unit)
1516+
}
1517+
}
1518+
14821519
/**
14831520
* Disconnect the current user, stop all observers and clear user data.
14841521
* This method should only be used whenever the user logouts from the main app.

0 commit comments

Comments
 (0)