From eeb28f6ddfdf9b1f6d9c22e83c57a9891cb2353d Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 15 Apr 2019 08:30:59 -0400 Subject: [PATCH] card view --- src/Core/Models/View/CardView.cs | 83 +++++++++++++++++++++++++++++--- 1 file changed, 76 insertions(+), 7 deletions(-) diff --git a/src/Core/Models/View/CardView.cs b/src/Core/Models/View/CardView.cs index 02ccb5aaa..f62e5b9f2 100644 --- a/src/Core/Models/View/CardView.cs +++ b/src/Core/Models/View/CardView.cs @@ -1,20 +1,89 @@ using Bit.Core.Models.Domain; +using System.Text.RegularExpressions; namespace Bit.Core.Models.View { public class CardView : View { + private string _brand; + private string _number; + private string _subTitle; + public CardView() { } public CardView(Card c) { } - public string Id { get; set; } - public string Url { get; set; } - public string Size { get; set; } - public string SizeName { get; set; } - public string FileName { get; set; } - public SymmetricCryptoKey Key { get; set; } + public string CardholderName { get; set; } + public string ExpMonth { get; set; } + public string ExpYear { get; set; } + public string Code { get; set; } + public string MaskedCode => Code != null ? new string('•', Code.Length) : null; - // TODO + public string Brand + { + get => _brand; + set + { + _brand = value; + _subTitle = null; + } + } + + public string Number + { + get => _number; + set + { + _number = value; + _subTitle = null; + } + } + + public string SubTitle + { + get + { + if(_subTitle == null) + { + _subTitle = Brand; + if(Number != null && Number.Length >= 4) + { + if(string.IsNullOrWhiteSpace(_subTitle)) + { + _subTitle += ", "; + } + else + { + _subTitle = string.Empty; + } + // Show last 5 on amex, last 4 for all others + var count = Number.Length >= 5 && Regex.Match(Number, "^3[47]").Success ? 5 : 4; + _subTitle += ("*" + Number.Substring(Number.Length - count)); + } + } + return _subTitle; + } + } + + public string Expiration + { + get + { + var expMonthNull = string.IsNullOrWhiteSpace(ExpMonth); + var expYearNull = string.IsNullOrWhiteSpace(ExpYear); + if(expMonthNull && expYearNull) + { + return null; + } + var expMo = !expMonthNull ? ExpMonth.PadLeft(2, '0') : "__"; + var expYr = !expYearNull ? FormatYear(ExpYear) : "____"; + return string.Format("{0} / {1}", expMo, expYr); + } + } + + private string FormatYear(string year) + { + return year.Length == 2 ? string.Concat("20", year) : year; + } } }