[gnome-games/wip/exalm/db: 24/29] core: Add GameModel



commit 1716170190c4848590b0ca086b7b0c051469340b
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Sat Feb 8 19:16:01 2020 +0500

    core: Add GameModel

 src/core/game-model.vala | 39 +++++++++++++++++++++++++++++++++++++++
 src/meson.build          |  1 +
 2 files changed, 40 insertions(+)
---
diff --git a/src/core/game-model.vala b/src/core/game-model.vala
new file mode 100644
index 00000000..0f15910a
--- /dev/null
+++ b/src/core/game-model.vala
@@ -0,0 +1,39 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+private class Games.GameModel : Object, ListModel {
+       public signal void game_added (Game game);
+
+       private Sequence<Game> sequence;
+       private int n_games;
+
+       construct {
+               sequence = new Sequence<Game> ();
+               n_games = 0;
+       }
+
+       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 (Game);
+       }
+
+       public uint get_n_items () {
+               return n_games;
+       }
+
+       public void add_game (Game game) {
+               var iter = sequence.insert_sorted (game, compare_func);
+               n_games++;
+
+               items_changed (iter.get_position (), 0, 1);
+               game_added (game);
+       }
+
+       private int compare_func (Game a, Game b) {
+               return a.name.collate (b.name);
+       }
+}
diff --git a/src/meson.build b/src/meson.build
index f26016e6..367850ad 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -19,6 +19,7 @@ vala_sources = [
   'core/game.vala',
   'core/game-callback.vala',
   'core/game-collection.vala',
+  'core/game-model.vala',
   'core/game-uri-adapter.vala',
   'core/icon.vala',
   'core/input-capabilities.vala',


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