mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-06-05 06:33:57 +02:00
Choose newly uploaded ringtone in the box.
This commit is contained in:
parent
d9f6a5206e
commit
c71ba2b8e7
4 changed files with 90 additions and 36 deletions
|
@ -112,7 +112,9 @@ void Ringtones::ready(const FullMsgId &msgId, const MTPInputFile &file) {
|
||||||
MTP_string(uploadedData.filename),
|
MTP_string(uploadedData.filename),
|
||||||
MTP_string(uploadedData.filemime)
|
MTP_string(uploadedData.filemime)
|
||||||
)).done([=](const MTPDocument &result) {
|
)).done([=](const MTPDocument &result) {
|
||||||
_session->data().processDocument(result);
|
const auto document = _session->data().processDocument(result);
|
||||||
|
_list.documents.insert(_list.documents.begin(), document->id);
|
||||||
|
_uploadDones.fire_copy(document->id);
|
||||||
}).fail([=](const MTP::Error &error) {
|
}).fail([=](const MTP::Error &error) {
|
||||||
_uploadFails.fire_copy(error.type());
|
_uploadFails.fire_copy(error.type());
|
||||||
}).send();
|
}).send();
|
||||||
|
@ -128,11 +130,11 @@ void Ringtones::requestList() {
|
||||||
_list.requestId = 0;
|
_list.requestId = 0;
|
||||||
result.match([&](const MTPDaccount_savedRingtones &data) {
|
result.match([&](const MTPDaccount_savedRingtones &data) {
|
||||||
_list.hash = data.vhash().v;
|
_list.hash = data.vhash().v;
|
||||||
_list.documents.reserve(_list.documents.size()
|
_list.documents.clear();
|
||||||
+ data.vringtones().v.size());
|
_list.documents.reserve(data.vringtones().v.size());
|
||||||
for (const auto &d : data.vringtones().v) {
|
for (const auto &d : data.vringtones().v) {
|
||||||
const auto document = _session->data().processDocument(d);
|
const auto document = _session->data().processDocument(d);
|
||||||
_list.documents.emplace(document->id);
|
_list.documents.emplace_back(document->id);
|
||||||
}
|
}
|
||||||
requestList();
|
requestList();
|
||||||
_list.updates.fire({});
|
_list.updates.fire({});
|
||||||
|
@ -155,6 +157,10 @@ rpl::producer<QString> Ringtones::uploadFails() const {
|
||||||
return _uploadFails.events();
|
return _uploadFails.events();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rpl::producer<DocumentId> Ringtones::uploadDones() const {
|
||||||
|
return _uploadDones.events();
|
||||||
|
}
|
||||||
|
|
||||||
void Ringtones::applyUpdate() {
|
void Ringtones::applyUpdate() {
|
||||||
_list.hash = 0;
|
_list.hash = 0;
|
||||||
_list.documents.clear();
|
_list.documents.clear();
|
||||||
|
|
|
@ -22,7 +22,7 @@ class Ringtones final {
|
||||||
public:
|
public:
|
||||||
explicit Ringtones(not_null<ApiWrap*> api);
|
explicit Ringtones(not_null<ApiWrap*> api);
|
||||||
|
|
||||||
using Ids = std::unordered_set<DocumentId>;
|
using Ids = std::vector<DocumentId>;
|
||||||
|
|
||||||
void requestList();
|
void requestList();
|
||||||
void applyUpdate();
|
void applyUpdate();
|
||||||
|
@ -36,6 +36,7 @@ public:
|
||||||
[[nodiscard]] const Ids &list() const;
|
[[nodiscard]] const Ids &list() const;
|
||||||
[[nodiscard]] rpl::producer<> listUpdates() const;
|
[[nodiscard]] rpl::producer<> listUpdates() const;
|
||||||
[[nodiscard]] rpl::producer<QString> uploadFails() const;
|
[[nodiscard]] rpl::producer<QString> uploadFails() const;
|
||||||
|
[[nodiscard]] rpl::producer<DocumentId> uploadDones() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct UploadedData {
|
struct UploadedData {
|
||||||
|
@ -49,6 +50,7 @@ private:
|
||||||
|
|
||||||
base::flat_map<FullMsgId, UploadedData> _uploads;
|
base::flat_map<FullMsgId, UploadedData> _uploads;
|
||||||
rpl::event_stream<QString> _uploadFails;
|
rpl::event_stream<QString> _uploadFails;
|
||||||
|
rpl::event_stream<DocumentId> _uploadDones;
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
uint64 hash = 0;
|
uint64 hash = 0;
|
||||||
|
|
|
@ -43,9 +43,34 @@ constexpr auto kNoSoundValue = -2;
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
QString ExtractRingtoneName(not_null<DocumentData*> document) {
|
||||||
|
if (document->isNull()) {
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
|
const auto name = document->filename();
|
||||||
|
if (!name.isEmpty()) {
|
||||||
|
const auto extension = Data::FileExtension(name);
|
||||||
|
if (extension.isEmpty()) {
|
||||||
|
return name;
|
||||||
|
} else if (name.size() > extension.size() + 1) {
|
||||||
|
return name.mid(0, name.size() - extension.size() - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const auto date = langDateTime(
|
||||||
|
base::unixtime::parse(document->date));
|
||||||
|
const auto base = document->isVoiceMessage()
|
||||||
|
? (tr::lng_in_dlg_audio(tr::now) + ' ')
|
||||||
|
: document->isAudioFile()
|
||||||
|
? (tr::lng_in_dlg_audio_file(tr::now) + ' ')
|
||||||
|
: QString();
|
||||||
|
return base + date;
|
||||||
|
}
|
||||||
|
|
||||||
void RingtonesBox(
|
void RingtonesBox(
|
||||||
not_null<Ui::GenericBox*> box,
|
not_null<Ui::GenericBox*> box,
|
||||||
not_null<PeerData*> peer) {
|
not_null<Main::Session*> session,
|
||||||
|
Data::NotifySound selected,
|
||||||
|
Fn<void(Data::NotifySound)> save) {
|
||||||
box->setTitle(tr::lng_ringtones_box_title());
|
box->setTitle(tr::lng_ringtones_box_title());
|
||||||
|
|
||||||
const auto container = box->verticalLayout();
|
const auto container = box->verticalLayout();
|
||||||
|
@ -56,13 +81,15 @@ void RingtonesBox(
|
||||||
struct State {
|
struct State {
|
||||||
std::shared_ptr<Ui::RadiobuttonGroup> group;
|
std::shared_ptr<Ui::RadiobuttonGroup> group;
|
||||||
std::vector<DocumentId> documentIds;
|
std::vector<DocumentId> documentIds;
|
||||||
|
Data::NotifySound chosen;
|
||||||
base::unique_qptr<Ui::PopupMenu> menu;
|
base::unique_qptr<Ui::PopupMenu> menu;
|
||||||
QPointer<Ui::Radiobutton> defaultButton;
|
QPointer<Ui::Radiobutton> defaultButton;
|
||||||
QPointer<Ui::Radiobutton> chosenButton;
|
QPointer<Ui::Radiobutton> chosenButton;
|
||||||
std::vector<QPointer<Ui::Radiobutton>> buttons;
|
std::vector<QPointer<Ui::Radiobutton>> buttons;
|
||||||
};
|
};
|
||||||
const auto state = container->lifetime().make_state<State>(State{
|
const auto state = container->lifetime().make_state<State>(State{
|
||||||
std::make_shared<Ui::RadiobuttonGroup>(),
|
.group = std::make_shared<Ui::RadiobuttonGroup>(),
|
||||||
|
.chosen = selected,
|
||||||
});
|
});
|
||||||
|
|
||||||
const auto addToGroup = [=](
|
const auto addToGroup = [=](
|
||||||
|
@ -73,15 +100,12 @@ void RingtonesBox(
|
||||||
if (chosen) {
|
if (chosen) {
|
||||||
state->group->setValue(value);
|
state->group->setValue(value);
|
||||||
}
|
}
|
||||||
const auto extension = Data::FileExtension(text);
|
|
||||||
const auto button = verticalLayout->add(
|
const auto button = verticalLayout->add(
|
||||||
object_ptr<Ui::Radiobutton>(
|
object_ptr<Ui::Radiobutton>(
|
||||||
verticalLayout,
|
verticalLayout,
|
||||||
state->group,
|
state->group,
|
||||||
value,
|
value,
|
||||||
extension.isEmpty()
|
text,
|
||||||
? text
|
|
||||||
: text.mid(0, text.length() - extension.length() - 1),
|
|
||||||
st::defaultCheckbox),
|
st::defaultCheckbox),
|
||||||
padding);
|
padding);
|
||||||
if (chosen) {
|
if (chosen) {
|
||||||
|
@ -105,7 +129,7 @@ void RingtonesBox(
|
||||||
st::popupMenuWithIcons);
|
st::popupMenuWithIcons);
|
||||||
auto callback = [=] {
|
auto callback = [=] {
|
||||||
const auto id = state->documentIds[value];
|
const auto id = state->documentIds[value];
|
||||||
peer->session().api().ringtones().remove(id);
|
session->api().ringtones().remove(id);
|
||||||
};
|
};
|
||||||
state->menu->addAction(
|
state->menu->addAction(
|
||||||
tr::lng_box_delete(tr::now),
|
tr::lng_box_delete(tr::now),
|
||||||
|
@ -116,7 +140,7 @@ void RingtonesBox(
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
peer->session().api().ringtones().uploadFails(
|
session->api().ringtones().uploadFails(
|
||||||
) | rpl::start_with_next([=](const QString &error) {
|
) | rpl::start_with_next([=](const QString &error) {
|
||||||
if ((error == u"RINGTONE_DURATION_TOO_LONG"_q)
|
if ((error == u"RINGTONE_DURATION_TOO_LONG"_q)
|
||||||
|| (error == u"RINGTONE_SIZE_TOO_BIG"_q)) {
|
|| (error == u"RINGTONE_SIZE_TOO_BIG"_q)) {
|
||||||
|
@ -132,7 +156,7 @@ void RingtonesBox(
|
||||||
container,
|
container,
|
||||||
tr::lng_ringtones_box_cloud_subtitle());
|
tr::lng_ringtones_box_cloud_subtitle());
|
||||||
|
|
||||||
const auto noSound = peer->owner().notifySettings().sound(peer).none;
|
const auto noSound = selected.none;
|
||||||
addToGroup(
|
addToGroup(
|
||||||
container,
|
container,
|
||||||
kDefaultValue,
|
kDefaultValue,
|
||||||
|
@ -154,23 +178,10 @@ void RingtonesBox(
|
||||||
delete custom->widgetAt(0);
|
delete custom->widgetAt(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto checkedId = peer->owner().notifySettings().sound(peer);
|
for (const auto &id : session->api().ringtones().list()) {
|
||||||
for (const auto &id : peer->session().api().ringtones().list()) {
|
const auto chosen = (state->chosen.id && state->chosen.id == id);
|
||||||
const auto chosen = (checkedId.id && checkedId.id == id);
|
const auto document = session->data().document(id);
|
||||||
const auto document = peer->session().data().document(id);
|
const auto text = ExtractRingtoneName(document);
|
||||||
const auto text = [&] {
|
|
||||||
if (!document->filename().isEmpty()) {
|
|
||||||
return document->filename();
|
|
||||||
}
|
|
||||||
const auto date = langDateTime(
|
|
||||||
base::unixtime::parse(document->date));
|
|
||||||
const auto base = document->isVoiceMessage()
|
|
||||||
? (tr::lng_in_dlg_audio(tr::now) + ' ')
|
|
||||||
: document->isAudioFile()
|
|
||||||
? (tr::lng_in_dlg_audio_file(tr::now) + ' ')
|
|
||||||
: QString();
|
|
||||||
return base + date;
|
|
||||||
}();
|
|
||||||
addToGroup(custom, value++, text, chosen);
|
addToGroup(custom, value++, text, chosen);
|
||||||
state->documentIds.push_back(id);
|
state->documentIds.push_back(id);
|
||||||
}
|
}
|
||||||
|
@ -182,10 +193,16 @@ void RingtonesBox(
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
peer->session().api().ringtones().listUpdates(
|
session->api().ringtones().listUpdates(
|
||||||
) | rpl::start_with_next(rebuild, container->lifetime());
|
) | rpl::start_with_next(rebuild, container->lifetime());
|
||||||
|
|
||||||
peer->session().api().ringtones().requestList();
|
session->api().ringtones().uploadDones(
|
||||||
|
) | rpl::start_with_next([=](DocumentId id) {
|
||||||
|
state->chosen = Data::NotifySound{ .id = id };
|
||||||
|
rebuild();
|
||||||
|
}, container->lifetime());
|
||||||
|
|
||||||
|
session->api().ringtones().requestList();
|
||||||
rebuild();
|
rebuild();
|
||||||
|
|
||||||
const auto upload = box->addRow(
|
const auto upload = box->addRow(
|
||||||
|
@ -221,7 +238,7 @@ void RingtonesBox(
|
||||||
name = "audio";
|
name = "audio";
|
||||||
}
|
}
|
||||||
|
|
||||||
peer->session().api().ringtones().upload(name, mime, content);
|
session->api().ringtones().upload(name, mime, content);
|
||||||
};
|
};
|
||||||
FileDialog::GetOpenPath(
|
FileDialog::GetOpenPath(
|
||||||
box.get(),
|
box.get(),
|
||||||
|
@ -243,9 +260,18 @@ void RingtonesBox(
|
||||||
? Data::NotifySound()
|
? Data::NotifySound()
|
||||||
: (value == kNoSoundValue)
|
: (value == kNoSoundValue)
|
||||||
? Data::NotifySound{ .none = true }
|
? Data::NotifySound{ .none = true }
|
||||||
: Data::NotifySound{ .id = state->documentIds[value] };
|
: Data::NotifySound{ .id = state->documentIds[value] };
|
||||||
peer->owner().notifySettings().update(peer, {}, {}, sound);
|
save(sound);
|
||||||
box->closeBox();
|
box->closeBox();
|
||||||
});
|
});
|
||||||
box->addButton(tr::lng_cancel(), [=] { box->closeBox(); });
|
box->addButton(tr::lng_cancel(), [=] { box->closeBox(); });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PeerRingtonesBox(
|
||||||
|
not_null<Ui::GenericBox*> box,
|
||||||
|
not_null<PeerData*> peer) {
|
||||||
|
const auto now = peer->owner().notifySettings().sound(peer);
|
||||||
|
RingtonesBox(box, &peer->session(), now, [=](Data::NotifySound sound) {
|
||||||
|
peer->owner().notifySettings().update(peer, {}, {}, sound);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
|
@ -11,6 +11,26 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
|
|
||||||
class PeerData;
|
class PeerData;
|
||||||
|
|
||||||
|
namespace Data {
|
||||||
|
struct NotifySound;
|
||||||
|
} // namespace Data
|
||||||
|
|
||||||
|
namespace Main {
|
||||||
|
class Session;
|
||||||
|
} // namespace Main
|
||||||
|
|
||||||
|
namespace Ui {
|
||||||
|
class GenericBox;
|
||||||
|
} // namespace Ui
|
||||||
|
|
||||||
|
[[nodiscard]] QString ExtractRingtoneName(not_null<DocumentData*> document);
|
||||||
|
|
||||||
void RingtonesBox(
|
void RingtonesBox(
|
||||||
|
not_null<Ui::GenericBox*> box,
|
||||||
|
not_null<Main::Session*> session,
|
||||||
|
Data::NotifySound selected,
|
||||||
|
Fn<void(Data::NotifySound)> save);
|
||||||
|
|
||||||
|
void PeerRingtonesBox(
|
||||||
not_null<Ui::GenericBox*> box,
|
not_null<Ui::GenericBox*> box,
|
||||||
not_null<PeerData*> peer);
|
not_null<PeerData*> peer);
|
||||||
|
|
Loading…
Add table
Reference in a new issue