<commit_before>#include <pebble.h>
#include "news.h"
#include "../libs/pebble-assist.h"
#include "../common.h"

static headline_t headline;
static char title[64];
static char summary[2048];

static void request_data();
static void back_single_click_handler(ClickRecognizerRef recognizer, void *context);
static void click_config_provider(void *context);
static void window_refresh();

static Window *window;
static ScrollLayer *scroll_layer;
static TextLayer *title_layer;
static TextLayer *summary_layer;

void news_init(headline_t h) {
	headline = h;
	strncpy(title, "Loading story...", sizeof(title) - 1);

	window = window_create();

	scroll_layer = scroll_layer_create(layer_get_bounds(window_get_root_layer(window)));
	scroll_layer_set_click_config_onto_window(scroll_layer, window);
	scroll_layer_set_callbacks(scroll_layer, (ScrollLayerCallbacks) {
		.click_config_provider = click_config_provider,
	});

	window_refresh();

	layer_add_child(window_get_root_layer(window), scroll_layer_get_layer(scroll_layer));

	window_stack_push(window, true);

	request_data();
}

void news_destroy(void) {
	text_layer_destroy_safe(title_layer);
	text_layer_destroy_safe(summary_layer);
	scroll_layer_destroy_safe(scroll_layer);
	window_destroy_safe(window);
}

void news_in_received_handler(DictionaryIterator *iter) {
	Tuple *title_tuple = dict_find(iter, KEY_TITLE);
	Tuple *summary_tuple = dict_find(iter, KEY_SUMMARY);

	if (summary_tuple) {
		if (title_tuple) {
			strncpy(title, title_tuple->value->cstring, sizeof(title) - 1);
			window_refresh();
		} else {
			strcat(summary, summary_tuple->value->cstring);
		}
	}
}

void news_out_sent_handler(DictionaryIterator *sent) {
}

void news_out_failed_handler(DictionaryIterator *failed, AppMessageResult reason) {
	strncpy(title, "Phone unreachable!", sizeof(title) - 1);
	window_refresh();
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //

static void request_data() {
	summary[0] = '\0';
	Tuplet summary_tuple = TupletInteger(KEY_SUMMARY, 1);
	Tuplet index_tuple = TupletInteger(KEY_INDEX, headline.index);
	DictionaryIterator *iter;
	app_message_outbox_begin(&iter);
	if (iter == NULL)
		return;
	dict_write_tuplet(iter, &summary_tuple);
	dict_write_tuplet(iter, &index_tuple);
	dict_write_end(iter);
	app_message_outbox_send();<commit_msg>Request story before loading window so summary is cleared.<commit_after>#include <pebble.h>
#include "news.h"
#include "../libs/pebble-assist.h"
#include "../common.h"

static headline_t headline;
static char title[64];
static char summary[2048];

static void request_data();
static void back_single_click_handler(ClickRecognizerRef recognizer, void *context);
static void click_config_provider(void *context);
static void window_refresh();

static Window *window;
static ScrollLayer *scroll_layer;
static TextLayer *title_layer;
static TextLayer *summary_layer;

void news_init(headline_t h) {
	headline = h;

	request_data();

	window = window_create();

	scroll_layer = scroll_layer_create(layer_get_bounds(window_get_root_layer(window)));
	scroll_layer_set_click_config_onto_window(scroll_layer, window);
	scroll_layer_set_callbacks(scroll_layer, (ScrollLayerCallbacks) {
		.click_config_provider = click_config_provider,
	});

	window_refresh();

	layer_add_child(window_get_root_layer(window), scroll_layer_get_layer(scroll_layer));

	window_stack_push(window, true);
}

void news_destroy(void) {
	text_layer_destroy_safe(title_layer);
	text_layer_destroy_safe(summary_layer);
	scroll_layer_destroy_safe(scroll_layer);
	window_destroy_safe(window);
}

void news_in_received_handler(DictionaryIterator *iter) {
	Tuple *title_tuple = dict_find(iter, KEY_TITLE);
	Tuple *summary_tuple = dict_find(iter, KEY_SUMMARY);

	if (summary_tuple) {
		if (title_tuple) {
			strncpy(title, title_tuple->value->cstring, sizeof(title) - 1);
			window_refresh();
		} else {
			strcat(summary, summary_tuple->value->cstring);
		}
	}
}

void news_out_sent_handler(DictionaryIterator *sent) {
}

void news_out_failed_handler(DictionaryIterator *failed, AppMessageResult reason) {
	strncpy(title, "Phone unreachable!", sizeof(title) - 1);
	window_refresh();
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //

static void request_data() {
	strncpy(title, "Loading story...", sizeof(title) - 1);
	summary[0] = '\0';
	Tuplet summary_tuple = TupletInteger(KEY_SUMMARY, 1);
	Tuplet index_tuple = TupletInteger(KEY_INDEX, headline.index);
	DictionaryIterator *iter;
	app_message_outbox_begin(&iter);
	if (iter == NULL)
		return;
	dict_write_tuplet(iter, &summary_tuple);
	dict_write_tuplet(iter, &index_tuple);
	dict_write_end(iter);
	app_message_outbox_send();
{'fraction_non_alphanumeric': 0.12827058072750477, 'fraction_numerical': 0.005105296745373325, 'mean_word_length': 5.755747126436781, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3261769', 'n_tokens_mistral': 1697, 'n_tokens_neox': 1622, 'n_words': 287}