Moved emoji sets and dictionaries loader states to CloudBlob.

- Moved CloudBlob to second namespace.
This commit is contained in:
23rd 2020-02-05 16:06:38 +03:00
parent 9f4d05b04c
commit 704dcc8d65
6 changed files with 65 additions and 93 deletions

View File

@ -38,48 +38,15 @@ namespace Ui {
namespace {
using Dictionaries = std::vector<int>;
using namespace Storage::CloudBlob;
struct Available {
int size = 0;
inline bool operator<(const Available &other) const {
return size < other.size;
}
inline bool operator==(const Available &other) const {
return size == other.size;
}
};
struct Ready {
inline bool operator<(const Ready &other) const {
return false;
}
inline bool operator==(const Ready &other) const {
return true;
}
};
struct Active {
inline bool operator<(const Active &other) const {
return false;
}
inline bool operator==(const Active &other) const {
return true;
}
};
using Loading = MTP::DedicatedLoader::Progress;
struct Failed {
inline bool operator<(const Failed &other) const {
return false;
}
inline bool operator==(const Failed &other) const {
return true;
}
};
using SetState = base::variant<
using DictState = base::variant<
Available,
Ready,
Active,
Loading,
Failed>;
Failed,
Loading>;
class Loader : public QObject {
public:
@ -87,7 +54,7 @@ public:
int id() const;
rpl::producer<SetState> state() const;
rpl::producer<DictState> state() const;
void destroy();
private:
@ -98,7 +65,7 @@ private:
int _id = 0;
int _size = 0;
rpl::variable<SetState> _state;
rpl::variable<DictState> _state;
MTP::WeakInstance _mtproto;
std::unique_ptr<MTP::DedicatedLoader> _implementation;
@ -148,7 +115,7 @@ MTP::DedicatedLoader::Location GetDownloadLocation(int id) {
return MTP::DedicatedLoader::Location{ kUsername, i->postId };
}
SetState ComputeState(int id) {
DictState ComputeState(int id) {
// if (id == CurrentSetId()) {
// return Active();
if (Spellchecker::DictionaryExists(id)) {
@ -157,7 +124,7 @@ SetState ComputeState(int id) {
return Available{ GetDownloadSize(id) };
}
QString StateDescription(const SetState &state) {
QString StateDescription(const DictState &state) {
return state.match([](const Available &data) {
return tr::lng_emoji_set_download(tr::now, lt_size, formatSizeText(data.size));
}, [](const Ready &data) -> QString {
@ -202,7 +169,7 @@ int Loader::id() const {
return _id;
}
rpl::producer<SetState> Loader::state() const {
rpl::producer<DictState> Loader::state() const {
return _state.value();
}
@ -210,11 +177,11 @@ void Loader::setImplementation(
std::unique_ptr<MTP::DedicatedLoader> loader) {
_implementation = std::move(loader);
auto convert = [](auto value) {
return SetState(value);
return DictState(value);
};
_state = _implementation->progress(
) | rpl::map([](const Loading &state) {
return SetState(state);
return DictState(state);
});
_implementation->failed(
) | rpl::start_with_next([=] {
@ -287,7 +254,7 @@ auto AddButtonWithLoader(
)->entity();
const auto buttonState = button->lifetime()
.make_state<rpl::variable<SetState>>();
.make_state<rpl::variable<DictState>>();
const auto label = Ui::CreateChild<Ui::FlatLabel>(
button,
@ -305,7 +272,7 @@ auto AddButtonWithLoader(
}, label->lifetime());
buttonState->value(
) | rpl::start_with_next([=](const SetState &state) {
) | rpl::start_with_next([=](const DictState &state) {
const auto isToggledSet = state.is<Active>();
const auto toggled = isToggledSet ? 1. : 0.;
const auto over = !button->isDisabled()
@ -326,9 +293,9 @@ auto AddButtonWithLoader(
buttonEnabled
) | rpl::then(
buttonState->value(
) | rpl::filter([](const SetState &state) {
) | rpl::filter([](const DictState &state) {
return state.is<Failed>();
}) | rpl::map([](const SetState &state) {
}) | rpl::map([](const auto &state) {
return false;
})
)
@ -346,15 +313,15 @@ auto AddButtonWithLoader(
) | rpl::map([=](auto enabled) {
const auto &state = buttonState->current();
if (enabled && state.is<Ready>()) {
return SetState(Active());
return DictState(Active());
}
if (!enabled && state.is<Active>()) {
return SetState(Ready());
return DictState(Ready());
}
return ComputeState(id);
});
}) | rpl::flatten_latest(
) | rpl::filter([=](const SetState &state) {
) | rpl::filter([=](const DictState &state) {
return !buttonState->current().is<Failed>() || !state.is<Available>();
});

View File

@ -31,7 +31,9 @@ namespace Ui {
namespace Emoji {
namespace {
struct Set : public Storage::Blob {
using namespace Storage::CloudBlob;
struct Set : public Blob {
QString previewPath;
};
@ -50,41 +52,7 @@ auto Sets() {
return kSets;
}
struct Available {
int size = 0;
inline bool operator<(const Available &other) const {
return size < other.size;
}
inline bool operator==(const Available &other) const {
return size == other.size;
}
};
struct Ready {
inline bool operator<(const Ready &other) const {
return false;
}
inline bool operator==(const Ready &other) const {
return true;
}
};
struct Active {
inline bool operator<(const Active &other) const {
return false;
}
inline bool operator==(const Active &other) const {
return true;
}
};
using Loading = MTP::DedicatedLoader::Progress;
struct Failed {
inline bool operator<(const Failed &other) const {
return false;
}
inline bool operator==(const Failed &other) const {
return true;
}
};
using SetState = base::variant<
Available,
Ready,
@ -224,7 +192,7 @@ bool GoodSetPartName(const QString &name) {
}
bool UnpackSet(const QString &path, const QString &folder) {
return Storage::UnpackBlob(path, folder, GoodSetPartName);
return UnpackBlob(path, folder, GoodSetPartName);
}
Loader::Loader(QObject *parent, int id)

View File

@ -15,6 +15,8 @@ namespace Spellchecker {
namespace {
using namespace Storage::CloudBlob;
// Language With Country.
inline auto LWC(QLocale::Country country) {
const auto l = QLocale::matchingLocales(
@ -108,7 +110,7 @@ QString DictionariesPath() {
bool UnpackDictionary(const QString &path, int langId) {
const auto folder = DictPathByLangId(langId);
return Storage::UnpackBlob(path, folder, IsGoodPartName);
return UnpackBlob(path, folder, IsGoodPartName);
}
bool DictionaryExists(int langId) {

View File

@ -13,7 +13,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
namespace Spellchecker {
struct Dict : public Storage::Blob {
struct Dict : public Storage::CloudBlob::Blob {
};
[[nodiscard]] QString DictionariesPath();

View File

@ -9,7 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "base/zlib_help.h"
namespace Storage {
namespace Storage::CloudBlob {
namespace {
@ -64,4 +64,4 @@ bool UnpackBlob(
return true;
}
} // namespace Storage
} // namespace Storage::CloudBlob

View File

@ -7,7 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once
namespace Storage {
namespace Storage::CloudBlob {
struct Blob {
int id = 0;
@ -16,9 +16,44 @@ struct Blob {
QString name;
};
struct Available {
int size = 0;
inline bool operator<(const Available &other) const {
return size < other.size;
}
inline bool operator==(const Available &other) const {
return size == other.size;
}
};
struct Ready {
inline bool operator<(const Ready &other) const {
return false;
}
inline bool operator==(const Ready &other) const {
return true;
}
};
struct Active {
inline bool operator<(const Active &other) const {
return false;
}
inline bool operator==(const Active &other) const {
return true;
}
};
struct Failed {
inline bool operator<(const Failed &other) const {
return false;
}
inline bool operator==(const Failed &other) const {
return true;
}
};
bool UnpackBlob(
const QString &path,
const QString &folder,
Fn<bool(const QString &)> checkNameCallback);
} // namespace Storage
} // namespace Storage::CloudBlob