Skip to content

Commit

Permalink
Various improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Serg-Norseman committed Dec 25, 2024
1 parent a18b537 commit 9b590e5
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 63 deletions.
2 changes: 1 addition & 1 deletion projects/GKCore/GDModel/GDMTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ public string NewXRef(GDMRecord gdmRec, bool removeOldXRef = false)

public T GetPtrValue<T>(GDMPointer ptr) where T : GDMRecord
{
return (ptr == null || !ptr.IsPointer) ? default(T) : XRefIndex_Find(ptr.XRef) as T;
return (ptr == null || !ptr.IsPointer) ? default : XRefIndex_Find(ptr.XRef) as T;
}

public void SetPtrValue(GDMPointer ptr, GDMRecord record)
Expand Down
9 changes: 7 additions & 2 deletions projects/GKCore/GKCore/BaseContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,7 @@ public async Task<bool> DeleteRecord(GDMRecord record)
}

if (result) {
fTree.Header.TransmissionDateTime = DateTime.Now;
Modified = true;
SetModified();

if (fViewer != null) {
fViewer.NotifyRecord(record, RecordAction.raDelete);
Expand Down Expand Up @@ -1771,6 +1770,12 @@ public void DoRollback()

#region Modify routines

public void SetModified()
{
fTree.Header.TransmissionDateTime = DateTime.Now;
Modified = true;
}

/// <summary>
/// This method performs a basic locking of the records for their
/// editors.
Expand Down
8 changes: 2 additions & 6 deletions projects/GKCore/GKCore/Controllers/BaseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1130,12 +1130,10 @@ public static void NotifyRecord(IBaseWindow baseWin, GDMRecord record, RecordAct
{
if (baseWin == null || record == null) return;

DateTime dtNow = DateTime.Now;

switch (action) {
case RecordAction.raAdd:
case RecordAction.raEdit:
record.ChangeDate.ChangeDateTime = dtNow;
record.ChangeDate.ChangeDateTime = DateTime.Now;
break;

case RecordAction.raDelete:
Expand All @@ -1150,9 +1148,7 @@ public static void NotifyRecord(IBaseWindow baseWin, GDMRecord record, RecordAct
}

if (action != RecordAction.raJump) {
baseWin.Context.Tree.Header.TransmissionDateTime = dtNow;
baseWin.Context.Modified = true;

baseWin.Context.SetModified();
AppHost.Instance.NotifyRecord(baseWin, record, action);
}
}
Expand Down
6 changes: 6 additions & 0 deletions projects/GKCore/GKCore/Controllers/TreeCheckController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,20 @@ public void CheckBase()
public async Task Repair()
{
try {
bool modified = false;
int num = fView.ChecksList.Items.Count;
for (int i = 0; i < num; i++) {
IListItem item = fView.ChecksList.Items[i];
if (item.Checked) {
var checkObj = item.Tag as TreeInspector.CheckObj;
await TreeInspector.RepairProblem(fView, fBase, checkObj);
modified = true;
}
}

if (modified) {
fBase.Context.SetModified();
}
} finally {
fBase.RefreshLists(false);
CheckBase();
Expand Down
2 changes: 1 addition & 1 deletion projects/GKCore/GKCore/Controllers/TreeMergeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public async void Merge()

fView.UpdateBase.Text = fileName;
TreeTools.MergeTreeFile(fBase.Context.Tree, fileName, fView.SyncLog, true);
fBase.Context.Modified = true;
fBase.Context.SetModified();
fBase.RefreshLists(false);
}

Expand Down
112 changes: 62 additions & 50 deletions projects/GKCore/GKCore/GKUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,12 +367,15 @@ public static string GenRecordLink(GDMTree tree, GDMRecord record, bool signed)
return result;
}

public static Tuple<string, string> GenRecordLinkTuple(GDMTree tree, GDMRecord record, bool signed)
private class LinksList : List<Tuple<string, string>> { }

public static Tuple<string, string> GenRecordLinkTuple(GDMTree tree, GDMRecord record, bool signed, string prefix = "", string suffix = "")
{
if (record != null) {
string recName = GetRecordName(tree, record, signed);
string recLink = HyperLink(record.XRef, recName);
return new Tuple<string, string>(recName, recLink);
string recLink = prefix + HyperLink(record.XRef, recName) + suffix;
// prefix always sortable
return new Tuple<string, string>(prefix + recName, recLink);
} else {
return new Tuple<string, string>(string.Empty, string.Empty);
}
Expand Down Expand Up @@ -1364,7 +1367,7 @@ public static int GetDaysFor(GDMDate dt, DateTime dtNow, out int years, out bool
bdD -= 1;
}

distance = DateHelper.DaysBetween(dtNow, new DateTime(bdY, bdM, bdD));
distance = DateHelper.DaysBetween(dtNow.Date, new DateTime(bdY, bdM, bdD));
}
}
} catch (Exception ex) {
Expand Down Expand Up @@ -2096,14 +2099,14 @@ public static void ShowDetailCause(GDMCustomEvent evt, StringList summary)
}
}

private static void ShowEvent(GDMTree tree, GDMRecord subj, StringList aToList, GDMRecord aRec, GDMCustomEvent evt)
private static void ShowEvent(GDMTree tree, GDMRecord subject, LinksList linksList, GDMRecord record, GDMCustomEvent evt)
{
switch (subj.RecordType) {
switch (subject.RecordType) {
case GDMRecordType.rtNote:
if (evt.HasNotes) {
for (int i = 0, num = evt.Notes.Count; i < num; i++) {
if (evt.Notes[i].XRef == subj.XRef) {
ShowLink(tree, subj, aToList, aRec, evt, null);
if (evt.Notes[i].XRef == subject.XRef) {
ShowLink(tree, subject, linksList, record, evt, null);
}
}
}
Expand All @@ -2112,8 +2115,8 @@ private static void ShowEvent(GDMTree tree, GDMRecord subj, StringList aToList,
case GDMRecordType.rtMultimedia:
if (evt.HasMultimediaLinks) {
for (int i = 0, num = evt.MultimediaLinks.Count; i < num; i++) {
if (evt.MultimediaLinks[i].XRef == subj.XRef) {
ShowLink(tree, subj, aToList, aRec, evt, null);
if (evt.MultimediaLinks[i].XRef == subject.XRef) {
ShowLink(tree, subject, linksList, record, evt, null);
}
}
}
Expand All @@ -2122,36 +2125,33 @@ private static void ShowEvent(GDMTree tree, GDMRecord subj, StringList aToList,
case GDMRecordType.rtSource:
if (evt.HasSourceCitations) {
for (int i = 0, num = evt.SourceCitations.Count; i < num; i++) {
if (evt.SourceCitations[i].XRef == subj.XRef) {
ShowLink(tree, subj, aToList, aRec, evt, evt.SourceCitations[i]);
var sourCit = evt.SourceCitations[i];
if (sourCit.XRef == subject.XRef) {
ShowLink(tree, subject, linksList, record, evt, sourCit);
}
}
}
break;
}
}

private static void ShowLink(GDMTree tree, GDMRecord aSubject, StringList aToList, GDMRecord aRec, GDMTag aTag, GDMPointer aExt)
private static void ShowLink(GDMTree tree, GDMRecord aSubject, LinksList linksList, GDMRecord record, GDMTag aTag, GDMPointer aExt)
{
string prefix;
if (aSubject is GDMSourceRecord && aExt != null) {
GDMSourceCitation cit = (aExt as GDMSourceCitation);
if (cit != null && !string.IsNullOrEmpty(cit.Page)) {
prefix = cit.Page + ": ";
} else {
prefix = "";
string prefix = " ";
if (aSubject is GDMSourceRecord && aExt is GDMSourceCitation cit) {
if (!string.IsNullOrEmpty(cit.Page)) {
prefix += cit.Page + ": ";
}
} else {
prefix = "";
}

string suffix;
if (aTag is GDMCustomEvent) {
suffix = ", " + GetEventNameLd((GDMCustomEvent) aTag);
if (aTag is GDMCustomEvent evt) {
suffix = ", " + GetEventNameLd(evt);
} else {
suffix = "";
}
aToList.Add(" " + prefix + GenRecordLink(tree, aRec, true) + suffix);

linksList.Add(GenRecordLinkTuple(tree, record, true, prefix, suffix));
}

public static int FindLinkStr(StringList list, string link)
Expand Down Expand Up @@ -2246,50 +2246,62 @@ private static void ShowPersonNamesakes(GDMTree tree, GDMIndividualRecord iRec,
private static void ShowSubjectLinks(GDMTree tree, GDMRecord subject, StringList summary, bool sort = false)
{
try {
StringList linkList = new StringList();
var subjectType = subject.RecordType;
var linksList = new LinksList();

for (int k = 0, num2 = tree.RecordsCount; k < num2; k++) {
GDMRecord record = tree[k];

if (subject is GDMNoteRecord && record.HasNotes) {
for (int i = 0, num = record.Notes.Count; i < num; i++) {
if (record.Notes[i].XRef == subject.XRef) {
ShowLink(tree, subject, linkList, record, null, null);
switch (subjectType) {
case GDMRecordType.rtNote:
if (record.HasNotes) {
for (int i = 0, num = record.Notes.Count; i < num; i++) {
if (record.Notes[i].XRef == subject.XRef) {
ShowLink(tree, subject, linksList, record, null, null);
}
}
}
}
} else if (subject is GDMMultimediaRecord && record.HasMultimediaLinks) {
for (int i = 0, num = record.MultimediaLinks.Count; i < num; i++) {
if (record.MultimediaLinks[i].XRef == subject.XRef) {
ShowLink(tree, subject, linkList, record, null, null);
break;

case GDMRecordType.rtMultimedia:
if (record.HasMultimediaLinks) {
for (int i = 0, num = record.MultimediaLinks.Count; i < num; i++) {
if (record.MultimediaLinks[i].XRef == subject.XRef) {
ShowLink(tree, subject, linksList, record, null, null);
}
}
}
}
} else if (subject is GDMSourceRecord && record.HasSourceCitations) {
for (int i = 0, num = record.SourceCitations.Count; i < num; i++) {
var sourCit = record.SourceCitations[i];
if (sourCit.XRef == subject.XRef) {
ShowLink(tree, subject, linkList, record, null, sourCit);
break;

case GDMRecordType.rtSource:
if (record.HasSourceCitations) {
for (int i = 0, num = record.SourceCitations.Count; i < num; i++) {
var sourCit = record.SourceCitations[i];
if (sourCit.XRef == subject.XRef) {
ShowLink(tree, subject, linksList, record, null, sourCit);
}
}
}
}
break;
}

var evsRec = record as GDMRecordWithEvents;
if (evsRec != null && evsRec.HasEvents) {
if (record is GDMRecordWithEvents evsRec && evsRec.HasEvents) {
for (int i = 0, num = evsRec.Events.Count; i < num; i++) {
ShowEvent(tree, subject, linkList, evsRec, evsRec.Events[i]);
ShowEvent(tree, subject, linksList, evsRec, evsRec.Events[i]);
}
}
}

if (linkList.Count > 0) {
if (linksList.Count > 0) {
summary.Add("");
summary.Add(LangMan.LS(LSID.Links) + ":");

if (sort) {
linkList.Sort();
linksList.Sort();
}

for (int j = 0, num3 = linkList.Count; j < num3; j++) {
summary.Add(linkList[j]);
for (int j = 0, num3 = linksList.Count; j < num3; j++) {
summary.Add(linksList[j].Item2);
}
}
} catch (Exception ex) {
Expand Down Expand Up @@ -2957,7 +2969,7 @@ public static void ShowRepositoryInfo(IBaseContext baseContext, GDMRepositoryRec
summary.Add("");
summary.Add(LangMan.LS(LSID.RPSources) + ":");

var sortedSources = new List<Tuple<string, string>>();
var sortedSources = new LinksList();
GDMTree tree = baseContext.Tree;
int num = tree.RecordsCount;
for (int i = 0; i < num; i++) {
Expand Down
5 changes: 5 additions & 0 deletions projects/GKCore/GKCore/Interfaces/IBaseContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ public interface IBaseContext : IDisposable
void DoCommit();
void DoRollback();

/// <summary>
/// Set the data modification flag and timestamp.
/// </summary>
void SetModified();

void LockRecord(GDMRecord record);
void UnlockRecord(GDMRecord record);
bool IsAvailableRecord(GDMRecord record);
Expand Down
1 change: 0 additions & 1 deletion projects/GKCore/GKCore/Lists/URefsListModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ public override async Task Modify(object sender, ModifyEventArgs eArgs)
string confirmation = !string.IsNullOrEmpty(userRef.StringValue) ? userRef.StringValue : userRef.ReferenceType;
if (await AppHost.StdDialogs.ShowQuestion(LangMan.LS(LSID.RemoveUserRefQuery, confirmation))) {
result = fUndoman.DoOrdinaryOperation(OperationType.otIndividualURefRemove, iRec, userRef);
fBaseWin.Context.Modified = true;
}
break;
}
Expand Down
2 changes: 1 addition & 1 deletion projects/GKCore/GKCore/Tools/TreeInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ public static string DetectCycle(GDMTree tree, GDMIndividualRecord iRec)
private static string CheckCycle(GDMTree tree, GDMIndividualRecord iRec)
{
var stack = new Stack<GDMIndividualRecord>();
GDMIndividualRecord hasCycle = null;
GDMIndividualRecord hasCycle;
var indiDCFlags = new GKVarCache<GDMIndividualRecord, int>();

if (!HasIndiFlag(indiDCFlags, iRec, DCFlag.dcfAncWalk)) {
Expand Down
2 changes: 1 addition & 1 deletion projects/GKTests/GKCore/BaseContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace GKCore
[TestFixture]
public class BaseContextTests
{
private BaseContext fContext;
private readonly BaseContext fContext;

public BaseContextTests()
{
Expand Down
5 changes: 5 additions & 0 deletions projects/GKTests/GKCore/GKUtilsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,11 @@ public void Test_GetDaysForBirth()
Assert.AreEqual(1, GKUtils.GetDaysFor(gdmDate, DateTime.Parse("1990-12-19T00:00:00"), out years, out anniversary)); // 1 days left, and year *will be* 10
Assert.AreEqual(10, years);
Assert.AreEqual(true, anniversary);

gdmDate.ParseString("22 DEC 1990");
Assert.AreEqual(1, GKUtils.GetDaysFor(gdmDate, DateTime.Parse("2024-12-21T21:00:00"), out years, out anniversary)); // 1 days left
Assert.AreEqual(34, years);
Assert.AreEqual(false, anniversary);
}

[Test]
Expand Down

0 comments on commit 9b590e5

Please sign in to comment.