Skip to content

Commit

Permalink
View::ViewsOptions map for available options and loader method
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianfrasineanu committed Nov 18, 2016
1 parent 9e81cc2 commit abdf64e
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 20 deletions.
2 changes: 1 addition & 1 deletion app/Bootstrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ void main()
try
{
Console console;

View::loadViewsOptions();
do
{
try
Expand Down
38 changes: 30 additions & 8 deletions app/ClassContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <sstream>
#include <stdexcept>
#include <filesystem>
#include <map>

#include "Helpers.h"
#include "ClassContainer.h"
Expand Down Expand Up @@ -38,15 +39,17 @@ unsigned User::getUuid()

//---View---
//----------
View::View(char *viewName, vector<char> availableOptions, bool hasInterpolation)
map<string, map<char, string>> View::ViewsOptions = {{ "", {{'\0', ""}} }};

View::View(string viewName, map<char, string> availableOptions, bool hasInterpolation)
{
this->viewName = new char[strlen(viewName) + 1];
strcpy(this->viewName, viewName);
this->viewName = new char[strlen(viewName.c_str()) + 1];
strcpy(this->viewName, viewName.c_str());
this->availableOptions = availableOptions;
this->hasInterpolation = hasInterpolation;
}

vector<char> View::getAvailableOptions()
map<char, string> View::getAvailableOptions()
{
return this->availableOptions;
}
Expand All @@ -56,6 +59,15 @@ char *View::getViewName()
return this->viewName;
}

void View::loadViewsOptions()
{
string someView = "aView",
anotherView = "aaaView";

View::ViewsOptions = { { someView, {{'1', "theview.view"}}},
{anotherView, {{'2', "someview.view"}}} };
}

View::~View()
{
delete[] this->viewName;
Expand All @@ -64,14 +76,16 @@ View::~View()
//---Console---
//-------------
// We need an initial state, because we don't have any means like mapping views to HTTP routes
char *Console::initialView = "home.view";
string Console::initialView = "home.view";
char *Console::viewsFolder = "..\\views";

Console::Console()
{
this->mode = new char[strlen("live") + 1];
strcpy(this->mode, "live");
this->currentView = new View(Console::initialView, { '1', '2', '3', '4', '5', 'q' }, false);
map<char, string> availableOptions = { {'1', "login.view"}, {'2', "signup.view"}, {'3', "browse-index.view"}, {'4', "faq.view"}, {'q', "quit"} };

this->currentView = new View(Console::initialView, availableOptions, false);
this->delay = 2000;

this->loadViews(Console::viewsFolder);
Expand All @@ -91,7 +105,8 @@ Console::Console(char *mode)
{
this->mode = new char[strlen("debug") + 1];
strcpy(this->mode, "debug");
this->currentView = new View("debug.view", { 'q' }, true);
string viewName = "debug.view";
this->currentView = new View(viewName, { {'q', "quit"} }, true);

this->loadViews(Console::viewsFolder);

Expand All @@ -108,7 +123,7 @@ Console::Console(char *mode)

void Console::setLastInput(char input)
{
if (isInCharVector(this->currentView->getAvailableOptions(), input))
if (isInCharStringMap(this->currentView->getAvailableOptions(), input))
{
this->lastInput = input;
}
Expand Down Expand Up @@ -164,6 +179,13 @@ void Console::renderView(View &view)
viewFile.close();
}

void Console::renderNextView()
{
this->currentView = new View(this->currentView->getAvailableOptions().find(this->getLastInput())->second, { {'5', "lala"} }, false);

//TODO: finish the renderer
}

void Console::reloadView()
{
system("cls");
Expand Down
18 changes: 12 additions & 6 deletions app/ClassContainer.h
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
#include <iostream>
#include <filesystem>
#include <map>

using namespace std;
namespace fs = std::experimental::filesystem::v1;

class View {
private:
static map<string, map<char, string>> ViewsOptions;
char *viewName;
bool hasInterpolation;
vector<char> availableOptions;
map<char, string> availableOptions;

View();
public:
View(char *, vector<char>, bool);
vector<char> getAvailableOptions();
View(string, map<char, string>, bool);

map<char, string> getAvailableOptions();
char *getViewName();
static void loadViewsOptions();

~View();
};

class Console {
private:
static char *initialView;
static string initialView;
static char *viewsFolder;
View *currentView;
char *mode;
char lastInput;
unsigned delay;

View *currentView;
vector<fs::path> loadedViews;

void loadViews(const fs::path &);
Expand All @@ -36,6 +41,7 @@ class Console {
char getLastInput();
void setLastInput(char);
void renderView(View &);
void renderNextView();
void reloadView();
unsigned getDelay();

Expand Down
6 changes: 6 additions & 0 deletions app/Helpers.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
#include <iostream>
#include <vector>
#include <map>

#include "Helpers.h"

bool isInCharStringMap(const map<char, string> &haystack, char needle)
{
return haystack.find(needle) != haystack.end();
}

bool isInCharVector(vector<char> haystack, char needle)
{
return find(haystack.begin(), haystack.end(), needle) != haystack.end();
Expand Down
7 changes: 4 additions & 3 deletions app/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using namespace std;

bool isInCharVector(vector<char>, char);
bool isInIntVector(vector<int>, int);
bool isInStringVector(vector<string>, string);
bool isInCharStringMap(const map<char, string> &, char);
bool isInCharVector(const vector<char> &, char);
bool isInIntVector(const vector<int> &, int);
bool isInStringVector(const vector<string> &, string);
2 changes: 1 addition & 1 deletion views/home.view
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ _\ \ || (_| | (__| </ ___/| | |_| \__ \/ ___/| | |_| \__ \
| 4) FAQ
| 5) Help

| *Press q to quit the application...
*** Press q to quit the application...
2 changes: 1 addition & 1 deletion views/login.view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
_\ \ || (_| | (__| </ ___/| | |_| \__ \/ ___/| | |_| \__ \
\__/\__\__,_|\___|_|\_\/ |_|\__,_|___/\/ |_|\__,_|___/

Welcome back!
Welcome back again!

@{

0 comments on commit abdf64e

Please sign in to comment.