168 lines
4.7 KiB
C++
168 lines
4.7 KiB
C++
#ifndef TJDUIAPP_EBOOK_PRESENTER
|
||
#define TJDUIAPP_EBOOK_PRESENTER
|
||
|
||
#include "Presenter.h"
|
||
#include "TjdUiScreenDrag.h"
|
||
#include "graphic_timer.h"
|
||
#include "root_view.h"
|
||
#include "ui_list.h"
|
||
#include "ui_view.h"
|
||
#include <fstream>
|
||
#include <list>
|
||
#include <vector>
|
||
|
||
namespace TJD {
|
||
#define BOOK_BIN_PATH TJD_IMAGE_PATH "img_ebook.bin"
|
||
|
||
constexpr int16_t EBOOK_ITEM_H = 128;
|
||
constexpr int16_t EBOOK_ITEM_W = 450;
|
||
constexpr int16_t EBOOK_DELECT_ICON_INTERVAL = 30;
|
||
constexpr int16_t EBOOK_DELECT_ICON_H = 100;
|
||
constexpr int16_t EBOOK_DELECT_ICON_W = 100;
|
||
constexpr int16_t EBOOK_ITEM_MARGIN_BOTTOM = 13;
|
||
|
||
enum EbookPageType
|
||
{
|
||
LAST_PAGE = 0,
|
||
NEXT_PAGE
|
||
};
|
||
|
||
enum EbookView
|
||
{
|
||
EBOOK_NONE_VIEW,
|
||
EBOOK_LIST_VIEW,
|
||
EBOOK_MAIN_VIEW,
|
||
EBOOK_DELETE_VIEW,
|
||
EBOOK_SETTING_VIEW,
|
||
EBOOK_VIEW_MAX
|
||
};
|
||
|
||
struct EbookItem
|
||
{
|
||
uint8_t id;
|
||
std::string name;
|
||
uint32_t curChapter;
|
||
uint32_t curPage;
|
||
bool loadOver;
|
||
|
||
EbookItem()
|
||
{
|
||
id = 0;
|
||
name = "";
|
||
curChapter = 0;
|
||
curPage = 0;
|
||
loadOver = false;
|
||
}
|
||
// 构造函数(必要,使用std::list::emplace_back函数)
|
||
EbookItem(uint8_t id, std::string name, uint32_t curChapter, uint32_t curPage, bool loadOver)
|
||
{
|
||
this->id = id;
|
||
this->name = name;
|
||
this->curChapter = curChapter;
|
||
this->curPage = curPage;
|
||
this->loadOver = loadOver;
|
||
}
|
||
};
|
||
|
||
struct EbookSizePerPage
|
||
{
|
||
uint8_t id;
|
||
std::vector<uint32_t> sizePerPageCounts;
|
||
EbookSizePerPage() {}
|
||
EbookSizePerPage(uint8_t id, std::initializer_list<uint32_t> init) : id(id), sizePerPageCounts(init) {}
|
||
};
|
||
|
||
class TextBlockManager
|
||
{
|
||
public:
|
||
TextBlockManager() { buffer_.resize(blockSize); }
|
||
~TextBlockManager() { CloseFile(); }
|
||
|
||
bool OpenFile(const char *filename);
|
||
void CloseFile();
|
||
|
||
bool readFromFile(size_t pos);
|
||
bool FileIsOpen(void) { return file_.is_open(); }
|
||
std::string GetBuffer(void);
|
||
|
||
private:
|
||
uint32_t readIntoBuffer(std::vector<char> &buffer, size_t numBytes, std::streampos offset);
|
||
|
||
std::ifstream file_;
|
||
std::vector<char> buffer_;
|
||
const size_t blockSize = 1024; // 缓冲区大小设置为1KB
|
||
};
|
||
|
||
class TjdEbookItemView;
|
||
class TjdUiAppEbookView;
|
||
class TjdUiAppEbookPresenter : public OHOS::Presenter<TjdUiAppEbookView>,
|
||
public OHOS::UIView::OnClickListener,
|
||
public TjdUiScreenDragListener,
|
||
public OHOS::RootView::OnKeyActListener,
|
||
public OHOS::ListScrollListener
|
||
{
|
||
public:
|
||
TjdUiAppEbookPresenter();
|
||
~TjdUiAppEbookPresenter();
|
||
static TjdUiAppEbookPresenter *GetInstance(void);
|
||
|
||
void OnStart() override;
|
||
void OnResume() override;
|
||
void OnPause() override;
|
||
void OnStop() override;
|
||
|
||
bool OnClick(OHOS::UIView &view, const OHOS::ClickEvent &event) override;
|
||
void ScreenDragEventCallback(OHOS::UIView &view, const OHOS::DragEvent &event) override { ExitCurrentView(); }
|
||
bool OnKeyAct(OHOS::UIView &view, const OHOS::KeyEvent &event) override;
|
||
void OnItemSelected(int16_t index, OHOS::UIView *view) override;
|
||
void OnScrollStart(int16_t index, OHOS::UIView* view);
|
||
void OnScrollEnd(int16_t index, OHOS::UIView* view);
|
||
|
||
void ClickEBookAdapterCallback(OHOS::UIView *view, EbookItem item);
|
||
EbookItem *GetSelectedEbookItem(void) { return &selectedItem; }
|
||
TextBlockManager *GetTextBlockManager(void) { return textBuffer_; }
|
||
void FillTextToTextLabel(EbookPageType page);
|
||
void PreLoadBook();
|
||
void UnLoadBook();
|
||
void startThread();
|
||
static void LoadBookTask(void *arg);
|
||
void WaitAnimatorCallback();
|
||
static void JumpTimerCallback(void *param);
|
||
|
||
std::list<EbookItem> &GetBookListData(void) { return bookList; }
|
||
void DeleteBookListItem(uint32_t id);
|
||
EbookView GetCurrentView(void) { return currentView; }
|
||
EbookView currentView{EBOOK_LIST_VIEW};
|
||
bool isLoadComplite{false};
|
||
|
||
private:
|
||
void ExitEbookSlice(void);
|
||
void ExitCurrentView(void);
|
||
|
||
void StartJumpTimer();
|
||
void StopJumpTimer();
|
||
|
||
osTimerId_t jumpTimerId_{nullptr};
|
||
|
||
TjdEbookItemView *itemViewSele{nullptr};
|
||
TjdEbookItemView *preItemViewSele{nullptr};
|
||
|
||
std::list<EbookItem> bookList;
|
||
EbookItem selectedItem;
|
||
|
||
TextBlockManager *textBuffer_{nullptr};
|
||
std::vector<uint32_t> pageWordCounts_;
|
||
uint32_t curPage_ = 0; // 当前页
|
||
int32_t bufLastPos_ = 0;
|
||
int32_t bufNextPos_ = 0;
|
||
|
||
bool ebook_slice_inited = false;
|
||
bool isExit_{false};
|
||
int16_t dragStartX_{0};
|
||
int16_t dragCurrentX_{0};
|
||
int32_t dragDistance_{0};
|
||
};
|
||
|
||
} // namespace TJD
|
||
|
||
#endif |