Skip to content

Commit

Permalink
use lazy loading
Browse files Browse the repository at this point in the history
  • Loading branch information
halfmexican committed Aug 19, 2024
1 parent afb9102 commit 3884064
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
1 change: 1 addition & 0 deletions src/combined_emoji.vala
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ namespace Mingle {

this.clicked.connect (() => {
this.copy_image_to_clipboard ();
message ("emoji: %s\n", combined_emoji.alt);
this.copied ();
});
}
Expand Down
47 changes: 24 additions & 23 deletions src/emoji_data_manager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,13 @@ namespace Mingle {
public class EmojiDataManager {
private Json.Object root_object;
private Json.Array supported_emojis;
public HashSet<string> added_combinations = new HashSet<string> ();
public HashSet<string> added_combinations;
private Gee.HashMap<string, EmojiData?> emoji_data_map;

public EmojiDataManager () {
supported_emojis = populate_supported_emojis_array ();
emoji_data_map = new Gee.HashMap<string, EmojiData?> ();
added_combinations = new HashSet<string> ();
start_emoji_data_map_thread ();
}

private void start_emoji_data_map_thread () {
// Starts a separate thread if possible to populate this.combinations_map
if (!Thread.supported ()) {
warning ("Threads are not supported!\n");
emoji_data_map = create_emoji_data_map ();
return;
}

try {
message ("data thread started\n");
Thread<Gee.HashMap<string, EmojiData?>> thread = new Thread<Gee.HashMap<string, EmojiData?>>.try ("combinations_map_thread", create_emoji_data_map);

emoji_data_map = thread.join ();
message ("data thread ended\n");
} catch (Error e) {
error ("Error: %s\n", e.message);
}
}

private Json.Array populate_supported_emojis_array () {
Expand Down Expand Up @@ -146,7 +126,7 @@ namespace Mingle {
unshuffled_combinations.add_element (combination_array.get_element (i));
}
}

// Shuffle the unshuffled_combinations array
Json.Array shuffled_combinations = new Json.Array ();
GLib.Rand rng = new GLib.Rand ();
Expand Down Expand Up @@ -214,10 +194,31 @@ namespace Mingle {
}

public EmojiData ? get_emoji_data (string emoji_codepoint) {
if (!emoji_data_map.has_key (emoji_codepoint)) {
emoji_data_map[emoji_codepoint] = create_emoji_data (emoji_codepoint);
}
return emoji_data_map[emoji_codepoint];
}

public EmojiCombination? get_combination (string left_emoji_codepoint, string right_emoji_codepoint) {
private EmojiData ? create_emoji_data (string emoji_codepoint) {
Json.Object data_object = root_object.get_object_member ("data");
Json.Object? emoji_object = data_object.get_object_member (emoji_codepoint);

if (emoji_object == null) {
return null;
}

EmojiData emoji_data = EmojiData ();
emoji_data.alt = emoji_object.get_string_member ("alt");
emoji_data.keywords = emoji_object.get_array_member ("keywords");
emoji_data.emoji_codepoint = emoji_codepoint;
emoji_data.gboard_order = (int) emoji_object.get_int_member ("gBoardOrder");
emoji_data.combinations = populate_combinations (emoji_object.get_object_member ("combinations"));

return emoji_data;
}

public EmojiCombination ? get_combination (string left_emoji_codepoint, string right_emoji_codepoint) {
var emoji_data = get_emoji_data (left_emoji_codepoint);
if (emoji_data != null && emoji_data.combinations.has_key (right_emoji_codepoint)) {
foreach (var combination in emoji_data.combinations[right_emoji_codepoint]) {
Expand Down
2 changes: 1 addition & 1 deletion src/window.vala
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ namespace Mingle {
is_loading = false; // Reset the loading state
return;
} else if (batch_offset > 0) {
create_and_show_toast ("Loading More Combinations…", 2);
create_and_show_toast ("Loading More Combinations…", 4);
}

foreach (Json.Object combination_object in batch) {
Expand Down

0 comments on commit 3884064

Please sign in to comment.