Skip to content

Commit

Permalink
Merge pull request #799 from Universal-Debloater-Alliance/refactor/ex…
Browse files Browse the repository at this point in the history
…tract-uninstalled_packages_file_name_format-into-const

refactor: extract UNINSTALLED_PACKAGES_FILE_NAME_FORMAT into a const
  • Loading branch information
Rudxain authored Jan 16, 2025
2 parents 2673fd5 + 60ec9f3 commit 0cff895
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
39 changes: 31 additions & 8 deletions src/core/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use crate::core::sync::{hashset_system_packages, list_all_system_packages, User}
use crate::core::theme::Theme;
use crate::core::uad_lists::{PackageHashMap, PackageState, Removal, UadList};
use crate::gui::widgets::package_row::PackageRow;
use chrono::offset::Utc;
use chrono::{DateTime, Local};
use chrono::{offset::Utc, DateTime};
use csv::Writer;
use std::path::PathBuf;
use std::process::Command;
Expand All @@ -17,7 +16,19 @@ pub const NAME: &str = "UAD-ng";
/// [More info](https://developer.android.com/tools/variables#adb)
pub const ANDROID_SERIAL: &str = "ANDROID_SERIAL";
pub const EXPORT_FILE_NAME: &str = "selection_export.txt";
pub const UNINSTALLED_PACKAGES_FILE_NAME: &str = "uninstalled_packages";

// Takes a time-stamp parameter,
// for purity and testability.
//
// The TZ is generic, because testing requires UTC,
// while users get the local-aware version.
pub fn generate_backup_name<T>(t: DateTime<T>) -> String
where
T: chrono::TimeZone,
T::Offset: std::fmt::Display,
{
format!("uninstalled_packages_{}.csv", t.format("%Y%m%d"))
}

#[derive(Debug, Clone)]
pub enum Error {
Expand Down Expand Up @@ -183,11 +194,7 @@ pub async fn export_packages(
user: User,
phone_packages: Vec<Vec<PackageRow>>,
) -> Result<bool, String> {
let backup_file = format!(
"{}_{}.csv",
UNINSTALLED_PACKAGES_FILE_NAME,
Local::now().format("%Y%m%d")
);
let backup_file = generate_backup_name(chrono::Local::now());

let file = fs::File::create(backup_file).map_err(|err| err.to_string())?;
let mut wtr = Writer::from_writer(file);
Expand All @@ -209,3 +216,19 @@ pub async fn export_packages(

Ok(true)
}

#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, reason = "")]

use super::*;
use chrono::TimeZone;

#[test]
fn backup_name() {
assert_eq!(
generate_backup_name(chrono::Utc.timestamp_millis_opt(0).unwrap()),
"uninstalled_packages_19700101.csv".to_string()
);
}
}
12 changes: 4 additions & 8 deletions src/gui/views/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use crate::core::save::{
use crate::core::sync::{get_android_sdk, perform_adb_commands, CommandType, Phone, User};
use crate::core::theme::Theme;
use crate::core::utils::{
export_packages, open_folder, open_url, string_to_theme, DisplayablePath,
UNINSTALLED_PACKAGES_FILE_NAME,
export_packages, generate_backup_name, open_folder, open_url, string_to_theme, DisplayablePath,
};
use crate::gui::style;
use crate::gui::views::list::{List as AppsView, PackageInfo};
Expand Down Expand Up @@ -543,12 +542,9 @@ impl Settings {
text(format!("Exported uninstalled packages into file.\nFile is exported in same directory where {NAME} is located.")).width(Length::Fill),
].padding(20);

let file_row = row![text(format!(
"{}_{}.csv",
UNINSTALLED_PACKAGES_FILE_NAME,
chrono::Local::now().format("%Y%m%d")
))
.style(style::Text::Commentary)]
let file_row = row![
text(generate_backup_name(chrono::Local::now())).style(style::Text::Commentary)
]
.padding(20);

let modal_btn_row = row![
Expand Down

0 comments on commit 0cff895

Please sign in to comment.