Start showing the list of games in the database

This commit is contained in:
Savanni D'Gerinel 2023-07-25 23:27:36 -04:00
parent 744511a552
commit 084a558740
7 changed files with 68 additions and 9 deletions

View File

@ -121,6 +121,12 @@ impl TryFrom<&str> for Rank {
}
}
impl ToString for Rank {
fn to_string(&self) -> String {
unimplemented!()
}
}
#[derive(Clone, Debug)]
pub struct GameTree {
pub file_format: i8,

View File

@ -12,7 +12,6 @@ use typeshare::typeshare;
#[serde(tag = "type", content = "content")]
pub enum CoreRequest {
CreateGame(CreateGameRequest),
LaunchScreen,
Home,
PlayingField,
PlayStone(PlayStoneRequest),
@ -119,8 +118,9 @@ impl CoreApp {
let game_state = app_state.game.as_ref().unwrap();
CoreResponse::PlayingFieldView(playing_field(game_state))
}
CoreRequest::LaunchScreen => CoreResponse::HomeView(home()),
CoreRequest::Home => CoreResponse::HomeView(home()),
CoreRequest::Home => {
CoreResponse::HomeView(home(self.state.read().unwrap().database.all_games()))
}
CoreRequest::PlayingField => {
let app_state = self.state.read().unwrap();
let game = app_state.game.as_ref().unwrap();

View File

@ -13,7 +13,7 @@ pub struct GamePreviewElement {
}
impl GamePreviewElement {
pub fn new(game: GameTree) -> GamePreviewElement {
pub fn new(game: &GameTree) -> GamePreviewElement {
GamePreviewElement {
date: game.info.date.clone(),
black_player: game

View File

@ -1,4 +1,5 @@
use crate::ui::{Action, GamePreviewElement};
use go_sgf::GameTree;
use serde::{Deserialize, Serialize};
use typeshare::typeshare;
@ -55,7 +56,7 @@ pub struct HomeView {
pub start_game: Action<()>,
}
pub fn home() -> HomeView {
pub fn home<'a>(games: impl Iterator<Item = &'a GameTree>) -> HomeView {
let black_player = PlayerElement::Hotseat(HotseatPlayerElement {
placeholder: Some("black player".to_owned()),
default_rank: None,
@ -69,7 +70,7 @@ pub fn home() -> HomeView {
HomeView {
black_player,
white_player,
games: vec![],
games: games.map(GamePreviewElement::new).collect(),
start_game: Action {
id: "start-game-action".to_owned(),
label: "New Game".to_owned(),

View File

@ -0,0 +1,42 @@
use glib::Object;
use gtk::{glib, prelude::*, subclass::prelude::*};
use kifu_core::ui::GamePreviewElement;
#[derive(Default)]
pub struct GamePreviewPrivate;
#[glib::object_subclass]
impl ObjectSubclass for GamePreviewPrivate {
const NAME: &'static str = "GamePreview";
type Type = GamePreview;
type ParentType = gtk::Box;
}
impl ObjectImpl for GamePreviewPrivate {}
impl WidgetImpl for GamePreviewPrivate {}
impl BoxImpl for GamePreviewPrivate {}
glib::wrapper! {
pub struct GamePreview(ObjectSubclass<GamePreviewPrivate>) @extends gtk::Box, gtk::Widget, @implements gtk::Orientable;
}
impl GamePreview {
pub fn new(element: GamePreviewElement) -> GamePreview {
let s: Self = Object::builder().build();
s.set_orientation(gtk::Orientation::Horizontal);
println!("game_preview: {:?}", element);
let black_player = match element.black_rank {
Some(rank) => format!("{} ({})", element.black_player, rank.to_string()),
None => element.black_player,
};
let white_player = match element.white_rank {
Some(rank) => format!("{} ({})", element.white_player, rank.to_string()),
None => element.white_player,
};
s.append(&gtk::Label::new(Some(&black_player)));
s.append(&gtk::Label::new(Some(&white_player)));
s
}
}

View File

@ -1,3 +1,4 @@
use crate::ui::GamePreview;
use crate::CoreApi;
use glib::Object;
use gtk::{glib, prelude::*, subclass::prelude::*};
@ -138,6 +139,12 @@ impl Home {
}
});
let game_list = gtk::Box::new(gtk::Orientation::Vertical, 0);
s.attach(&game_list, 1, 3, 2, 1);
view.games
.iter()
.for_each(|game_preview| game_list.append(&GamePreview::new(game_preview.clone())));
s
}
}

View File

@ -1,9 +1,12 @@
mod player_card;
pub use player_card::PlayerCard;
mod chat;
pub use chat::Chat;
mod game_preview;
pub use game_preview::GamePreview;
mod player_card;
pub use player_card::PlayerCard;
mod playing_field;
pub use playing_field::PlayingField;