This commit is contained in:
mmrbnjd
2025-08-10 21:02:51 +03:30
parent 0856c8ae59
commit 11608c4f6d

View File

@@ -175,15 +175,15 @@
<Button Color="ButtonColor.Secondary" Size=ButtonSize.Small Outline="true" @onclick="OpenFileDialog" Class="attach-btn" title="افزودن تصویر">
<Icon Name="IconName.Image" />
</Button>
<!-- Audio Recording Button -->
<Button Color="@(IsRecording ? ButtonColor.Danger : ButtonColor.Secondary)"
Size=ButtonSize.Small
Outline="true"
@onclick="ToggleAudioRecording"
class=@($"audio-btn {(IsRecording ? "recording" : "")}")
title="@(IsRecording ? "توقف ضبط" : "ضبط صدا")"
>
Size=ButtonSize.Small
Outline="true"
@onclick="ToggleAudioRecording"
class=@($"audio-btn {(IsRecording ? "recording" : "")}")
title="@(IsRecording ? "توقف ضبط" : "ضبط صدا")"
>
@if (IsRecording)
{
<Icon Name="IconName.StopCircle" Class="recording-pulse" />
@@ -193,12 +193,12 @@
<Icon Name="IconName.Mic" />
}
</Button>
<Button Color="ButtonColor.Primary" Size=ButtonSize.Small @onclick="OnClickSendMsg" Class="send-btn" title="ارسال">
<Icon Name="IconName.Send" />
</Button>
</div>
<!-- Image Preview -->
@if (SelectedImagePreview != null)
{
@@ -207,7 +207,7 @@
<Button Color="ButtonColor.Secondary" Size=ButtonSize.ExtraSmall Outline="true" @onclick="ClearSelectedImage">حذف تصویر</Button>
</div>
}
<!-- Audio Preview -->
@if (RecordedAudioBytes != null)
{
@@ -223,7 +223,7 @@
<Button Color="ButtonColor.Danger" Size=ButtonSize.ExtraSmall Outline="true" @onclick="ClearRecordedAudio">حذف صدا</Button>
</div>
}
<!-- Recording Status -->
@if (IsRecording)
{
@@ -278,7 +278,7 @@
IBrowserFile? SelectedImageFile = null;
byte[]? SelectedImageBytes = null;
string? SelectedImagePreview = null;
// Audio recording properties
bool IsRecording = false;
string RecordingTime = "00:00";
@@ -287,7 +287,7 @@
string RecordedAudioDuration = "00:00";
private System.Threading.Timer? recordingTimer;
private DateTime recordingStartTime;
bool chatloading = false;
public bool IsLogin { get; set; } = false;
public bool IsEndFirstProcess { get; set; } = false;
@@ -317,7 +317,7 @@
return value;
}
}
// Audio recording methods
private async Task ToggleAudioRecording()
{
@@ -330,7 +330,7 @@
await StartAudioRecording();
}
}
private async Task StartAudioRecording()
{
try
@@ -353,7 +353,7 @@
toastService.Notify(new ToastMessage(ToastType.Danger, $"خطا در ضبط صدا: {ex.Message}"));
}
}
private async Task StopAudioRecording()
{
try
@@ -366,10 +366,11 @@
RecordedAudioBytes = Convert.FromBase64String(base64Data);
RecordedAudioUrl = audioData;
RecordedAudioDuration = RecordingTime;
IsRecording = false;
recordingTimer?.Dispose();
recordingTimer = null;
await ClearSelectedImage();
StateHasChanged();
}
}
@@ -385,14 +386,14 @@
StateHasChanged();
}
}
private void UpdateRecordingTime(object? state)
{
var elapsed = DateTime.Now - recordingStartTime;
RecordingTime = $"{elapsed.Minutes:D2}:{elapsed.Seconds:D2}";
InvokeAsync(StateHasChanged);
}
private Task ClearRecordedAudio()
{
RecordedAudioBytes = null;
@@ -400,17 +401,17 @@
RecordedAudioDuration = "00:00";
return Task.CompletedTask;
}
private string GetAudioDataUrl(string? fileType, byte[]? content)
=> (string.IsNullOrWhiteSpace(fileType) || content == null || content.Length == 0)
? string.Empty
: $"data:{fileType};base64,{Convert.ToBase64String(content)}";
private string GetAudioDuration(byte[]? content)
{
// Simple duration calculation based on file size (approximate)
if (content == null || content.Length == 0) return "00:00";
// Assuming 16-bit PCM at 44.1kHz, mono
var bytesPerSecond = 44100 * 2; // 44.1kHz * 2 bytes per sample
var durationSeconds = content.Length / bytesPerSecond;
@@ -428,7 +429,7 @@
{
Common.Enums.ConversationType type = Common.Enums.ConversationType.UE;
ChatItemResponseDto? model;
if (SelectedImageFile != null)
{
var bytes = SelectedImageBytes ?? Array.Empty<byte>();
@@ -456,7 +457,7 @@
{
model = await chatService.ADDChatResponse(LastOpenChat.ID, MsgInput, type);
}
LastOpenChat?.Responses.Add(model);
LastOpenChat.LastText = MsgInput;
@@ -486,7 +487,7 @@
SelectedImageFile = null;
SelectedImageBytes = null;
SelectedImagePreview = null;
// Clear recorded audio after sending
RecordedAudioBytes = null;
RecordedAudioUrl = null;
@@ -701,6 +702,7 @@
await file.OpenReadStream().CopyToAsync(memoryStream);
SelectedImageBytes = memoryStream.ToArray();
SelectedImagePreview = $"data:{file.ContentType};base64,{Convert.ToBase64String(SelectedImageBytes)}";
await ClearRecordedAudio();
}
private Task ClearSelectedImage()