Skip to content

Commit

Permalink
feat: history migration from old format!
Browse files Browse the repository at this point in the history
version: 1.7.0 (1000015) => 1.7.1 (1000016)
  • Loading branch information
awaLiny2333 committed Dec 14, 2024
1 parent 524714b commit 5aca2c6
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 52 deletions.
4 changes: 2 additions & 2 deletions AppScope/app.json5
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"app": {
"bundleName": "com.next.liny.linysbrowserNEXT",
"vendor": "example",
"versionCode": 1000015,
"versionName": "1.7.0",
"versionCode": 1000016,
"versionName": "1.7.1",
"icon": "$media:app_icon",
"label": "$string:app_name"
}
Expand Down
Binary file modified build_auto/HMOS-home-default-unsigned.hap
Binary file not shown.
Binary file modified build_auto/OHOS-home-default-unsigned.hap
Binary file not shown.
Binary file modified build_auto/entry.hap
Binary file not shown.
1 change: 1 addition & 0 deletions home/src/main/ets/dialogs/woofHistory.ets
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ struct woofHistory {
.animation(animation_default())
}
}
.reverse(true)
.width("100%")
} // Main history list
.scrollable(ScrollDirection.Vertical)
Expand Down
6 changes: 4 additions & 2 deletions home/src/main/ets/dialogs/woofUpdateHistory.ets
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ struct woofUpdateHistory {
@State texts: string[] = [
'v1.7.0-beta',
'A KEY release of a KEY update, bringing better History system and a few experience optimizations into Browser Cat!\n' +
'However, the previous History records and last status of opened tabs are not inherited. So sorry for that...',
'This version is trying to solve some performance issue of parts of this project, but this requires some deep level modification.\n' +
'Therefore, the previous History records and last status of opened tabs are not inherited. So sorry for that...',
'一个关键的发行版更新,将网页深色模式、更新记录、致谢和一些体验优化带入冲浪喵~\n' +
'不过,以前的历史浏览记录和上一次打开的标签页并没有得到继承……对不起!!!\n>︿<',
'这个版本正在尝试优化一些组件的性能问题,但是这需要一些深层的修改。\n' +
'所以,以前的历史浏览记录和上一次打开的标签页并没有得到继承……对不起!!!\n>︿<',

'v1.6.4-beta',
'A decent release of update, bringing Web Dark Mode, Update History, Credits and a few experience optimizations into Browser Cat!',
Expand Down
130 changes: 82 additions & 48 deletions home/src/main/ets/hosts/bunch_of_history.ets
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sandbox_open_sync, sandbox_save } from '../utils/storage_tools';
import { sandbox_open_sync, sandbox_save, sandbox_unlink } from '../utils/storage_tools';
import { fileIo as fs } from '@kit.CoreFileKit';

export class history_record {
Expand Down Expand Up @@ -37,7 +37,26 @@ export class bunch_of_history {
if (plain_history_max_length) {
this.plain_history_default_max_length = plain_history_max_length;
}
// Load today
this.open_month_from_disk(this.current_year, this.current_month, true);

// Compatibility path for older BrowserCats
this.patch_old_history_files();
}

// PATCHES

/**
* Executed when an old format history.txt is found.
* This migrates old histories to new format and deletes the old file.
* */
patch_old_history_files() {
let old_history = sandbox_open_sync('history.txt');
if (old_history != 'undefined') {
console.log('found old history' + old_history.toString())
this.import_string(old_history);
sandbox_unlink('history.txt');
}
}

// Settings
Expand Down Expand Up @@ -204,53 +223,6 @@ export class bunch_of_history {

// External interaction

/**
* Export history records in a specific plain text format.
* @returns "\n" connected string of history records in the format of:
* @example 'bing\nbing.com\n127771721'
* */
private export_string() {
let export_list: string[] = []
for (let index = 0; index < this.history_this_month.length; index++) {
let history_item: history_record = this.history_this_month[index];
export_list.push(history_item.label);
export_list.push(history_item.link);
export_list.push(history_item.accessed_time.toString());
}
return export_list.join("\n");
}

/**
* Import history records in a specific plain text format.
*
* In default overwrites whatever was in this history_list.
* @param imp The string in the correct format:
* @example 'bing\nbing.com\n127771721'
* */
private import_string(imp: string) {
this.history_this_month = [];

if (imp.split("\n").length % 3 > 0) {
// Incorrect format, log an error.
// Though this is not very likely to happen so often
// since all the txt files are generated by a fixed algorithm.
console.error("[ERROR][Meow][bunch_of_history] Import Error: not 3*n length: " + imp)
return;
}

this.history_this_month = [];
// Clear
let import_list: string[] = imp.split("\n");
for (let index = 0; index < import_list.length; index += 3) {
if (import_list[index] != "") {
let new_history_record =
new history_record(import_list[index], import_list[index+1], Number.parseInt(import_list[index+2]));
this.add_history(new_history_record, false);
}
}
console.log("[Meow][bunch_of_history] Import history success, length: " + this.history_this_month.length.toString())
}

/**
* Check the history file structure on disk.
* After executing this, there will be ENSURED a history/history_year_month.txt existing for use.
Expand Down Expand Up @@ -349,4 +321,66 @@ export class bunch_of_history {
// console.log(result.toString())
return result;
}

/**
* Export history records in a specific plain text format.
* @returns "\n" connected string of history records in the format of:
* @example 'bing\nbing.com\n127771721'
* */
private export_string() {
let export_list: string[] = []
for (let index = 0; index < this.history_this_month.length; index++) {
let history_item: history_record = this.history_this_month[index];
export_list.push(history_item.label);
export_list.push(history_item.link);
export_list.push(history_item.accessed_time.toString());
}
return export_list.join("\n");
}

/**
* Import history records in a specific plain text format.
*
* In default overwrites whatever was in this history_list.
* @param imp The string in the correct format:
* @example 'bing\nbing.com\n127771721'
* */
private import_string(imp: string) {
this.history_this_month = [];

if (imp.split("\n").length % 3 > 0) {
// Incorrect format, log an error.
// Though this is not very likely to happen so often
// since all the txt files are generated by a fixed algorithm.
console.error("[ERROR][Meow][bunch_of_history] Import Error: not 3*n length: " + imp)
return;
}

this.history_this_month = [];
// Clear
let import_list: string[] = imp.split("\n");
let last_month = [0, 0];
for (let index = 0; index < import_list.length; index += 3) {
if (import_list[index] != "") {
let access_time = Number.parseInt(import_list[index+2]);
let access_Date = new Date(access_time);
let new_month = [new Date(access_time).getFullYear(), new Date(access_time).getMonth() + 1];

if ((last_month[0] != new_month[0]) || (last_month[1] != new_month[1])) {
// if changed month, then save this file
if ((last_month[0] != 0) && (last_month[1] != 0)) {
console.log('[Meow][bunch_of_history] Import history save month to disk: ' + last_month.toString());
this.save_month_to_disk();
}
}

let new_history_record =
new history_record(import_list[index], import_list[index+1], access_time);
this.add_history(new_history_record, false);

last_month = [access_Date.getFullYear(), access_Date.getMonth() + 1];
}
}
console.log("[Meow][bunch_of_history] Import history success, length: " + this.history_this_month.length.toString())
}
}
20 changes: 20 additions & 0 deletions home/src/main/ets/utils/storage_tools.ets
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,26 @@ export function sandbox_save(file_name: string, content: string | ArrayBuffer) {
fs.closeSync(file);
}

/**
* Unlink (delete) a file.
* @param file_name A string, the path of the file.
* */
export function sandbox_unlink(file_name: string) {
let filesDir = getContext().filesDir;
let full_file_directory = filesDir + '/' + file_name;
fs.unlink(full_file_directory);
}

/**
* Unlink (delete) a file in a sync way.
* @param file_name A string, the path of the file.
* */
export function sandbox_unlink_sync(file_name: string) {
let filesDir = getContext().filesDir;
let full_file_directory = filesDir + '/' + file_name;
fs.unlinkSync(full_file_directory);
}

/**
* Read a text file from sandbox storage.
* @returns A string, of the requested file's content.
Expand Down

0 comments on commit 5aca2c6

Please sign in to comment.