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

ash / quick_insert / model / quick_insert_emoji_history_model.cc [blame]

// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/quick_insert/model/quick_insert_emoji_history_model.h"

#include <string>
#include <string_view>
#include <vector>

#include "ash/constants/ash_pref_names.h"
#include "base/check_deref.h"
#include "base/json/values_util.h"
#include "base/time/time.h"
#include "base/values.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "ui/base/emoji/emoji_panel_helper.h"

namespace ash {
namespace {

constexpr int kMaxRecentEmoji = 20;

constexpr std::string_view kEmojiHistoryValueFieldName = "text";
constexpr std::string_view kEmojiHistoryTimestampFieldName = "timestamp";

std::string ConvertEmojiCategoryToString(ui::EmojiPickerCategory category) {
  switch (category) {
    case ui::EmojiPickerCategory::kEmojis:
      return "emoji";
    case ui::EmojiPickerCategory::kSymbols:
      return "symbol";
    case ui::EmojiPickerCategory::kEmoticons:
      return "emoticon";
    case ui::EmojiPickerCategory::kGifs:
      return "gif";
  }
}

}  // namespace

bool QuickInsertEmojiHistoryModel::EmojiHistoryItem::operator==(
    const QuickInsertEmojiHistoryModel::EmojiHistoryItem&) const = default;

QuickInsertEmojiHistoryModel::QuickInsertEmojiHistoryModel(PrefService* prefs,
                                                           base::Clock* clock)
    : prefs_(CHECK_DEREF(prefs)), clock_(clock) {}

std::vector<QuickInsertEmojiHistoryModel::EmojiHistoryItem>
QuickInsertEmojiHistoryModel::GetRecentEmojis(
    ui::EmojiPickerCategory category) const {
  const base::Value::List* history =
      prefs_->GetDict(prefs::kEmojiPickerHistory)
          .FindList(ConvertEmojiCategoryToString(category));
  if (history == nullptr) {
    return {};
  }
  std::vector<EmojiHistoryItem> results;
  for (const base::Value& it : *history) {
    const base::Value::Dict* value_dict = it.GetIfDict();
    if (value_dict == nullptr) {
      continue;
    }
    const std::string* text =
        value_dict->FindString(kEmojiHistoryValueFieldName);
    std::optional<base::Time> timestamp =
        base::ValueToTime(value_dict->Find(kEmojiHistoryTimestampFieldName));
    if (text != nullptr) {
      results.push_back({.text = *text,
                         .category = category,
                         .timestamp = timestamp == std::nullopt
                                          ? base::Time::UnixEpoch()
                                          : *timestamp});
    }
  }
  return results;
}

void QuickInsertEmojiHistoryModel::UpdateRecentEmoji(
    ui::EmojiPickerCategory category,
    std::string_view latest_emoji) {
  std::vector<EmojiHistoryItem> history = GetRecentEmojis(category);
  base::Value::List history_value;
  history_value.Append(base::Value::Dict()
                           .Set(kEmojiHistoryValueFieldName, latest_emoji)
                           .Set(kEmojiHistoryTimestampFieldName,
                                base::TimeToValue(clock_->Now())));
  for (const EmojiHistoryItem& item : history) {
    if (item.text == latest_emoji) {
      continue;
    }
    history_value.Append(base::Value::Dict()
                             .Set(kEmojiHistoryValueFieldName, item.text)
                             .Set(kEmojiHistoryTimestampFieldName,
                                  base::TimeToValue(item.timestamp)));
    if (history_value.size() == kMaxRecentEmoji) {
      break;
    }
  }

  ScopedDictPrefUpdate update(&prefs_.get(), prefs::kEmojiPickerHistory);
  update->Set(ConvertEmojiCategoryToString(category), std::move(history_value));
}

}  // namespace ash