From 0abbc2883e0c570ef84246aa3c8b735ba2a8eea2 Mon Sep 17 00:00:00 2001 From: Niels De Graef Date: Wed, 1 Mar 2023 08:53:54 +0100 Subject: addresses-chunk: properly notify is-empty We weren't sending out a property notification for `is-empty` when the underlying `Folks.PostalAddress` changed, which meant that editing a contact would only allows for a single address. Also add a test to make sure we're not regressing on this. --- src/core/contacts-addresses-chunk.vala | 35 ++++++++++++++++++---------------- tests/core/test-addresses-chunk.vala | 5 +++++ 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/core/contacts-addresses-chunk.vala b/src/core/contacts-addresses-chunk.vala index bcdf508..b4a2c85 100644 --- a/src/core/contacts-addresses-chunk.vala +++ b/src/core/contacts-addresses-chunk.vala @@ -52,39 +52,42 @@ public class Contacts.AddressesChunk : BinChunk { public class Contacts.Address : BinChunkChild { - public PostalAddress address { - get { return this._address; } - set { - if (this._address.equal (value)) - return; - - bool was_empty = this._address.is_empty (); - this._address = value; - notify_property ("address"); - if (was_empty != value.is_empty ()) - notify_property ("is-empty"); - } - } - private PostalAddress _address = new PostalAddress ("", "", "", "", "", "", "", "", ""); + public PostalAddress address { get; construct; } public override bool is_empty { - get { return this.address.is_empty (); } + get { return this._is_empty; } } + private bool _is_empty = true; public override string icon_name { get { return "mark-location-symbolic"; } } + construct { + update_on_address (); + this.address.notify.connect ((obj, pspec) => { update_on_address (); }); + } + public Address () { + Object (address: new PostalAddress ("", "", "", "", "", "", "", "", "")); + this.parameters = new Gee.HashMultiMap (); this.parameters["type"] = "HOME"; } public Address.from_field_details (PostalAddressFieldDetails address_field) { - this.address = address_field.value; + Object (address: address_field.value); + this.parameters = address_field.parameters; } + private void update_on_address () { + if (this.is_empty != this.address.is_empty ()) { + this._is_empty = this.address.is_empty (); + notify_property ("is-empty"); + } + } + protected override int compare_internal (BinChunkChild other) requires (other is Address) { var this_types = this.parameters["type"]; diff --git a/tests/core/test-addresses-chunk.vala b/tests/core/test-addresses-chunk.vala index 83ebb61..685c6bd 100644 --- a/tests/core/test-addresses-chunk.vala +++ b/tests/core/test-addresses-chunk.vala @@ -44,9 +44,14 @@ private void test_is_empty () { assert_true (address.is_empty); assert_true (chunk.is_empty); + // Make sure that the notify works correctly for the address too + bool notified = false; + address.notify["is-empty"].connect ((o, p) => { notified = true; }); + address.address.street = "Yellow brick road"; assert_false (address.is_empty); assert_false (chunk.is_empty); + assert_true (notified); address.address.street = ""; assert_true (address.is_empty); -- cgit v1.2.1