[libgames-support] scores: Replace Context.high_score_added with DialogMode



commit 846dd6d22d24abecbe769db4719b69aa0b752906
Author: Michael Catanzaro <mcatanzaro gnome org>
Date:   Sun Feb 22 12:22:38 2015 -0600

    scores: Replace Context.high_score_added with DialogMode

 scores/context.vala |   21 +++++++++++++++------
 scores/dialog.vala  |   43 +++++++++++++++++++++++++------------------
 2 files changed, 40 insertions(+), 24 deletions(-)
---
diff --git a/scores/context.vala b/scores/context.vala
index b254098..b461245 100644
--- a/scores/context.vala
+++ b/scores/context.vala
@@ -41,8 +41,6 @@ public class Context : Object
     /* A priority queue enables us to easily fetch the top 10 scores */
     private Gee.HashMap<Category?, Gee.PriorityQueue<Score> > scores_per_category = new 
Gee.HashMap<Category?, Gee.PriorityQueue<Score> > ((owned) category_hash, (owned) category_equal);
 
-    /* This variable is used to identify if the dialog has opened due to adding of a score */
-    internal bool high_score_added { get; set; default = false; }
     private bool score_added = false;
     private string user_score_dir;
     private bool scores_loaded_from_file = false;
@@ -169,6 +167,7 @@ public class Context : Object
             }
         }
 
+        var high_score_added = false;
        /* We need to check for game_window to be not null because thats a way to identify if add_score is 
being called by the test file.
           If it's being called by test file, then the dialog wouldn't be run and hence player name wouldn't 
be updated. So, in that case,
           we wish to save scores to file right now itself rather than waiting for a call to update_score. */
@@ -197,7 +196,7 @@ public class Context : Object
         }
         else
         {
-            run_dialog ();
+            run_dialog_internal (DialogMode.HIGH_SCORE_ADDED);
             return true;
         }
     }
@@ -297,7 +296,7 @@ public class Context : Object
         return score_value > lowest;
     }
 
-    public void run_dialog ()
+    internal void run_dialog_internal (DialogMode mode)
     {
         if (!scores_loaded_from_file)
         {
@@ -314,16 +313,26 @@ public class Context : Object
 
         if (game_window != null)
         {
-            var dialog = new Dialog (this, dialog_label, style, last_score, current_category, game_window);
+            var dialog = new Dialog (this, dialog_label, style, last_score, current_category, game_window, 
mode);
             dialog.run ();
             dialog.destroy ();
         }
         score_added = false;
     }
 
+    public void run_dialog ()
+    {
+        run_dialog_internal (DialogMode.CATEGORY_BROWSER);
+    }
+
     public bool has_scores ()
     {
-        return scores_per_category.is_empty;
+        foreach (var scores in scores_per_category.values)
+        {
+            if (scores.size > 0)
+                return true;
+        }
+        return false;
     }
 }
 
diff --git a/scores/dialog.vala b/scores/dialog.vala
index d890201..efdee48 100644
--- a/scores/dialog.vala
+++ b/scores/dialog.vala
@@ -21,6 +21,12 @@
 namespace Games {
 namespace Scores {
 
+internal enum DialogMode
+{
+    CATEGORY_BROWSER,
+    HIGH_SCORE_ADDED
+}
+
 private class Dialog : Gtk.Dialog
 {
     private Context context;
@@ -36,7 +42,9 @@ private class Dialog : Gtk.Dialog
     private Score? latest_score;
     private Category? scores_active_category;
 
-    public Dialog (Context context, string dialog_label, Style style, Score? latest_score, Category? 
current_cat, Gtk.Window window)
+    private DialogMode mode;
+
+    public Dialog (Context context, string dialog_label, Style style, Score? latest_score, Category? 
current_cat, Gtk.Window window, DialogMode mode)
     {
         Object (use_header_bar : 1);
 
@@ -45,17 +53,16 @@ private class Dialog : Gtk.Dialog
         this.context = context;
         this.transient_for = window;
         this.latest_score = latest_score;
+        this.mode = mode;
+
         scores_style = style;
         scores_active_category = current_cat;
 
         headerbar = (Gtk.HeaderBar) this.get_header_bar ();
 
-        if (context.high_score_added)
-            headerbar.show_close_button = false;
-        else
-            headerbar.show_close_button = true;
+        headerbar.show_close_button = (mode == DialogMode.CATEGORY_BROWSER);
 
-        if (context.high_score_added)
+        if (mode == DialogMode.HIGH_SCORE_ADDED)
         /* Appears at the top of the dialog, as the heading of the dialog */
             headerbar.title = _("Congratulations!");
         else if (scores_style == Style.PLAIN_ASCENDING || scores_style == Style.PLAIN_DESCENDING)
@@ -80,7 +87,7 @@ private class Dialog : Gtk.Dialog
         label.halign = Gtk.Align.CENTER;
         catbar.pack_start (label, false, false, 0);
 
-        if (context.high_score_added)
+        if (mode == DialogMode.HIGH_SCORE_ADDED)
         {
             category_label = new Gtk.Label (scores_active_category.name);
             category_label.use_markup = true;
@@ -131,7 +138,7 @@ private class Dialog : Gtk.Dialog
         grid.baseline_row = 0;
         fill_grid_with_labels ();
 
-        if (context.high_score_added)
+        if (mode == DialogMode.HIGH_SCORE_ADDED)
             /* Appears on the top right corner of the dialog. Clicking the button closes the dialog. */
             add_button (_("Done"), Gtk.ResponseType.OK).get_style_context ().add_class ("suggested-action");
 
@@ -180,13 +187,11 @@ private class Dialog : Gtk.Dialog
     private void load_categories ()
     {
         /* If we are adding a high score, we don't wish to load all categories. We only wish to load scores 
of active category. */
-        if (context.high_score_added)
+        if (mode == DialogMode.HIGH_SCORE_ADDED)
+        {
             load_scores ();
-
-        if (combo == null)
-            return;
-
-        if (context.has_scores ())
+        }
+        else if (!context.has_scores ())
         {
             warning ("A GamesScoresDialog was created but no scores exist yet. " +
                      "Games should not allow this dialog to be created before scores have been added. " +
@@ -194,6 +199,10 @@ private class Dialog : Gtk.Dialog
             return;
         }
 
+
+        if (combo == null)
+            return;
+
         var categories = context.get_categories ();
         categories.foreach ((x) => combo.append (x.key, x.name));
 
@@ -216,7 +225,7 @@ private class Dialog : Gtk.Dialog
     /* loads the scores of current active_category */
     private void load_scores ()
     {
-        if (context.high_score_added)
+        if (mode == DialogMode.HIGH_SCORE_ADDED)
             active_category = new Category (scores_active_category.key, scores_active_category.name);
         else
             active_category = new Category (combo.get_active_id (), combo.get_active_text ());
@@ -245,7 +254,7 @@ private class Dialog : Gtk.Dialog
         var score = (Gtk.Label) grid.get_child_at (1, row_count);
         score.set_text (x.score.to_string ());
 
-        if (context.high_score_added
+        if (mode == DialogMode.HIGH_SCORE_ADDED
             && latest_score != null
             && Score.equals (x, latest_score))
         {
@@ -263,8 +272,6 @@ private class Dialog : Gtk.Dialog
                 context.update_score_name (x, visible.get_text (), active_category);
                 x.user = visible.get_text ();
             });
-
-            context.high_score_added = false;
         }
 
         var name_stack = (Gtk.Stack) grid.get_child_at (2, row_count);


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