Skip to content

Commit

Permalink
simplify auth response
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDavenport committed Apr 25, 2024
1 parent 0aa0e60 commit 58885c8
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 38 deletions.
67 changes: 38 additions & 29 deletions gamercade_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::{
local_directory::LocalDirectory,
modes::{AppMode, ArcadeModeView, LibraryModeView, SettingsModeView},
task_manager::{
AuthState, AuthorRequest, GameResponse, HttpResponse, PlatformRequest, PlatformResponse,
SuperTaskManager, TagRequest, TaskNotification,
AuthResponse, AuthState, AuthorRequest, GameResponse, HttpResponse, PlatformRequest,
PlatformResponse, SuperTaskManager, TagRequest, TaskNotification,
},
};

Expand Down Expand Up @@ -85,33 +85,9 @@ impl App {
TaskNotification::GlobalPermissionLevels(permissions) => {
self.directory.upsert_permission_levesl(&permissions, true);
}
TaskNotification::AuthStateChanged(new_state) => {
self.auth_state = new_state;

self.modes.arcade.login.waiting = false;

match &self.auth_state {
AuthState::Unauthorized => self.modes.arcade.logged_out(),
AuthState::SessionHeld(session) => {
self.modes.arcade.logged_in();
self.tasks
.platform
.send(PlatformRequest::FrontPage(FrontPageRequest {}));
self.tasks
.platform
.send(PlatformRequest::VotedGames(session.clone()));
self.tasks
.platform
.send(PlatformRequest::EditableGames(session.clone()));
self.tasks.tags.send(TagRequest::Initialize);
self.tasks.author.send(AuthorRequest::Initialize);
}
}
}
TaskNotification::LoginFailed => {
self.modes.arcade.login.waiting = false;
self.modes.arcade.logged_out();
}

TaskNotification::AuthResponse(response) => self.handle_auth_response(response),

TaskNotification::PlatformResponse(response) => {
self.handle_platform_response(response)
}
Expand All @@ -121,6 +97,39 @@ impl App {
}
}

fn handle_auth_response(&mut self, response: AuthResponse) {
match response {
AuthResponse::LoggedIn(new_state) => {
self.auth_state = new_state;

self.modes.arcade.login.waiting = false;

match &self.auth_state {
AuthState::Unauthorized => self.modes.arcade.logged_out(),
AuthState::SessionHeld(session) => {
self.modes.arcade.logged_in();
self.tasks
.platform
.send(PlatformRequest::FrontPage(FrontPageRequest {}));
self.tasks
.platform
.send(PlatformRequest::VotedGames(session.clone()));
self.tasks
.platform
.send(PlatformRequest::EditableGames(session.clone()));
self.tasks.tags.send(TagRequest::Initialize);
self.tasks.author.send(AuthorRequest::Initialize);
}
}
}
AuthResponse::Error(e) => {
self.modes.arcade.login.waiting = false;
self.modes.arcade.logged_out();
println!("Auth error: {e}");
}
}
}

fn handle_game_response(&mut self, response: GameResponse) {
let mut update = None;
match response {
Expand Down
22 changes: 16 additions & 6 deletions gamercade_app/src/task_manager/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ pub enum AuthRequest {
SignUp(SignUpRequest),
}

#[derive(Debug)]
pub enum AuthResponse {
LoggedIn(AuthState),
Error(String),
}

impl Debug for AuthRequest {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expand Down Expand Up @@ -87,8 +93,8 @@ async fn handle_login(

if let Ok(session) = Session::try_from(response.session.as_slice()) {
sender
.send(TaskNotification::AuthStateChanged(AuthState::SessionHeld(
session,
.send(TaskNotification::AuthResponse(AuthResponse::LoggedIn(
AuthState::SessionHeld(session),
)))
.await
.unwrap();
Expand All @@ -98,8 +104,12 @@ async fn handle_login(
}
}
Err(e) => {
sender.send(TaskNotification::LoginFailed).await.unwrap();
println!("{e}");
sender
.send(TaskNotification::AuthResponse(AuthResponse::Error(
e.to_string(),
)))
.await
.unwrap();
}
}
}
Expand All @@ -116,8 +126,8 @@ async fn handle_sign_up(

if let Ok(session) = Session::try_from(response.session.as_slice()) {
sender
.send(TaskNotification::AuthStateChanged(AuthState::SessionHeld(
session,
.send(TaskNotification::AuthResponse(AuthResponse::LoggedIn(
AuthState::SessionHeld(session),
)))
.await
.unwrap();
Expand Down
9 changes: 6 additions & 3 deletions gamercade_app/src/task_manager/super_task_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ use tokio::sync::mpsc::{channel, Receiver};
use crate::local_directory::{PermissionLevel, PermissionLevelId, Tag, TagId};

use super::{
AuthManager, AuthState, AuthorManager, GameManager, GameResponse, HttpManager, HttpResponse,
AuthManager, AuthResponse, AuthorManager, GameManager, GameResponse, HttpManager, HttpResponse,
PlatformManager, PlatformResponse, TagManager, SUPER_TASK_CHANNEL_SIZE,
};

#[derive(Debug)]
pub enum TaskNotification {
GlobalTags(Vec<(TagId, Tag)>),
GlobalPermissionLevels(Vec<(PermissionLevelId, PermissionLevel)>),
AuthStateChanged(AuthState),
LoginFailed,
//AuthStateChanged(AuthState),
//LoginFailed,

// Auth
AuthResponse(AuthResponse),

// Game
GameResponse(GameResponse),
Expand Down

0 comments on commit 58885c8

Please sign in to comment.