Skip to content

Commit

Permalink
Tags (#47) are working (no user tags yet)
Browse files Browse the repository at this point in the history
Games context menu
  • Loading branch information
tkashkin committed Aug 29, 2018
1 parent c4e0f3f commit 6765826
Show file tree
Hide file tree
Showing 12 changed files with 384 additions and 49 deletions.
47 changes: 46 additions & 1 deletion data/GameHub.css
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ list.gameinfo-content-list
background: transparent;
}
GtkListBox.gameinfo-content-list .list-row,
list.gameinfo-content-list row
list.gameinfo-content-list row
{
background: transparent;
border-bottom: 1px alpha(#888, 0.2) solid;
Expand Down Expand Up @@ -288,4 +288,49 @@ list.gameinfo-content-list row:hover
dialog .gameinfo-toolbar, dialog .dark .gameinfo-toolbar
{
background: transparent;
}

GtkListBox.tags-list,
list.tags-list
{
background: transparent;
}
GtkListBox.tags-list .list-row,
list.tags-list row
{
background: transparent;
border-bottom: 1px alpha(#888, 0.2) solid;
outline: none;
padding: 0;
margin: 0;
}
GtkListBox.tags-list .list-row:last-child,
list.tags-list row:last-child
{
border-bottom: none;
}
GtkListBox.tags-list .list-row:selected,
list.tags-list row:selected
{
outline: none;
}
GtkListBox.tags-list .list-row:focus,
list.tags-list row:focus
{
background: transparent;
outline: none;
}
GtkListBox.tags-list .list-row:hover,
list.tags-list row:hover
{
background: alpha(#888, 0.1);
}

.tags-list-header.hover
{
background: alpha(#888, 0.1);
}
.tags-list-header label
{
padding: 2px 0;
}
24 changes: 24 additions & 0 deletions src/data/Game.vala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,30 @@ namespace GameHub.Data
}

public ArrayList<GamesDB.Tables.Tags.Tag> tags { get; protected set; default = new ArrayList<GamesDB.Tables.Tags.Tag>(GamesDB.Tables.Tags.Tag.is_equal); }
public bool has_tag(GamesDB.Tables.Tags.Tag tag)
{
return has_tag_id(tag.id);
}
public bool has_tag_id(string tag)
{
foreach(var t in tags)
{
if(t.id == tag) return true;
}
return false;
}
public void toggle_tag(GamesDB.Tables.Tags.Tag tag)
{
if(tags.contains(tag))
{
tags.remove(tag);
}
else
{
tags.add(tag);
}
GamesDB.get_instance().add_game(this);
}

public bool is_installable { get; protected set; default = false; }

Expand Down
66 changes: 47 additions & 19 deletions src/data/GamesDB.vala
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ namespace GameHub.Data
if(i == null) return bind_null(s);
return s.bind_int64(column_for_bind, i);
}
public int bind_bool(Statement s, bool? b)
{
if(b == null) return bind_null(s);
return s.bind_int(column_for_bind, b ? 1 : 0);
}
public int bind_value(Statement s, Sqlite.Value? v)
{
if(v == null) return bind_null(s);
Expand All @@ -97,11 +102,15 @@ namespace GameHub.Data
{
return s.column_text(column);
}
public int? get_int(Statement s)
public int get_int(Statement s)
{
return s.column_int(column);
}
public int64? get_int64(Statement s)
public bool get_bool(Statement s)
{
return get_int(s) == 0 ? false : true;
}
public int64 get_int64(Statement s)
{
return s.column_int64(column);
}
Expand Down Expand Up @@ -174,9 +183,13 @@ namespace GameHub.Data
public static DBTable.Field ID;
public static DBTable.Field NAME;
public static DBTable.Field ICON;
public static DBTable.Field SELECTED;

public static ArrayList<Tag> TAGS;

public static Tag BUILTIN_FAVORITES;
public static Tag BUILTIN_HIDDEN;

public class Tag: Object
{
public const string BUILTIN_PREFIX = "builtin:";
Expand Down Expand Up @@ -218,19 +231,20 @@ namespace GameHub.Data

public string? id { get; construct set; }
public string? name { get; construct set; }
public string icon { get; construct; }
public string icon { get; construct set; }
public bool selected { get; construct set; default = true; }

public Tag(string? id, string? name, string icon="tag-symbolic")
public Tag(string? id, string? name, string icon="tag-symbolic", bool selected=true)
{
Object(id: id, name: name, icon: icon);
Object(id: id, name: name, icon: icon, selected: selected);
}
public Tag.from_db(Statement s)
{
this(ID.get(s), NAME.get(s), ICON.get(s));
this(ID.get(s), NAME.get(s), ICON.get(s), SELECTED.get_bool(s));
}
public Tag.from_builtin(Builtin t)
{
this(BUILTIN_PREFIX + t.id(), t.name(), t.icon());
this(BUILTIN_PREFIX + t.id(), t.name(), t.icon(), true);
}

public static bool is_equal(Tag first, Tag second)
Expand All @@ -241,27 +255,40 @@ namespace GameHub.Data

public static void init(Database? db) requires (db != null)
{
db.exec("CREATE TABLE IF NOT EXISTS `tags`(`id` string, `name` string, `icon` string, PRIMARY KEY(`id`))");
Statement s;
if(db.prepare_v2("SELECT `selected` FROM `tags`", -1, out s) != Sqlite.OK)
{
db.exec("ALTER TABLE `tags` ADD `selected` int");
}

db.exec("CREATE TABLE IF NOT EXISTS `tags`(`id` string, `name` string, `icon` string, `selected` int, PRIMARY KEY(`id`))");

ID = f(0);
NAME = f(1);
ICON = f(2);
SELECTED = f(3);

TAGS = new ArrayList<Tag>(Tag.is_equal);
TAGS.add(new Tag.from_builtin(Tag.Builtin.FAVORITES));
TAGS.add(new Tag.from_builtin(Tag.Builtin.HIDDEN));

foreach(var t in TAGS)
{
GamesDB.get_instance().add_tag(t);
}

Statement s;
int res = db.prepare_v2("SELECT * FROM `tags`", -1, out s);
int res = db.prepare_v2("SELECT * FROM `tags` ORDER BY SUBSTR(`id`, 1, 1) ASC, `name` ASC", -1, out s);
while((res = s.step()) == Sqlite.ROW)
{
var tag = new Tag.from_db(s);
if(!TAGS.contains(tag)) TAGS.add(tag);

if(BUILTIN_FAVORITES == null && tag.id == Tag.BUILTIN_PREFIX + Tag.Builtin.FAVORITES.id()) BUILTIN_FAVORITES = tag;
if(BUILTIN_HIDDEN == null && tag.id == Tag.BUILTIN_PREFIX + Tag.Builtin.HIDDEN.id()) BUILTIN_HIDDEN = tag;
}

Tag.Builtin[] builtin = { Tag.Builtin.FAVORITES, Tag.Builtin.HIDDEN };

foreach(var bt in builtin)
{
var tag = new Tag.from_builtin(bt);
GamesDB.get_instance().add_tag(tag);

if(BUILTIN_FAVORITES == null && tag.id == Tag.BUILTIN_PREFIX + Tag.Builtin.FAVORITES.id()) BUILTIN_FAVORITES = tag;
if(BUILTIN_HIDDEN == null && tag.id == Tag.BUILTIN_PREFIX + Tag.Builtin.HIDDEN.id()) BUILTIN_HIDDEN = tag;
}
}
}
Expand Down Expand Up @@ -308,16 +335,17 @@ namespace GameHub.Data
return res == Sqlite.DONE;
}

public bool add_tag(Tables.Tags.Tag tag) requires (db != null)
public bool add_tag(Tables.Tags.Tag tag, bool replace=false) requires (db != null)
{
Statement s;
int res = db.prepare_v2("INSERT OR REPLACE INTO `tags` (`id`, `name`, `icon`) VALUES (?, ?, ?)", -1, out s);
int res = db.prepare_v2("INSERT " + (replace ? "OR REPLACE " : "") + "INTO `tags` (`id`, `name`, `icon`, `selected`) VALUES (?, ?, ?, ?)", -1, out s);

assert(res == Sqlite.OK);

Tables.Tags.ID.bind(s, tag.id);
Tables.Tags.NAME.bind(s, tag.name);
Tables.Tags.ICON.bind(s, tag.icon);
Tables.Tags.SELECTED.bind_bool(s, tag.selected);

res = s.step();

Expand Down
1 change: 1 addition & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ executable(
'ui/views/GamesView/GameListRow.vala',
'ui/views/GamesView/DownloadProgressView.vala',
'ui/views/GamesView/FiltersPopover.vala',
'ui/views/GamesView/GameContextMenu.vala',

'ui/views/GameDetailsView/GameDetailsView.vala',
'ui/views/GameDetailsView/GameDetailsPage.vala',
Expand Down
1 change: 0 additions & 1 deletion src/ui/dialogs/GameDetailsDialog.vala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ namespace GameHub.UI.Dialogs
Object(transient_for: Windows.MainWindow.instance, deletable: false, resizable: false, title: game.name);

gravity = Gdk.Gravity.CENTER;
modal = true;

var content = get_content_area();
content.set_size_request(560, -1);
Expand Down
2 changes: 1 addition & 1 deletion src/ui/views/GamesView/DownloadProgressView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ using GameHub.Data;
using GameHub.Utils;
using GameHub.UI.Widgets;

namespace GameHub.UI.Views
namespace GameHub.UI.Views.GamesView
{
public class DownloadProgressView: ListBoxRow
{
Expand Down
Loading

0 comments on commit 6765826

Please sign in to comment.