[gnome-games/wip/exalm/tnum: 1/24] collection: Add hash, equal and compare funcs




commit 4de86b83ef0bd0fd566cf16a7c14bf0a34b41972
Author: Neville <nevilleantony98 gmail com>
Date:   Thu Aug 6 20:33:35 2020 +0530

    collection: Add hash, equal and compare funcs
    
    Introduces CollectionType enum which will be used later on to filter
    and compare between collections.
    
    Adds matches_search_terms() which will be used to filter collections
    while searching.

 src/collection/collection-model.vala           |  2 +-
 src/collection/collection.vala                 | 42 ++++++++++++++++++++++++++
 src/collection/favorites-collection.vala       |  4 +++
 src/collection/recently-played-collection.vala |  4 +++
 4 files changed, 51 insertions(+), 1 deletion(-)
---
diff --git a/src/collection/collection-model.vala b/src/collection/collection-model.vala
index e82fc6788..66ce14ce3 100644
--- a/src/collection/collection-model.vala
+++ b/src/collection/collection-model.vala
@@ -26,7 +26,7 @@ private class Games.CollectionModel : Object, ListModel {
        }
 
        public void add_collection (Collection collection) {
-               var iter = sequence.append (collection);
+               var iter = sequence.insert_sorted (collection, Collection.compare);
                n_collections++;
 
                items_changed (iter.get_position (), 0, 1);
diff --git a/src/collection/collection.vala b/src/collection/collection.vala
index 9cbbaf01b..42df3cf76 100644
--- a/src/collection/collection.vala
+++ b/src/collection/collection.vala
@@ -1,6 +1,13 @@
 // This file is part of GNOME Games. License: GPL-3.0+.
 
 private interface Games.Collection : Object {
+       // Collection types are in decreasing order of precedence while comparing between them.
+       public enum CollectionType {
+               AUTO,
+               USER,
+               PLACEHOLDER
+       }
+
        public signal void games_changed ();
 
        public abstract bool is_empty { get; }
@@ -15,6 +22,8 @@ private interface Games.Collection : Object {
 
        public abstract GameModel get_game_model ();
 
+       public abstract CollectionType get_collection_type ();
+
        public abstract void add_games (Game[] games);
 
        public abstract void remove_games (Game[] games);
@@ -24,4 +33,37 @@ private interface Games.Collection : Object {
        public abstract void on_game_removed (Game game);
 
        public abstract void on_game_replaced (Game game, Game prev_game);
+
+       public bool matches_search_terms (string[] search_terms) {
+               var name = get_title ();
+               if (search_terms.length != 0)
+                       foreach (var term in search_terms)
+                               if (!(term.casefold () in name.casefold ()))
+                                       return false;
+
+               return true;
+       }
+
+       public static uint hash (Collection collection) {
+               return str_hash (collection.get_id ());
+       }
+
+       public static bool equal (Collection a, Collection b) {
+               if (direct_equal (a, b))
+                       return true;
+
+               return str_equal (a.get_id (), b.get_id ());
+       }
+
+       public static int compare (Collection a, Collection b) {
+               var title_a = a.get_title ();
+               var title_b = b.get_title ();
+               var type_a = a.get_collection_type ();
+               var type_b = b.get_collection_type ();
+
+               if (type_a == type_b)
+                       return title_a.collate (title_b);
+
+               return type_a < type_b ? -1 : 1;
+       }
 }
diff --git a/src/collection/favorites-collection.vala b/src/collection/favorites-collection.vala
index 5cf5daf52..2a5739189 100644
--- a/src/collection/favorites-collection.vala
+++ b/src/collection/favorites-collection.vala
@@ -55,6 +55,10 @@ private class Games.FavoritesCollection : Object, Collection {
                return true;
        }
 
+       public CollectionType get_collection_type () {
+               return AUTO;
+       }
+
        public void load () {
                try {
                        favorite_game_uids = database.list_favorite_games ();
diff --git a/src/collection/recently-played-collection.vala b/src/collection/recently-played-collection.vala
index c9240e490..0c710b7a9 100644
--- a/src/collection/recently-played-collection.vala
+++ b/src/collection/recently-played-collection.vala
@@ -54,6 +54,10 @@ private class Games.RecentlyPlayedCollection : Object, Collection {
                return false;
        }
 
+       public CollectionType get_collection_type () {
+               return AUTO;
+       }
+
        public void load () {
                try {
                        game_uids = database.list_recently_played_games ();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]