From f1946aff79bf07ee4537a2c8dc1fa4c272aae87b Mon Sep 17 00:00:00 2001
From: Julian Sparber <julian@sparber.net>
Date: Thu, 28 Jan 2021 15:17:30 +0100
Subject: [PATCH] BestMatch: remove BestMatchView to reduce vala code

---
 src/calls-best-match-view.vala | 49 ----------------------------------
 src/calls-best-match.c         | 13 ++++++---
 src/calls-best-match.h         |  2 --
 src/calls-contacts-provider.h  |  1 +
 src/meson.build                |  1 -
 5 files changed, 10 insertions(+), 56 deletions(-)
 delete mode 100644 src/calls-best-match-view.vala

diff --git a/src/calls-best-match-view.vala b/src/calls-best-match-view.vala
deleted file mode 100644
index c396c69..0000000
--- a/src/calls-best-match-view.vala
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * Copyright (C) 2019 Purism SPC
- *
- * This file is part of Calls.
- *
- * Calls 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.
- *
- * Calls 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 Calls.  If not, see <http://www.gnu.org/licenses/>.
- *
- * Author: Bob Ham <bob.ham@puri.sm>
- *
- * SPDX-License-Identifier: GPL-3.0-or-later
- *
- */
-
-
-public class Calls.BestMatchView : Folks.SearchView
-{
-    public Folks.Individual? best_match
-    {
-        get;
-        private set;
-    }
-
-    private void update ()
-    {
-        best_match =
-            individuals.is_empty ? null : individuals.first ();
-    }
-
-    public BestMatchView (Folks.IndividualAggregator aggregator,
-                          Folks.Query                query)
-    {
-        base (aggregator, query);
-
-        this.individuals_changed_detailed.connect
-            ((sv, p) => { update (); });
-        update();
-    }
-}
diff --git a/src/calls-best-match.c b/src/calls-best-match.c
index 6587f50..53c5b70 100644
--- a/src/calls-best-match.c
+++ b/src/calls-best-match.c
@@ -24,6 +24,7 @@
 
 #include "calls-best-match.h"
 #include "calls-contacts-provider.h"
+#include "calls-vala.h"
 #include "util.h"
 
 #include <glib/gi18n.h>
@@ -33,7 +34,7 @@ struct _CallsBestMatch
 {
   GObject parent_instance;
 
-  CallsBestMatchView *view;
+  FolksSearchView    *view;
   FolksIndividual    *best_match;
   gchar              *phone_number;
 };
@@ -80,7 +81,11 @@ notify_avatar (CallsBestMatch *self)
 static void
 update_best_match (CallsBestMatch *self)
 {
-  FolksIndividual *best_match = calls_best_match_view_get_best_match (self->view);
+  g_autoptr (GeeSortedSet) individuals = folks_search_view_get_individuals (self->view);
+  FolksIndividual *best_match = NULL;
+
+  if (!gee_collection_get_is_empty (GEE_COLLECTION (individuals)))
+      best_match = gee_sorted_set_first (individuals);
 
   if (best_match == self->best_match)
     return;
@@ -266,10 +271,10 @@ calls_best_match_set_phone_number (CallsBestMatch *self,
       g_warning ("Failed to convert %s to a phone number: %s", phone_number, error->message);
     } else {
       query = calls_phone_number_query_new (number);
-      self->view = calls_best_match_view_new (folks_individual_aggregator_dup (), FOLKS_QUERY (query));
+      self->view = folks_search_view_new (folks_individual_aggregator_dup (), FOLKS_QUERY (query));
 
       g_signal_connect_swapped (self->view,
-                                "notify::best-match",
+                                "individuals-changed-detailed",
                                 G_CALLBACK (update_best_match),
                                 self);
 
diff --git a/src/calls-best-match.h b/src/calls-best-match.h
index 85ce825..b31fc3a 100644
--- a/src/calls-best-match.h
+++ b/src/calls-best-match.h
@@ -25,8 +25,6 @@
 #ifndef CALLS_BEST_MATCH_H__
 #define CALLS_BEST_MATCH_H__
 
-#include "calls-vala.h"
-
 #include <gdk/gdk.h>
 
 G_BEGIN_DECLS
diff --git a/src/calls-contacts-provider.h b/src/calls-contacts-provider.h
index 2cc8e09..e105134 100644
--- a/src/calls-contacts-provider.h
+++ b/src/calls-contacts-provider.h
@@ -37,6 +37,7 @@ G_BEGIN_DECLS
 
 G_DEFINE_AUTOPTR_CLEANUP_FUNC (GeeMap, g_object_unref)
 G_DEFINE_AUTOPTR_CLEANUP_FUNC (GeeSet, g_object_unref)
+G_DEFINE_AUTOPTR_CLEANUP_FUNC (GeeSortedSet, g_object_unref)
 G_DEFINE_AUTOPTR_CLEANUP_FUNC (GeeCollection, g_object_unref)
 G_DEFINE_AUTOPTR_CLEANUP_FUNC (EPhoneNumber, e_phone_number_free)
 
diff --git a/src/meson.build b/src/meson.build
index ac55a1a..b864dc4 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -53,7 +53,6 @@ calls_vala_deps = [
 calls_vala_sources = files (
   [
     'calls-phone-number-query.vala',
-    'calls-best-match-view.vala',
   ]
 )