Skip to content

Commit a77fbd1

Browse files
committed
Fix chat files saving issue
1 parent 66fab61 commit a77fbd1

File tree

83 files changed

+106
-90
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+106
-90
lines changed

app/src/main/java/com/ismartcoding/plain/chat/PeerFileDownloader.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.ismartcoding.plain.db.ChatItemDataUpdate
88
import com.ismartcoding.plain.db.DMessageFiles
99
import com.ismartcoding.plain.db.DMessageImages
1010
import com.ismartcoding.plain.helpers.ChatFileSaveHelper
11+
import com.ismartcoding.plain.preferences.ChatFilesSaveFolderPreference
1112
import kotlinx.coroutines.flow.MutableStateFlow
1213
import okhttp3.OkHttpClient
1314
import okhttp3.Request
@@ -51,7 +52,8 @@ object PeerFileDownloader {
5152
task.status = DownloadStatus.DOWNLOADING
5253
task.downloadedSize = 0
5354

54-
val chatFilePath = ChatFileSaveHelper.generateChatFilePathAsync(context, fileName)
55+
val customDir = ChatFilesSaveFolderPreference.getAsync(context)
56+
val chatFilePath = ChatFileSaveHelper.generateChatFilePathAsync(context, fileName, customDir)
5557
val localFile = File(chatFilePath.path).apply {
5658
parentFile?.mkdirs()
5759
createNewFile()

app/src/main/java/com/ismartcoding/plain/events/HttpApiEvents.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import com.ismartcoding.plain.db.DChat
66

77
// The events sent from the HTTP API
88
class HttpApiEvents {
9-
class MessageCreatedEvent(val items: List<DChat>) : ChannelEvent()
9+
class MessageCreatedEvent(val fromId: String, val items: List<DChat>) : ChannelEvent()
1010

1111
class MessageUpdatedEvent(val id: String) : ChannelEvent()
1212

app/src/main/java/com/ismartcoding/plain/helpers/ChatFileSaveHelper.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ object ChatFileSaveHelper {
2121
suspend fun generateChatFilePathAsync(
2222
context: Context,
2323
fileName: String,
24+
customDir: String,
2425
pickFileType: PickFileType? = null
2526
): ChatFilePath {
2627
var actualFileName = fileName
@@ -29,7 +30,6 @@ object ChatFileSaveHelper {
2930
actualFileName = "$actualFileName.jpg"
3031
}
3132

32-
val customDir = ChatFilesSaveFolderPreference.getAsync(context)
3333
val targetDir = if (customDir.isEmpty()) {
3434
context.getExternalFilesDir(null)!!
3535
} else {

app/src/main/java/com/ismartcoding/plain/ui/page/chat/ChatPage.kt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,13 @@ import com.ismartcoding.plain.events.DeleteChatItemViewEvent
5252
import com.ismartcoding.plain.events.HttpApiEvents
5353
import com.ismartcoding.plain.events.PickFileResultEvent
5454
import com.ismartcoding.plain.extensions.getDuration
55+
import com.ismartcoding.plain.features.file.FileSystemHelper
5556
import com.ismartcoding.plain.features.locale.LocaleHelper
5657
import com.ismartcoding.plain.helpers.ChatFileSaveHelper
5758
import com.ismartcoding.plain.helpers.FileHelper
5859
import com.ismartcoding.plain.helpers.ImageHelper
5960
import com.ismartcoding.plain.helpers.VideoHelper
61+
import com.ismartcoding.plain.preferences.ChatFilesSaveFolderPreference
6062
import com.ismartcoding.plain.preferences.ChatInputTextPreference
6163
import com.ismartcoding.plain.ui.base.AnimatedBottomAction
6264
import com.ismartcoding.plain.ui.base.HorizontalSpace
@@ -139,9 +141,11 @@ fun ChatPage(
139141
}
140142

141143
is HttpApiEvents.MessageCreatedEvent -> {
142-
chatVM.addAll(event.items)
143-
scope.launch {
144-
scrollState.scrollToItem(0)
144+
if (chatVM.chatState.value.toId == event.fromId) {
145+
chatVM.addAll(event.items)
146+
scope.launch {
147+
scrollState.scrollToItem(0)
148+
}
145149
}
146150
}
147151

@@ -302,13 +306,12 @@ private fun handleFileSelection(
302306
}
303307
}
304308
val size = file.size
305-
306-
// Generate chat file save path
307-
val chatFilePath = ChatFileSaveHelper.generateChatFilePathAsync(context, fileName, event.type)
308-
309+
310+
val chatFilePath = ChatFileSaveHelper.generateChatFilePathAsync(context, fileName, "", event.type)
311+
309312
// Copy file to destination
310313
FileHelper.copyFile(context, uri, chatFilePath.path)
311-
314+
312315
val dstFile = File(chatFilePath.path)
313316
val intrinsicSize = if (chatFilePath.path.isImageFast()) ImageHelper.getIntrinsicSize(
314317
chatFilePath.path,

app/src/main/java/com/ismartcoding/plain/ui/page/chat/components/ChatFiles.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import androidx.core.net.toUri
3838
import androidx.navigation.NavHostController
3939
import coil3.compose.AsyncImage
4040
import com.ismartcoding.lib.extensions.dp2px
41+
import com.ismartcoding.lib.extensions.formatBytes
4142
import com.ismartcoding.lib.extensions.formatDuration
4243
import com.ismartcoding.lib.extensions.getFilenameExtension
4344
import com.ismartcoding.lib.extensions.getFilenameFromPath
@@ -71,6 +72,7 @@ import com.ismartcoding.plain.ui.nav.navigatePdf
7172
import com.ismartcoding.plain.ui.nav.navigateTextFile
7273
import com.ismartcoding.plain.ui.page.audio.AudioPlayerPage
7374
import com.ismartcoding.plain.ui.theme.cardBackgroundNormal
75+
import com.ismartcoding.plain.ui.theme.listItemSubtitle
7476
import com.ismartcoding.plain.ui.theme.listItemTitle
7577
import kotlinx.coroutines.Job
7678
import kotlinx.coroutines.delay
@@ -199,6 +201,15 @@ fun ChatFiles(
199201
color = if (isCurrentlyPlaying) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.onSurface
200202
)
201203

204+
Text(
205+
modifier =
206+
Modifier
207+
.fillMaxWidth()
208+
.padding(end = 8.dp),
209+
text = item.size.formatBytes() + if (item.duration > 0) " / ${item.duration.formatDuration()}" else "",
210+
style = MaterialTheme.typography.listItemSubtitle(),
211+
)
212+
202213
// Display summary for text files if available
203214
if (fileName.isTextFile() && item.summary.isNotEmpty()) {
204215
Text(

app/src/main/java/com/ismartcoding/plain/web/MainGraphQL.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ class MainGraphQL(val schema: Schema) {
730730
if (item.content.type == DMessageType.TEXT.value) {
731731
sendEvent(FetchLinkPreviewsEvent(item))
732732
}
733-
sendEvent(HttpApiEvents.MessageCreatedEvent(arrayListOf(item)))
733+
sendEvent(HttpApiEvents.MessageCreatedEvent("local", arrayListOf(item)))
734734
arrayListOf(item).map { it.toModel() }
735735
}
736736
}

app/src/main/java/com/ismartcoding/plain/web/PeerGraphQL.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class PeerGraphQL(val schema: Schema) {
9090
}
9191
}
9292

93-
sendEvent(HttpApiEvents.MessageCreatedEvent(arrayListOf(item)))
93+
sendEvent(HttpApiEvents.MessageCreatedEvent(fromId, arrayListOf(item)))
9494
arrayListOf(item).map { it.toModel() }
9595
}
9696
}

app/src/main/resources/web/assets/AppsSidebar-CNw_DWCl.js renamed to app/src/main/resources/web/assets/AppsSidebar-DchLoU3i.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)