[gnome-games/wip/exalm/db: 25/29] core: Add PlatformModel



commit 7c12de7ccd94d16292d80d76314b177543054530
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Sat Feb 8 19:16:13 2020 +0500

    core: Add PlatformModel

 src/core/platform-model.vala | 46 ++++++++++++++++++++++++++++++++++++++++++++
 src/meson.build              |  1 +
 2 files changed, 47 insertions(+)
---
diff --git a/src/core/platform-model.vala b/src/core/platform-model.vala
new file mode 100644
index 00000000..372608bb
--- /dev/null
+++ b/src/core/platform-model.vala
@@ -0,0 +1,46 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+private class Games.PlatformModel : Object, ListModel {
+       private GameModel game_model;
+       private Sequence<Platform> sequence;
+       private int n_platforms;
+       private HashTable<Platform, uint> n_games;
+
+       public PlatformModel (GameModel game_model) {
+               this.game_model = game_model;
+               sequence = new Sequence<Platform> ();
+               n_platforms = 0;
+               n_games = new HashTable<Platform, uint> (Platform.hash, Platform.equal);
+
+               game_model.game_added.connect (game_added);
+       }
+
+       public Object? get_item (uint position) {
+               var iter = sequence.get_iter_at_pos ((int) position);
+
+               return iter.get ();
+       }
+
+       public Type get_item_type () {
+               return typeof (Platform);
+       }
+
+       public uint get_n_items () {
+               return n_platforms;
+       }
+
+       private void game_added (Game game) {
+               var platform = game.get_platform ();
+
+               if (n_games[platform] == 0) {
+                       var iter = sequence.insert_sorted (platform, compare_func);
+                       items_changed (iter.get_position (), 0, 1);
+               }
+
+               n_games[platform] = n_games[platform] + 1;
+       }
+
+       private int compare_func (Platform a, Platform b) {
+               return a.get_name ().collate (b.get_name ());
+       }
+}
diff --git a/src/meson.build b/src/meson.build
index 367850ad..18cc70bf 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -29,6 +29,7 @@ vala_sources = [
   'core/media-set/media-set-error.vala',
   'core/migrator.vala',
   'core/platform.vala',
+  'core/platform-model.vala',
   'core/platform-register.vala',
   'core/plugin.vala',
   'core/plugin-register.vala',


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