Skip to content

Commit

Permalink
add - doc - Added anniversary type
Browse files Browse the repository at this point in the history
---

We've added the wedding anniversary type!

---

Type: add
Breaking: False
Doc Required: True
Part: 1/1
  • Loading branch information
AptiviCEO committed Mar 28, 2024
1 parent ca26d26 commit 4631681
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 24 deletions.
10 changes: 9 additions & 1 deletion VisualCard.ShowContacts/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Terminaux.Colors.Data;
using Terminaux.Writer.ConsoleWriters;
using VisualCard.Converters;
Expand Down Expand Up @@ -165,7 +168,12 @@ static void Main(string[] args)
}

// List remaining
TextWriterColor.Write("Contact birthdate: {0}", Contact.GetPart(PartsEnum.Birthdate));
var birth = Contact.GetPart(PartsEnum.Birthdate);
var wed = Contact.GetPart(PartsEnum.Anniversary);
if (birth is BirthDateInfo bday)
TextWriterColor.Write("Contact birthdate: {0}", bday.BirthDate);
if (wed is AnniversaryInfo adate)
TextWriterColor.Write("Contact wedding date: {0}", adate.Anniversary);
TextWriterColor.Write("Contact mailer: {0}", Contact.GetString(StringsEnum.Mailer));
TextWriterColor.Write("Contact URL: {0}", Contact.GetString(StringsEnum.Url));
TextWriterColor.Write("Contact Note: {0}", Contact.GetString(StringsEnum.Notes));
Expand Down
1 change: 1 addition & 0 deletions VisualCard.ShowContacts/TestFiles/fourVCard4.vcf
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ TEL;TYPE=cell:589-210-1059
TITLE:Chief Executive Officer
URL:https://sso.org/
X-SIP-SIP:sip test
ANNIVERSARY:20230406
END:VCARD

BEGIN:VCARD
Expand Down
1 change: 1 addition & 0 deletions VisualCard.ShowContacts/TestFiles/fourVCard5.vcf
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ TITLE:Chief Executive Officer
URL:https://sso.org/
X-SIP-SIP:sip test
SORT-STRING:sarahsantos
ANNIVERSARY:20230406
END:VCARD

BEGIN:VCARD
Expand Down
1 change: 1 addition & 0 deletions VisualCard.ShowContacts/TestFiles/fourVCard5Agents.vcf
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ TITLE:Chief Executive Officer
URL:https://sso.org/
X-SIP-SIP:sip test
SORT-STRING:sarahsantos
ANNIVERSARY:20230406
END:VCARD

BEGIN:VCARD
Expand Down
2 changes: 0 additions & 2 deletions VisualCard.ShowContacts/VisualCard.ShowContacts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<ApplicationIcon>OfficialAppIcon-VisualCard.ico</ApplicationIcon>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\aptivi_snk.snk</AssemblyOriginatorKeyFile>
Expand Down
13 changes: 12 additions & 1 deletion VisualCard/Parsers/BaseVcardParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,15 @@ bool StartsWithPrefix(string prefix) =>
caladrUriStringValue = uri.ToString();
card.SetString(StringsEnum.CalendarSchedulingRequestUrl, caladrUriStringValue);
}

// Wedding anniversary (ANNIVERSARY:19960415)
// Here, we don't support ALTID.
if (StartsWithPrefix(VcardConstants._anniversarySpecifier) &&
EnumTypeSupported(PartsEnum.Anniversary, CardVersion))
{
var partInfo = AnniversaryInfo.FromStringVcardStatic(_value, altId, CardVersion, CardContentReader);
card.SetPart(PartsEnum.Anniversary, partInfo);
}
}
catch (Exception ex)
{
Expand Down Expand Up @@ -776,11 +785,12 @@ internal static bool EnumArrayTypeSupported(PartsArrayEnum partsArrayEnum, Versi
throw new InvalidOperationException("Invalid parts array enumeration type to get supported value"),
};

internal static bool EnumTypeSupported(PartsEnum partsEnum) =>
internal static bool EnumTypeSupported(PartsEnum partsEnum, Version cardVersion) =>
partsEnum switch
{
PartsEnum.Revision => true,
PartsEnum.Birthdate => true,
PartsEnum.Anniversary => cardVersion.Major >= 4,
_ =>
throw new InvalidOperationException("Invalid parts enumeration type to get supported value"),
};
Expand Down Expand Up @@ -815,6 +825,7 @@ internal static Type GetEnumType(PartsEnum partsEnum) =>
{
PartsEnum.Revision => typeof(RevisionInfo),
PartsEnum.Birthdate => typeof(BirthDateInfo),
PartsEnum.Anniversary => typeof(AnniversaryInfo),
_ =>
throw new InvalidOperationException("Invalid parts enumeration type"),
};
Expand Down
1 change: 1 addition & 0 deletions VisualCard/Parsers/VcardConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ internal static class VcardConstants

// Available in vCard 4.0 and 5.0
internal const string _kindSpecifier = "KIND";
internal const string _anniversarySpecifier = "ANNIVERSARY";
internal const string _altIdArgumentSpecifier = "ALTID=";
}
}
2 changes: 1 addition & 1 deletion VisualCard/Parts/Card.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public BaseCardPartInfo[] GetPartsArray(PartsArrayEnum key)
public BaseCardPartInfo GetPart(PartsEnum key)
{
// Check for version support
if (!BaseVcardParser.EnumTypeSupported(key))
if (!BaseVcardParser.EnumTypeSupported(key, CardVersion))
return null;

// Get the fallback value
Expand Down
4 changes: 4 additions & 0 deletions VisualCard/Parts/Enums/PartsEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,9 @@ public enum PartsEnum
/// The contact's birthdate
/// </summary>
Birthdate,
/// <summary>
/// The contact's wedding anniversary date (that is, the day that this contact is married)
/// </summary>
Anniversary,
}
}
142 changes: 142 additions & 0 deletions VisualCard/Parts/Implementations/AnniversaryInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
//
// VisualCard Copyright (C) 2021-2024 Aptivi
//
// This file is part of VisualCard
//
// VisualCard is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// VisualCard is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using VisualCard.Parsers;

namespace VisualCard.Parts.Implementations
{
/// <summary>
/// Contact wedding anniversary info
/// </summary>
[DebuggerDisplay("Wedding anniversary = {Anniversary}")]
public class AnniversaryInfo : BaseCardPartInfo, IEquatable<AnniversaryInfo>
{
/// <summary>
/// The contact's wedding anniversary date (that is, the day that this contact is married)
/// </summary>
public DateTime? Anniversary { get; }

internal static BaseCardPartInfo FromStringVcardStatic(string value, int altId, Version cardVersion, StreamReader cardContentReader) =>
new AnniversaryInfo().FromStringVcardInternal(value, altId, cardVersion, cardContentReader);

internal static BaseCardPartInfo FromStringVcardWithTypeStatic(string value, string[] finalArgs, int altId, Version cardVersion, StreamReader cardContentReader) =>
new AnniversaryInfo().FromStringVcardWithTypeInternal(value, finalArgs, altId, cardVersion, cardContentReader);

internal override string ToStringVcardInternal(Version cardVersion) =>
$"{VcardConstants._anniversarySpecifier}:{Anniversary:yyyyMMdd}";

internal override BaseCardPartInfo FromStringVcardInternal(string value, int altId, Version cardVersion, StreamReader cardContentReader)
{
// Get the value
string anniversaryValue = value.Substring(VcardConstants._anniversarySpecifier.Length + 1);

// Populate the fields
return InstallInfo(anniversaryValue);
}

internal override BaseCardPartInfo FromStringVcardWithTypeInternal(string value, string[] finalArgs, int altId, Version cardVersion, StreamReader cardContentReader) =>
FromStringVcardInternal(value, altId, cardVersion, cardContentReader);

private AnniversaryInfo InstallInfo(string value)
{
// Populate field
DateTime anniversary;
if (int.TryParse(value, out _) && value.Length == 8)
{
int anniversaryNum = int.Parse(value);
var anniversaryDigits = VcardParserTools.GetDigits(anniversaryNum).ToList();
int anniversaryYear = anniversaryDigits[0] * 1000 + anniversaryDigits[1] * 100 + anniversaryDigits[2] * 10 + anniversaryDigits[3];
int anniversaryMonth = anniversaryDigits[4] * 10 + anniversaryDigits[5];
int anniversaryDay = anniversaryDigits[6] * 10 + anniversaryDigits[7];
anniversary = new DateTime(anniversaryYear, anniversaryMonth, anniversaryDay);
}
else
anniversary = DateTime.Parse(value);

// Add the fetched information
AnniversaryInfo _time = new(0, [], anniversary);
return _time;
}

/// <inheritdoc/>
public override bool Equals(object obj) =>
base.Equals(obj);

/// <summary>
/// Checks to see if both the parts are equal
/// </summary>
/// <param name="other">The target <see cref="AnniversaryInfo"/> instance to check to see if they equal</param>
/// <returns>True if all the part elements are equal. Otherwise, false.</returns>
public bool Equals(AnniversaryInfo other) =>
Equals(this, other);

/// <summary>
/// Checks to see if both the parts are equal
/// </summary>
/// <param name="source">The source <see cref="AnniversaryInfo"/> instance to check to see if they equal</param>
/// <param name="target">The target <see cref="AnniversaryInfo"/> instance to check to see if they equal</param>
/// <returns>True if all the part elements are equal. Otherwise, false.</returns>
public bool Equals(AnniversaryInfo source, AnniversaryInfo target)
{
// We can't perform this operation on null.
if (source is null)
return false;

// Check all the properties
return
source.AltArguments.SequenceEqual(target.AltArguments) &&
source.AltId == target.AltId &&
source.Anniversary == target.Anniversary
;
}

/// <inheritdoc/>
public override int GetHashCode()
{
int hashCode = -2035919920;
hashCode = hashCode * -1521134295 + base.GetHashCode();
hashCode = hashCode * -1521134295 + EqualityComparer<string[]>.Default.GetHashCode(AltArguments);
hashCode = hashCode * -1521134295 + AltId.GetHashCode();
hashCode = hashCode * -1521134295 + Anniversary.GetHashCode();
return hashCode;
}

/// <inheritdoc/>
public static bool operator ==(AnniversaryInfo left, AnniversaryInfo right) =>
EqualityComparer<AnniversaryInfo>.Default.Equals(left, right);

/// <inheritdoc/>
public static bool operator !=(AnniversaryInfo left, AnniversaryInfo right) =>
!(left == right);

internal AnniversaryInfo() { }

internal AnniversaryInfo(int altId, string[] altArguments, DateTime? anniversary)
{
AltId = altId;
AltArguments = altArguments;
Anniversary = anniversary;
}
}
}
21 changes: 2 additions & 19 deletions VisualCard/Parts/Implementations/BirthDateInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,8 @@ internal static BaseCardPartInfo FromStringVcardStatic(string value, int altId,
internal static BaseCardPartInfo FromStringVcardWithTypeStatic(string value, string[] finalArgs, int altId, Version cardVersion, StreamReader cardContentReader) =>
new BirthDateInfo().FromStringVcardWithTypeInternal(value, finalArgs, altId, cardVersion, cardContentReader);

internal override string ToStringVcardInternal(Version cardVersion)
{
bool altIdSupported = cardVersion.Major >= 4;
if (altIdSupported)
{
bool installAltId = AltId >= 0 && AltArguments.Length > 0;
return
$"{VcardConstants._birthSpecifier}{(installAltId ? VcardConstants._fieldDelimiter : VcardConstants._argumentDelimiter)}" +
$"{(installAltId ? VcardConstants._altIdArgumentSpecifier + AltId + VcardConstants._fieldDelimiter : "")}" +
$"{(installAltId ? string.Join(VcardConstants._fieldDelimiter.ToString(), AltArguments) + VcardConstants._argumentDelimiter : "")}" +
$"{BirthDate}";
}
else
{
return
$"{VcardConstants._birthSpecifier}:" +
$"{BirthDate}";
}
}
internal override string ToStringVcardInternal(Version cardVersion) =>
$"{VcardConstants._birthSpecifier}:{BirthDate:yyyyMMdd}";

internal override BaseCardPartInfo FromStringVcardInternal(string value, int altId, Version cardVersion, StreamReader cardContentReader)
{
Expand Down

0 comments on commit 4631681

Please sign in to comment.