1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-11-29 12:45:20 +01:00

PM-3349 PM-3350 Changed SendViewCell and its binding to be directly against the ViewModel

This commit is contained in:
Federico Maccaroni 2023-11-13 14:44:19 -03:00
parent 363da063fa
commit e0a3c301fb
No known key found for this signature in database
GPG Key ID: 5D233F8F2B034536
10 changed files with 70 additions and 110 deletions

View File

@ -1,62 +1,28 @@
using System; using Bit.App.Abstractions;
using Bit.App.Abstractions;
using Bit.Core.Models.View; using Bit.Core.Models.View;
using Bit.Core.Utilities; using Bit.Core.Utilities;
using Microsoft.Maui.Controls;
using Microsoft.Maui;
namespace Bit.App.Controls namespace Bit.App.Controls
{ {
public partial class SendViewCell : ExtendedGrid public partial class SendViewCell : ExtendedGrid
{ {
public static readonly BindableProperty SendProperty = BindableProperty.Create(
nameof(Send), typeof(SendView), typeof(SendViewCell), default(SendView), BindingMode.OneWay);
public static readonly BindableProperty ButtonCommandProperty = BindableProperty.Create( public static readonly BindableProperty ButtonCommandProperty = BindableProperty.Create(
nameof(ButtonCommand), typeof(Command<SendView>), typeof(SendViewCell)); nameof(ButtonCommand), typeof(Command<SendView>), typeof(SendViewCell));
public static readonly BindableProperty ShowOptionsProperty = BindableProperty.Create(
nameof(ShowOptions), typeof(bool), typeof(SendViewCell), true, BindingMode.OneWay);
public SendViewCell() public SendViewCell()
{ {
InitializeComponent(); InitializeComponent();
var deviceActionService = ServiceContainer.Resolve<IDeviceActionService>("deviceActionService"); var deviceActionService = ServiceContainer.Resolve<IDeviceActionService>();
_iconColumn.Width = new GridLength(40 * deviceActionService.GetSystemFontSizeScale(), GridUnitType.Absolute); _iconColumn.Width = new GridLength(40 * deviceActionService.GetSystemFontSizeScale(), GridUnitType.Absolute);
} }
public SendView Send
{
get => GetValue(SendProperty) as SendView;
set => SetValue(SendProperty, value);
}
public Command<SendView> ButtonCommand public Command<SendView> ButtonCommand
{ {
get => GetValue(ButtonCommandProperty) as Command<SendView>; get => GetValue(ButtonCommandProperty) as Command<SendView>;
set => SetValue(ButtonCommandProperty, value); set => SetValue(ButtonCommandProperty, value);
} }
public bool ShowOptions
{
get => (bool)GetValue(ShowOptionsProperty);
set => SetValue(ShowOptionsProperty, value);
}
protected override void OnPropertyChanged(string propertyName = null)
{
base.OnPropertyChanged(propertyName);
if (propertyName == SendProperty.PropertyName)
{
if (Send == null)
{
return;
}
BindingContext = new SendViewCellViewModel(Send, ShowOptions);
}
}
private void MoreButton_Clicked(object sender, EventArgs e) private void MoreButton_Clicked(object sender, EventArgs e)
{ {
var send = ((sender as MiButton)?.BindingContext as SendViewCellViewModel)?.Send; var send = ((sender as MiButton)?.BindingContext as SendViewCellViewModel)?.Send;

View File

@ -1,4 +1,5 @@
using Bit.Core.Models.View; using System.Globalization;
using Bit.Core.Models.View;
using Bit.Core.Utilities; using Bit.Core.Utilities;
namespace Bit.App.Controls namespace Bit.App.Controls

View File

@ -37,12 +37,13 @@
SemanticProperties.Description="{u:I18n AddItem}" /> SemanticProperties.Description="{u:I18n AddItem}" />
<DataTemplate x:Key="sendTemplate" <DataTemplate x:Key="sendTemplate"
x:DataType="pages:SendGroupingsPageListItem"> x:DataType="pages:SendGroupingsPageListItem">
<controls:SendViewCell <Grid>
Send="{Binding Send}" <controls:SendViewCell
ButtonCommand="{Binding BindingContext.SendOptionsCommand, Source={x:Reference _page}}" BindingContext="{Binding SendItemViewModel}"
ShowOptions="{Binding BindingContext.SendEnabled, Source={x:Reference _page}}" ButtonCommand="{Binding BindingContext.SendOptionsCommand, Source={x:Reference _page}}"
AutomationId="SendCell" /> AutomationId="SendCell" />
</Grid>
</DataTemplate> </DataTemplate>
<DataTemplate x:Key="sendGroupTemplate" <DataTemplate x:Key="sendGroupTemplate"

View File

@ -156,9 +156,9 @@ namespace Bit.App.Pages
return; return;
} }
if (item.Send != null) if (item.SendItemViewModel != null)
{ {
await _vm.SelectSendAsync(item.Send); await _vm.SelectSendAsync(item.SendItemViewModel.Send);
} }
else if (item.Type != null) else if (item.Type != null)
{ {

View File

@ -1,7 +1,8 @@
using Bit.Core.Resources.Localization; using Bit.App.Controls;
using Bit.Core; using Bit.Core;
using Bit.Core.Enums; using Bit.Core.Enums;
using Bit.Core.Models.View; using Bit.Core.Models.View;
using Bit.Core.Resources.Localization;
namespace Bit.App.Pages namespace Bit.App.Pages
{ {
@ -10,10 +11,22 @@ namespace Bit.App.Pages
private string _icon; private string _icon;
private string _name; private string _name;
public SendView Send { get; set; } public SendGroupingsPageListItem(SendType type, int itemCount)
{
Type = type;
ItemCount = itemCount.ToString("N0");
}
public SendGroupingsPageListItem(SendView send, bool showOptions)
{
SendItemViewModel = new SendViewCellViewModel(send, showOptions);
}
//public SendView Send { get; set; }
public SendType? Type { get; set; } public SendType? Type { get; set; }
public SendViewCellViewModel SendItemViewModel { get; }
public string ItemCount { get; set; } public string ItemCount { get; set; }
public bool ShowOptions { get; set; } //public bool ShowOptions { get; set; }
public string Name public string Name
{ {
@ -23,19 +36,20 @@ namespace Bit.App.Pages
{ {
return _name; return _name;
} }
if (Type != null)
if (!Type.HasValue)
{ {
switch (Type.Value) return null;
{ }
case SendType.Text:
_name = AppResources.TypeText; switch (Type.Value)
break; {
case SendType.File: case SendType.Text:
_name = AppResources.TypeFile; _name = AppResources.TypeText;
break; break;
default: case SendType.File:
break; _name = AppResources.TypeFile;
} break;
} }
return _name; return _name;
} }
@ -49,19 +63,20 @@ namespace Bit.App.Pages
{ {
return _icon; return _icon;
} }
if (Type != null)
if (!Type.HasValue)
{ {
switch (Type.Value) return null;
{ }
case SendType.Text:
_icon = BitwardenIcons.FileText; switch (Type.Value)
break; {
case SendType.File: case SendType.Text:
_icon = BitwardenIcons.File; _icon = BitwardenIcons.FileText;
break; break;
default: case SendType.File:
break; _icon = BitwardenIcons.File;
} break;
} }
return _icon; return _icon;
} }

View File

@ -18,7 +18,7 @@ namespace Bit.App.Pages
if (item is SendGroupingsPageListItem listItem) if (item is SendGroupingsPageListItem listItem)
{ {
return listItem.Send != null ? SendTemplate : GroupTemplate; return listItem.SendItemViewModel != null ? SendTemplate : GroupTemplate;
} }
return null; return null;
} }

View File

@ -153,36 +153,20 @@ namespace Bit.App.Pages
groupedSends.Add(new SendGroupingsPageListGroup( groupedSends.Add(new SendGroupingsPageListGroup(
AppResources.Types, 0, uppercaseGroupNames, true) AppResources.Types, 0, uppercaseGroupNames, true)
{ {
new SendGroupingsPageListItem new SendGroupingsPageListItem(SendType.Text, _typeCounts.TryGetValue(SendType.Text, out var textCountValue) ? textCountValue : 0),
{ new SendGroupingsPageListItem(SendType.File, _typeCounts.TryGetValue(SendType.File, out var fileCountValue) ? fileCountValue : 0)
Type = SendType.Text,
ItemCount = (_typeCounts.ContainsKey(SendType.Text) ?
_typeCounts[SendType.Text] : 0).ToString("N0")
},
new SendGroupingsPageListItem
{
Type = SendType.File,
ItemCount = (_typeCounts.ContainsKey(SendType.File) ?
_typeCounts[SendType.File] : 0).ToString("N0")
},
}); });
} }
if (Sends?.Any() ?? false) if (Sends?.Any() == true)
{ {
var sendsListItems = Sends.Select(s => new SendGroupingsPageListItem var sendsListItems = Sends.Select(s => new SendGroupingsPageListItem(s, SendEnabled)).ToList();
{
Send = s,
ShowOptions = SendEnabled
}).ToList();
groupedSends.Add(new SendGroupingsPageListGroup(sendsListItems, groupedSends.Add(new SendGroupingsPageListGroup(sendsListItems,
MainPage ? AppResources.AllSends : AppResources.Sends, sendsListItems.Count, MainPage ? AppResources.AllSends : AppResources.Sends, sendsListItems.Count,
uppercaseGroupNames, !MainPage)); uppercaseGroupNames, !MainPage));
} }
// TODO: refactor this if (DeviceInfo.Platform == DevicePlatform.Android
// TODO Xamarin.Forms.Device.RuntimePlatform is no longer supported. Use Microsoft.Maui.Devices.DeviceInfo.Platform instead. For more details see https://learn.microsoft.com/en-us/dotnet/maui/migration/forms-projects#device-changes
if (Device.RuntimePlatform == Device.Android
|| ||
GroupedSends.Any()) GroupedSends.Any())
{ {

View File

@ -71,11 +71,9 @@
ExtraDataForLogging="Sends Page" ExtraDataForLogging="Sends Page"
AutomationId="SendCellList"> AutomationId="SendCellList">
<CollectionView.ItemTemplate> <CollectionView.ItemTemplate>
<DataTemplate x:DataType="views:SendView"> <DataTemplate x:DataType="controls:SendViewCellViewModel">
<controls:SendViewCell <controls:SendViewCell
Send="{Binding .}"
ButtonCommand="{Binding BindingContext.SendOptionsCommand, Source={x:Reference _page}}" ButtonCommand="{Binding BindingContext.SendOptionsCommand, Source={x:Reference _page}}"
ShowOptions="{Binding BindingContext.SendEnabled, Source={x:Reference _page}}"
AutomationId="SendCell" /> AutomationId="SendCell" />
</DataTemplate> </DataTemplate>
</CollectionView.ItemTemplate> </CollectionView.ItemTemplate>

View File

@ -108,9 +108,9 @@ namespace Bit.App.Pages
return; return;
} }
if (e.CurrentSelection?.FirstOrDefault() is SendView send) if (e.CurrentSelection?.FirstOrDefault() is SendViewCellViewModel sendVM)
{ {
await _vm.SelectSendAsync(send); await _vm.SelectSendAsync(sendVM.Send);
} }
} }

View File

@ -1,13 +1,8 @@
using System; using Bit.App.Controls;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Bit.App.Utilities; using Bit.App.Utilities;
using Bit.Core.Abstractions; using Bit.Core.Abstractions;
using Bit.Core.Models.View; using Bit.Core.Models.View;
using Bit.Core.Utilities; using Bit.Core.Utilities;
using Microsoft.Maui.Controls;
using Microsoft.Maui;
namespace Bit.App.Pages namespace Bit.App.Pages
{ {
@ -22,13 +17,13 @@ namespace Bit.App.Pages
public SendsPageViewModel() public SendsPageViewModel()
{ {
_searchService = ServiceContainer.Resolve<ISearchService>("searchService"); _searchService = ServiceContainer.Resolve<ISearchService>();
Sends = new ExtendedObservableCollection<SendView>(); Sends = new ExtendedObservableCollection<SendViewCellViewModel>();
SendOptionsCommand = new Command<SendView>(SendOptionsAsync); SendOptionsCommand = new Command<SendView>(SendOptionsAsync);
} }
public Command SendOptionsCommand { get; set; } public Command SendOptionsCommand { get; set; }
public ExtendedObservableCollection<SendView> Sends { get; set; } public ExtendedObservableCollection<SendViewCellViewModel> Sends { get; set; }
public Func<SendView, bool> Filter { get; set; } public Func<SendView, bool> Filter { get; set; }
public bool SendEnabled public bool SendEnabled
@ -102,9 +97,9 @@ namespace Bit.App.Pages
{ {
sends = new List<SendView>(); sends = new List<SendView>();
} }
Device.BeginInvokeOnMainThread(() => MainThread.BeginInvokeOnMainThread(() =>
{ {
Sends.ResetWithRange(sends); Sends.ResetWithRange(sends.Select(s => new SendViewCellViewModel(s, SendEnabled)).ToList());
ShowNoData = searchable && Sends.Count == 0; ShowNoData = searchable && Sends.Count == 0;
ShowList = searchable && !ShowNoData; ShowList = searchable && !ShowNoData;
}); });