complete the branch
This commit is contained in:
1 parent
2c0889f64b
commit
cb20ea41f6
29 files changed
+426
-115
No files matched your search
@@ -1,38 +1,42 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Media;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media.Imaging;
|
||||
using GalaSoft.MvvmLight;
|
||||
using GalaSoft.MvvmLight.CommandWpf;
|
||||
using GalaSoft.MvvmLight.Messaging;
|
||||
using HandyControl.Controls;
|
||||
using HandyControl.Data;
|
||||
using HandyControl.Tools;
|
||||
using HandyControlDemo.Data;
|
||||
using HandyControlDemo.Tools;
|
||||
using Microsoft.Win32;
|
||||
|
||||
namespace HandyControlDemo.ViewModel
|
||||
{
|
||||
public class ChatBoxViewModel : ViewModelBase
|
||||
{
|
||||
private static readonly string AutioCachePath = $"{AppDomain.CurrentDomain.BaseDirectory}Cache";
|
||||
|
||||
private readonly string _id = Guid.NewGuid().ToString();
|
||||
|
||||
private readonly Stopwatch _stopwatch = new Lazy<Stopwatch>(() => new Stopwatch()).Value;
|
||||
|
||||
public ChatBoxViewModel()
|
||||
{
|
||||
Messenger.Default.Register<ChatInfoModel>(this, MessageToken.SendChatString, ReceiveString);
|
||||
Messenger.Default.Register<ChatInfoModel>(this, MessageToken.SendChatImage, ReceiveImage);
|
||||
Messenger.Default.Register<ChatInfoModel>(this, MessageToken.SendChatAudio, ReceiveAudio);
|
||||
Messenger.Default.Register<ChatInfoModel>(this, MessageToken.SendChatMessage, ReceiveMessage);
|
||||
}
|
||||
|
||||
private void ReceiveString(ChatInfoModel info)
|
||||
private void ReceiveMessage(ChatInfoModel info)
|
||||
{
|
||||
if (!_id.Equals(info.SenderId)) return;
|
||||
}
|
||||
|
||||
private void ReceiveImage(ChatInfoModel info)
|
||||
{
|
||||
if (!_id.Equals(info.SenderId)) return;
|
||||
}
|
||||
|
||||
private void ReceiveAudio(ChatInfoModel info)
|
||||
{
|
||||
if (!_id.Equals(info.SenderId)) return;
|
||||
if (_id.Equals(info.SenderId)) return;
|
||||
info.Role = ChatRoleType.Receiver;
|
||||
ChatInfos.Add(info);
|
||||
}
|
||||
|
||||
private string _chatString;
|
||||
@@ -43,6 +47,8 @@ namespace HandyControlDemo.ViewModel
|
||||
set => Set(ref _chatString, value);
|
||||
}
|
||||
|
||||
public ObservableCollection<ChatInfoModel> ChatInfos { get; set; } = new ObservableCollection<ChatInfoModel>();
|
||||
|
||||
public RelayCommand<KeyEventArgs> SendStringCmd => new Lazy<RelayCommand<KeyEventArgs>>(() =>
|
||||
new RelayCommand<KeyEventArgs>(SendString)).Value;
|
||||
|
||||
@@ -51,15 +57,94 @@ namespace HandyControlDemo.ViewModel
|
||||
if (e.Key == Key.Enter)
|
||||
{
|
||||
if (string.IsNullOrEmpty(ChatString)) return;
|
||||
Messenger.Default.Send(new ChatInfoModel
|
||||
var info = new ChatInfoModel
|
||||
{
|
||||
Message = ChatString,
|
||||
SenderId = _id
|
||||
}, MessageToken.SendChatString);
|
||||
SenderId = _id,
|
||||
Type = ChatMessageType.String,
|
||||
Role = ChatRoleType.Sender
|
||||
};
|
||||
ChatInfos.Add(info);
|
||||
Messenger.Default.Send(info, MessageToken.SendChatMessage);
|
||||
ChatString = string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
public RelayCommand<RoutedEventArgs> ReadMessageCmd => new Lazy<RelayCommand<RoutedEventArgs>>(() =>
|
||||
new RelayCommand<RoutedEventArgs>(ReadMessage)).Value;
|
||||
|
||||
private void ReadMessage(RoutedEventArgs e)
|
||||
{
|
||||
if (e.OriginalSource is FrameworkElement element && element.Tag is ChatInfoModel info)
|
||||
{
|
||||
if (info.Type == ChatMessageType.Image)
|
||||
{
|
||||
new ImageBrowser(new Uri(info.Enclosure.ToString()))
|
||||
{
|
||||
Owner = VisualHelper.GetActiveWindow()
|
||||
}.Show();
|
||||
}
|
||||
else if (info.Type == ChatMessageType.Audio)
|
||||
{
|
||||
var player = new SoundPlayer(info.Enclosure.ToString());
|
||||
player.PlaySync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public RelayCommand StartRecordCmd => new Lazy<RelayCommand>(() =>
|
||||
new RelayCommand(StartRecord)).Value;
|
||||
|
||||
private void StartRecord()
|
||||
{
|
||||
ExternDllHelper.MciSendString("set wave bitpersample 8", "", 0, 0);
|
||||
ExternDllHelper.MciSendString("set wave samplespersec 20000", "", 0, 0);
|
||||
ExternDllHelper.MciSendString("set wave channels 2", "", 0, 0);
|
||||
ExternDllHelper.MciSendString("set wave format tag pcm", "", 0, 0);
|
||||
ExternDllHelper.MciSendString("open new type WAVEAudio alias movie", "", 0, 0);
|
||||
ExternDllHelper.MciSendString("record movie", "", 0, 0);
|
||||
|
||||
_stopwatch.Reset();
|
||||
_stopwatch.Start();
|
||||
}
|
||||
|
||||
public RelayCommand StopRecordCmd => new Lazy<RelayCommand>(() =>
|
||||
new RelayCommand(StopRecord)).Value;
|
||||
|
||||
private void StopRecord()
|
||||
{
|
||||
if (!Directory.Exists(AutioCachePath))
|
||||
{
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(AutioCachePath);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Growl.Error(e.Message);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
var cachePath = $"{AutioCachePath}\\{Guid.NewGuid().ToString()}";
|
||||
ExternDllHelper.MciSendString("stop movie", "", 0, 0);
|
||||
ExternDllHelper.MciSendString($"save movie {cachePath}", "", 0, 0);
|
||||
ExternDllHelper.MciSendString("close movie", "", 0, 0);
|
||||
|
||||
_stopwatch.Stop();
|
||||
|
||||
var info = new ChatInfoModel
|
||||
{
|
||||
Message = $"{_stopwatch.Elapsed.Seconds.ToString()} {Properties.Langs.Lang.Second}",
|
||||
SenderId = _id,
|
||||
Type = ChatMessageType.Audio,
|
||||
Role = ChatRoleType.Sender,
|
||||
Enclosure = cachePath
|
||||
};
|
||||
ChatInfos.Add(info);
|
||||
Messenger.Default.Send(info, MessageToken.SendChatMessage);
|
||||
}
|
||||
|
||||
public RelayCommand OpenImageCmd => new Lazy<RelayCommand>(() =>
|
||||
new RelayCommand(OpenImage)).Value;
|
||||
|
||||
@@ -71,11 +156,16 @@ namespace HandyControlDemo.ViewModel
|
||||
var fileName = dialog.FileName;
|
||||
if (File.Exists(fileName))
|
||||
{
|
||||
Messenger.Default.Send(new ChatInfoModel
|
||||
var info = new ChatInfoModel
|
||||
{
|
||||
Message = fileName,
|
||||
SenderId = _id
|
||||
}, MessageToken.SendChatImage);
|
||||
Message = BitmapFrame.Create(new Uri(fileName)),
|
||||
SenderId = _id,
|
||||
Type = ChatMessageType.Image,
|
||||
Role = ChatRoleType.Sender,
|
||||
Enclosure = fileName
|
||||
};
|
||||
ChatInfos.Add(info);
|
||||
Messenger.Default.Send(info, MessageToken.SendChatMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ namespace HandyControlDemo.ViewModel
|
||||
|
||||
public PaginationDemoViewModel PaginationDemo => ServiceLocator.Current.GetInstance<PaginationDemoViewModel>();
|
||||
|
||||
public ChatBoxViewModel ChatBox => ServiceLocator.Current.GetInstance<ChatBoxViewModel>();
|
||||
public ChatBoxViewModel ChatBox => new ChatBoxViewModel();
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user