|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "release_notes.h" |
| 18 | +#include <pebble.h> |
| 19 | + |
| 20 | +#include "util/formatted_text_layer.h" |
| 21 | +#include "util/logging.h" |
| 22 | +#include "util/memory/malloc.h" |
| 23 | +#include "util/memory/sdk.h" |
| 24 | +#include "version/version.h" |
| 25 | + |
| 26 | +typedef struct { |
| 27 | + Window *window; |
| 28 | + ScrollLayer *scroll_layer; |
| 29 | + FormattedTextLayer *text_layer; |
| 30 | + char* text; |
| 31 | +} ReleaseNotesWindowData; |
| 32 | + |
| 33 | +static void prv_load(Window *window); |
| 34 | +static void prv_unload(Window *window); |
| 35 | +// static void prv_click_config_provider(void *context); |
| 36 | +static char* prv_create_release_notes(); |
| 37 | + |
| 38 | +void prv_release_notes_push() { |
| 39 | + Window *window = bwindow_create(); |
| 40 | + ReleaseNotesWindowData *data = bmalloc(sizeof(ReleaseNotesWindowData)); |
| 41 | + data->window = window; |
| 42 | + window_set_user_data(window, data); |
| 43 | + window_set_window_handlers(window, (WindowHandlers) { |
| 44 | + .load = prv_load, |
| 45 | + .unload = prv_unload, |
| 46 | + }); |
| 47 | + window_stack_push(window, true); |
| 48 | +} |
| 49 | + |
| 50 | +void release_notes_maybe_push() { |
| 51 | + if (version_is_updated() && !version_is_first_launch()) { |
| 52 | + BOBBY_LOG(APP_LOG_LEVEL_INFO, "Showing release notes"); |
| 53 | + prv_release_notes_push(); |
| 54 | + } else { |
| 55 | + BOBBY_LOG(APP_LOG_LEVEL_INFO, "Not showing release notes. Is updated: %d, Is first launch: %d", version_is_updated(), version_is_first_launch()); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +static char* prv_create_release_notes() { |
| 60 | + ResHandle handle = resource_get_handle(RESOURCE_ID_CHANGELOG_1_4); |
| 61 | + size_t size = resource_size(handle) + 1; |
| 62 | + char* text = bmalloc(size); |
| 63 | + text[size - 1] = '\0'; |
| 64 | + resource_load(handle, (uint8_t*)text, size); |
| 65 | + return text; |
| 66 | +} |
| 67 | + |
| 68 | +static void prv_load(Window *window) { |
| 69 | + GRect bounds = layer_get_bounds(window_get_root_layer(window)); |
| 70 | + ReleaseNotesWindowData *data = window_get_user_data(window); |
| 71 | + data->text = prv_create_release_notes(); |
| 72 | + data->text_layer = formatted_text_layer_create(GRect(5, 0, bounds.size.w - 10, 5000)); |
| 73 | + formatted_text_layer_set_text(data->text_layer, data->text); |
| 74 | + GSize content_size = formatted_text_layer_get_content_size(data->text_layer); |
| 75 | + layer_set_frame(data->text_layer, GRect(5, 0, bounds.size.w - 10, content_size.h + 10)); |
| 76 | + data->scroll_layer = scroll_layer_create(GRect(0, 0, bounds.size.w, bounds.size.h)); |
| 77 | + scroll_layer_set_shadow_hidden(data->scroll_layer, true); |
| 78 | + scroll_layer_set_content_size(data->scroll_layer, layer_get_frame(data->text_layer).size); |
| 79 | + scroll_layer_set_click_config_onto_window(data->scroll_layer, window); |
| 80 | + scroll_layer_add_child(data->scroll_layer, data->text_layer); |
| 81 | + layer_add_child(window_get_root_layer(window), scroll_layer_get_layer(data->scroll_layer)); |
| 82 | + scroll_layer_set_click_config_onto_window(data->scroll_layer, window); |
| 83 | +} |
| 84 | + |
| 85 | +static void prv_unload(Window *window) { |
| 86 | + ReleaseNotesWindowData *data = window_get_user_data(window); |
| 87 | + free(data->text); |
| 88 | + formatted_text_layer_destroy(data->text_layer); |
| 89 | + scroll_layer_destroy(data->scroll_layer); |
| 90 | + free(data); |
| 91 | + window_destroy(window); |
| 92 | +} |
0 commit comments