InfiniTime.git

ref: 0ce98c7ac7ba66acaf504be9bb042796e12f2733

src/components/ble/NotificationManager.cpp


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "NotificationManager.h"
#include <cstring>
#include <algorithm>

using namespace Pinetime::Controllers;

constexpr uint8_t NotificationManager::MessageSize;

void NotificationManager::Push(NotificationManager::Notification&& notif) {
  notif.id = GetNextId();
  notif.valid = true;
  notifications[writeIndex] = std::move(notif);
  writeIndex = (writeIndex + 1 < TotalNbNotifications) ? writeIndex + 1 : 0;
  if (!empty)
    readIndex = (readIndex + 1 < TotalNbNotifications) ? readIndex + 1 : 0;
  else
    empty = false;

  newNotification = true;
}

NotificationManager::Notification NotificationManager::GetLastNotification() {
  NotificationManager::Notification notification = notifications[readIndex];
  notification.index = 1;
  return notification;
}

NotificationManager::Notification::Id NotificationManager::GetNextId() {
  return nextId++;
}

NotificationManager::Notification NotificationManager::GetNext(NotificationManager::Notification::Id id) {
  auto currentIterator = std::find_if(notifications.begin(), notifications.end(), [id](const Notification& n) {
    return n.valid && n.id == id;
  });
  if (currentIterator == notifications.end() || currentIterator->id != id)
    return Notification {};

  auto& lastNotification = notifications[readIndex];

  NotificationManager::Notification result;

  if (currentIterator == (notifications.end() - 1))
    result = *(notifications.begin());
  else
    result = *(currentIterator + 1);

  if (result.id <= id)
    return {};

  result.index = (lastNotification.id - result.id) + 1;
  return result;
}

NotificationManager::Notification NotificationManager::GetPrevious(NotificationManager::Notification::Id id) {
  auto currentIterator = std::find_if(notifications.begin(), notifications.end(), [id](const Notification& n) {
    return n.valid && n.id == id;
  });
  if (currentIterator == notifications.end() || currentIterator->id != id)
    return Notification {};

  auto& lastNotification = notifications[readIndex];

  NotificationManager::Notification result;

  if (currentIterator == notifications.begin())
    result = *(notifications.end() - 1);
  else
    result = *(currentIterator - 1);

  if (result.id >= id)
    return {};

  result.index = (lastNotification.id - result.id) + 1;
  return result;
}

bool NotificationManager::AreNewNotificationsAvailable() {
  return newNotification;
}

bool NotificationManager::IsVibrationEnabled() {
  return vibrationEnabled;
}

void NotificationManager::ToggleVibrations() {
  vibrationEnabled = !vibrationEnabled;
}

bool NotificationManager::ClearNewNotificationFlag() {
  return newNotification.exchange(false);
}

size_t NotificationManager::NbNotifications() const {
  return std::count_if(notifications.begin(), notifications.end(), [](const Notification& n) {
    return n.valid;
  });
}

const char* NotificationManager::Notification::Message() const {
  const char* itField = std::find(message.begin(), message.begin() + size - 1, '\0');
  if (itField != message.begin() + size - 1) {
    const char* ptr = (itField) + 1;
    return ptr;
  }
  return const_cast<char*>(message.data());
}

const char* NotificationManager::Notification::Title() const {
  const char* itField = std::find(message.begin(), message.begin() + size - 1, '\0');
  if (itField != message.begin() + size - 1) {
    return message.data();
  }
  return {};
}