Skip to content

Commit

Permalink
1. add real income compute 2. add pagination 3. optimizing code
Browse files Browse the repository at this point in the history
  • Loading branch information
geniusC committed Jul 24, 2020
1 parent 7528580 commit bd75734
Show file tree
Hide file tree
Showing 21 changed files with 915 additions and 772 deletions.
8 changes: 4 additions & 4 deletions include/colors.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
namespace BigMoney {

enum Color {
kRedBlack = 1,
kGreenBlack = 2,
kWhiteBlue = 3
kRedBlack = 1,
kGreenBlack = 2,
kWhiteBlue = 3
};

int GetColorPair(Color color);

} // namespace BigMoney
} // namespace BigMoney
14 changes: 7 additions & 7 deletions include/command_bar.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ namespace BigMoney {

class CommandBar : public Window{
public:
CommandBar(int x, int y, int startx, int starty);
void GetCommand();
~CommandBar();
CommandBar(int x, int y, int startx, int starty);
void GetCommand();
~CommandBar();

private:
void ParseCommand(const std::string &cmd);
void ParseCommand(const std::string &cmd);
private:
std::thread *listen_thread_{nullptr};
bool is_listen_{false};
std::thread *listen_thread_{nullptr};
bool is_listen_{false};
};

} // namespace BigMoney
} // namespace BigMoney
12 changes: 6 additions & 6 deletions include/earn.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ namespace BigMoney {

class Earn {
public:
Earn();
~Earn();
Earn();
~Earn();
private:
FundBoard *fund_board_{nullptr};
HelpWindow* help_windows_{nullptr};
StatusBar *status_bar_{nullptr};
CommandBar *command_bar_{nullptr};
FundBoard *fund_board_{nullptr};
HelpWindow* help_windows_{nullptr};
StatusBar *status_bar_{nullptr};
CommandBar *command_bar_{nullptr};
};

} // namespace BigMoney
26 changes: 16 additions & 10 deletions include/fund.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@

namespace BigMoney {
struct Fund {
std::string fund_code;
float fund_worth{0.0f};
float valuation{0.0f};
float fluctuations{0.0f};
float share{0.0f};
float income{0.0f};
float sum{0.0f};
std::string fund_name;
std::string last_update_time;
std::string fund_code;
float fund_worth{0.0f};
float valuation{0.0f};
float fluctuations{0.0f};
float share{0.0f};
float income{0.0f};
float real_income{0.0f};
float sum{0.0f};
std::string fund_name;
std::string last_update_time;
};
} // namespace BigMoney
struct FundIncome {
float income{0.0f};
float read_income{0.0f};
float sum{0.0f};
};
} // namespace BigMoney
38 changes: 22 additions & 16 deletions include/fund_board.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,28 @@
namespace BigMoney {
class FundBoard : public Window {
public:
FundBoard(int x, int y, int startx, int starty);
~FundBoard();
void Paint() override;
virtual bool MessageProc(const Msg &msg) override;
FundBoard(int x, int y, int startx, int starty);
~FundBoard();
void Paint() override;
virtual bool MessageProc(const Msg &msg) override;
private:
void GetFundData();
void LoadFundFromFile();
static size_t WriteFunction(void *data, size_t size, size_t bytes, void *user_data);
bool UpdateFund(const Fund& fund);
bool DeleteFund(const std::string &fund_code);
bool Serialize();
void GetFundData();
void LoadFundFromFile();
static size_t WriteFunction(void *data, size_t size, size_t bytes, void *user_data);
bool UpdateFund(const Fund& fund);
bool DeleteFund(const std::string &fund_code);
bool Serialize();
private:
const static std::array<std::pair<std::string, int>, 9> FIELD_WIDTH_MAP;
std::vector<Fund> funds_;
CURL *curl_{nullptr};
std::mutex fund_mutex_;
Timer *timer{nullptr};
const static std::array<std::pair<std::string, int>, 10> FIELD_WIDTH_MAP;
std::vector<Fund> funds_;
CURL *curl_{nullptr};
std::mutex fund_mutex_;
Timer *timer{nullptr};
uint32_t page_{0};
uint32_t per_page_{0};
uint32_t max_page_{0};
std::atomic<bool> request_flag_{false};
std::atomic<bool> running_{true};
std::thread *request_thread_{nullptr};
};
} // namespace BigMoney
} // namespace BigMoney
10 changes: 5 additions & 5 deletions include/help.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ namespace BigMoney {

class HelpWindow : public Window {
public:
HelpWindow(int x, int y, int startx, int starty);
bool MessageProc(const Msg& msg) override;
void Paint() override;
HelpWindow(int x, int y, int startx, int starty);
bool MessageProc(const Msg& msg) override;
void Paint() override;
private:
bool show_{ false };
bool show_{ false };
};

} // namespace BigMoney
} // namespace BigMoney
72 changes: 37 additions & 35 deletions include/msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,59 +10,61 @@
namespace BigMoney {

enum MsgType {
kUpdateFund,
kDeleteFund,
kUpdateIncome,
kUpdateStatus,
kPaint,
kShowHelp,
kHiddenPop,
kReloadFile,
kQuit,
kUpdateFund,
kDeleteFund,
kUpdateIncome,
kUpdateStatus,
kPrePage,
kNextPage,
kPaint,
kShowHelp,
kHiddenPop,
kReloadFile,
kQuit,
};

struct Msg {
MsgType msg_type;
void *lparam;
void *rparam;
MsgType msg_type;
void *lparam;
void *rparam;
};

class MsgQueue {
public:
bool Enqueue(const Msg& msg);
bool Dequeue(Msg *msg);
bool Empty();
bool Enqueue(const Msg& msg);
bool Dequeue(Msg *msg);
bool Empty();
private:
std::queue<Msg> msg_queue_;
std::mutex mutex_;
std::queue<Msg> msg_queue_;
std::mutex mutex_;
};


class MsgReactor {
public:
MsgReactor();
virtual ~MsgReactor();
virtual bool MessageProc(const Msg &msg) = 0;
MsgReactor();
virtual ~MsgReactor();
virtual bool MessageProc(const Msg &msg) = 0;
};

class MsgManager : public sy::Singleton<MsgManager> {
public:
inline bool Enqueue(const Msg& msg) {
return msg_queue_.Enqueue(msg);
}
inline bool Dequeue(Msg *msg) {
return msg_queue_.Dequeue(msg);
}
inline bool QueueEmpty() {
return msg_queue_.Empty();
}
void AddReactor(MsgReactor *reactor);
void RemoveReactor(MsgReactor *reactor);
void StartMainLoop();
inline bool Enqueue(const Msg& msg) {
return msg_queue_.Enqueue(msg);
}
inline bool Dequeue(Msg *msg) {
return msg_queue_.Dequeue(msg);
}
inline bool QueueEmpty() {
return msg_queue_.Empty();
}
void AddReactor(MsgReactor *reactor);
void RemoveReactor(MsgReactor *reactor);
void StartMainLoop();
private:
MsgQueue msg_queue_;
std::unordered_set<MsgReactor*> reactors_;
std::mutex reactors_mutex_;
MsgQueue msg_queue_;
std::unordered_set<MsgReactor*> reactors_;
std::mutex reactors_mutex_;
};

bool GetMsg(Msg *msg);
Expand Down
22 changes: 11 additions & 11 deletions include/status_bar.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
#include "timer.h"
#include <atomic>
#include <mutex>
#include "fund.h"

namespace BigMoney {

class StatusBar : Window{
public:
StatusBar(int x, int y, int startx, int starty);
~StatusBar();
void Paint() override;
bool MessageProc(const Msg &msg) override;
StatusBar(int x, int y, int startx, int starty);
~StatusBar();
void Paint() override;
bool MessageProc(const Msg &msg) override;
private:
std::string show_msg_;
float income_{0.0f};
float sum_{0.0f};
Timer *timer_{nullptr};
std::atomic<bool> update_{false};
std::mutex msg_mutex_;
std::string show_msg_;
FundIncome fund_income_{0.0f, 0.0f, 0.0f};
Timer *timer_{nullptr};
std::atomic<bool> update_{false};
std::mutex msg_mutex_;
};
} // namespace BigMoney
} // namespace BigMoney
38 changes: 19 additions & 19 deletions include/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,33 @@ namespace BigMoney {
class TimerManager;

class Timer {
friend class TimerManager;
typedef std::function<void(void)> TimeoutCallback;
friend class TimerManager;
typedef std::function<void(void)> TimeoutCallback;
public:
Timer(const TimeoutCallback &callback);
~Timer();
void Start(uint32_t milliseconds);
void Stop();
Timer(const TimeoutCallback &callback);
~Timer();
void Start(uint32_t milliseconds);
void Stop();
private:
std::atomic<bool> running_{false};
TimeoutCallback callback_;
std::chrono::time_point<std::chrono::system_clock> start_time_;
int timeout_{0};
std::atomic<bool> running_{false};
TimeoutCallback callback_;
std::chrono::time_point<std::chrono::system_clock> start_time_;
int timeout_{0};
};

class TimerManager :
public sy::Singleton<TimerManager>, public MsgReactor {
public:
void AddTimer(Timer *timer);
void RemoveTimer(Timer *timer);
bool MessageProc(const Msg &msg) override;
void StartWork();
~TimerManager();
void AddTimer(Timer *timer);
void RemoveTimer(Timer *timer);
bool MessageProc(const Msg &msg) override;
void StartWork();
~TimerManager();
private:
std::unordered_set<Timer*> timers_;
std::mutex timers_mutex_;
std::atomic<bool> running_{false};
std::thread work_thread_;
std::unordered_set<Timer*> timers_;
std::mutex timers_mutex_;
std::atomic<bool> running_{false};
std::thread work_thread_;
};

void StartTimerLoop();
Expand Down
Loading

0 comments on commit bd75734

Please sign in to comment.