Files
App/OwnChar.App.Desktop/UI/MainTabs/Controls/CharacterListControl.cs
Schedel Pascal a6c45feb57 some fixes
2024-07-09 10:23:04 +02:00

205 lines
6.0 KiB
C#

using OwnChar.App.Desktop.Api;
using OwnChar.App.Desktop.LangRes;
using OwnChar.Model;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
namespace OwnChar.App.Desktop.UI.MainTabs.Controls;
public partial class CharacterListControl : UserControl
{
private IMainWindowApi mainApi = null!;
private UserProfile? profile;
private Group? group;
public CharacterListControl()
{
InitializeComponent();
}
public void LoadContent(IMainWindowApi mainApi, UserProfile? profile)
{
LoadContent(mainApi, profile, null);
}
public void LoadContent(IMainWindowApi mainApi, Group? group)
{
LoadContent(mainApi, null, group);
}
private void LoadContent(IMainWindowApi mainApi, UserProfile? profile, Group? group)
{
this.mainApi = mainApi;
this.profile = profile;
this.group = group;
PrepareList();
LoadList(group);
}
private void PrepareList()
{
radListView_CharList.BeginUpdate();
radListView_CharList.Columns.Clear();
radListView_CharList.Columns.Add(GeneralLangRes.Column_Name);
radListView_CharList.Columns.Add(GeneralLangRes.Column_Fandom);
if (profile != null)
radListView_CharList.Columns.Add(CharListControlLangRes.Column_CharOwner);
else if (group != null)
radListView_CharList.Columns.Add(CharListControlLangRes.Column_CharGroup);
radListView_CharList.EndUpdate();
}
private void LoadList(Group? group)
{
radListView_CharList.BeginUpdate();
radListView_CharList.Items.Clear();
if (profile != null && mainApi.Manager?.Characters.GetCharacters(profile) is IEnumerable<Character> characters)
{
foreach (Character character in characters)
{
AddCharToList(character, false);
}
}
radListView_CharList.EndUpdate();
}
private void AddCharToList(Character character, bool focus)
{
radListView_CharList.BeginUpdate();
var newItem = new ListViewDataItem();
UpdateCharListViewItem(newItem, character);
newItem.Value = character;
radListView_CharList.EndUpdate();
if (focus)
{
radListView_CharList.SelectedItem = newItem;
radListView_CharList.ListViewElement.ViewElement.Scroller.ScrollToItem(newItem);
}
}
private void RemoveCharFromList(Character character)
{
radListView_CharList.BeginUpdate();
foreach (var item in radListView_CharList.Items.ToArray())
{
if (item.Value == character)
radListView_CharList.Items.Remove(item);
}
radListView_CharList.EndUpdate();
}
private void UpdateCharListViewItem(ListViewDataItem listItem, Character character)
{
listItem[0] = character.Name;
listItem[1] = character.Fandom;
if (group != null)
listItem[2] = group.Name;
else
listItem[2] = mainApi.Manager?.Groups.GetOwner(group)?.Name;
}
private Character? GetSelectedChar()
{
return radListView_CharList.SelectedItem?.Value as Character;
}
private void NewChar()
{
if (mainApi.Manager?.Characters.CreateCharacter(CharListControlLangRes.UnnamedChar) is Character newChar)
AddCharToList(newChar, true);
}
private void DeleteChar()
{
var selChar = GetSelectedChar();
if (selChar != null)
{
if (RadMessageBox.Show(string.Format(GeneralLangRes.MsgBox_ConfirmDelete_TextGeneral, string.Format(CharListControlLangRes.MsgBox_ConfirmDelete_TextChar, selChar.Name)), string.Format(GeneralLangRes.MsgBox_ConfirmDelete_TitleGeneral, CharListControlLangRes.MsgBox_ConfirmDelete_TitleChar), MessageBoxButtons.YesNo, RadMessageIcon.Exclamation) == DialogResult.Yes)
{
mainApi.Manager?.Characters.DeleteCharacter(selChar);
RemoveCharFromList(selChar);
}
}
else
RadMessageBox.Show(GeneralLangRes.MsgBox_NothingSelected, GeneralLangRes.MsgBox_Error, MessageBoxButtons.OK, RadMessageIcon.Error);
}
private void TryOpenChar(Character? character)
{
if (character != null)
mainApi.OpenTab(new TabCharView(mainApi, character), string.IsNullOrWhiteSpace(character.Name) ? CharListControlLangRes.UnnamedChar : character.Name);
}
private void SearchInList()
{
var filterText = radTextBoxControl_SearchBox.Text.Trim();
radListView_CharList.BeginUpdate();
foreach (ListViewDataItem item in radListView_CharList.Items)
{
if (item.Value is Character character)
{
var isValidResult = false;
if (!string.IsNullOrWhiteSpace(filterText))
{
var allValues = new string?[]
{
character.Name,
character.Fandom,
mainApi.Manager?.Characters.GetOwner(character)?.Name,
};
foreach (var value in allValues)
{
if (!isValidResult && value is not null && value.Contains(filterText))
isValidResult = true;
}
}
item.Visible = isValidResult;
}
}
radListView_CharList.EndUpdate();
}
// E V E N T S
private void RadMenuItem_AddChar_Click(object sender, System.EventArgs e)
{
NewChar();
}
private void RadMenuItem_DeleteChar_Click(object sender, System.EventArgs e)
{
DeleteChar();
}
private void RadMenuItem_OpenChar_Click(object sender, System.EventArgs e)
{
TryOpenChar(GetSelectedChar());
}
private void RadTextBoxControl_SearchBox_TextChanged(object sender, System.EventArgs e)
{
SearchInList();
}
}