Skip to content

Commit

Permalink
Code Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Harabe-x committed Sep 27, 2023
1 parent f81ffb8 commit ebedcc8
Show file tree
Hide file tree
Showing 20 changed files with 83 additions and 100 deletions.
11 changes: 5 additions & 6 deletions PixaiBot/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public partial class App : Application

public App()
{
_logger = new Logger() ;
_logger = new Logger();
IServiceCollection services = new ServiceCollection();
services.AddSingleton<MainWindowView>(provider => new MainWindowView()
{ DataContext = provider.GetService<MainWindowViewModel>() });
Expand All @@ -42,29 +42,28 @@ public App()
services.AddSingleton<Func<Type, BaseViewModel>>(serviceProvider =>
viewModelType => (BaseViewModel)serviceProvider.GetRequiredService(viewModelType));
_serviceProvider = services.BuildServiceProvider();
Application.Current.DispatcherUnhandledException += HandleUnhandledApplicationException;
Current.DispatcherUnhandledException += HandleUnhandledApplicationException;
}

protected override void OnStartup(StartupEventArgs e)
{
InitialConfiguration.CreateConfigFile();
InitialConfiguration.CreateStatisticsFile();
_logger.Log("=====Application Started=====",_logger.ApplicationLogFilePath);
_logger.Log("=====Application Started=====", _logger.ApplicationLogFilePath);
base.OnStartup(e);
var mainWindow = _serviceProvider.GetRequiredService<MainWindowView>();
mainWindow.Show();
}

protected override void OnExit(ExitEventArgs e)
{
_logger.Log("=====Application Closed=====",_logger.ApplicationLogFilePath);
_logger.Log("=====Application Closed=====", _logger.ApplicationLogFilePath);
base.OnExit(e);
}

private void HandleUnhandledApplicationException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
_logger.Log($"{e.Exception} | {e.Exception.Message}",_logger.ApplicationLogFilePath);
_logger.Log($"{e.Exception} | {e.Exception.Message}", _logger.ApplicationLogFilePath);
e.Handled = true;
}

}
9 changes: 3 additions & 6 deletions PixaiBot/Bussines Logic/Data Management/AccountsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class AccountsManager : IAccountsManager

private readonly ILogger _logger;

public AccountsManager(IBotStatisticsManager botStatisticsManager,ILogger logger)
public AccountsManager(IBotStatisticsManager botStatisticsManager, ILogger logger)
{
_logger = logger;
AccountsFilePath = InitialConfiguration.AccountsFilePath;
Expand All @@ -33,7 +33,6 @@ public AccountsManager(IBotStatisticsManager botStatisticsManager,ILogger logger

public void AddAccount(UserAccount account)
{

if (!File.Exists(AccountsFilePath))
{
var accountsList = new List<UserAccount>();
Expand All @@ -43,7 +42,7 @@ public void AddAccount(UserAccount account)
UpdateAccountManagerProperties();
_logger.Log("Added account ", _logger.ApplicationLogFilePath);

return;
return;
}

var accountList = _jsonReader.ReadAccountFile(AccountsFilePath);
Expand All @@ -52,7 +51,6 @@ public void AddAccount(UserAccount account)
_botStatisticsManager.IncreaseAccountsCount(1);
_logger.Log("Added account", _logger.ApplicationLogFilePath);
UpdateAccountManagerProperties();

}

public void RemoveAccount(IList<UserAccount> accountList, UserAccount userAccount)
Expand All @@ -63,7 +61,6 @@ public void RemoveAccount(IList<UserAccount> accountList, UserAccount userAccoun
_botStatisticsManager.IncreaseAccountsCount(-1);
UpdateAccountManagerProperties();
_logger.Log("Removed account", _logger.ApplicationLogFilePath);

}

public IEnumerable<UserAccount> GetAllAccounts()
Expand All @@ -87,7 +84,7 @@ public void AddManyAccounts()
var result = dialog.ShowDialog();

if (result == false) return;

var importedUserAccounts = GetUserAccountsFromTxt(dialog.FileName);
_logger.Log("Adding accounts in batch", _logger.ApplicationLogFilePath);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace PixaiBot.Bussines_Logic;

public class BotStatisticsManager : IBotStatisticsManager
{

private string AccountsStatisticsFilePath { get; }

private BotStatistics _botStatistics;
Expand Down Expand Up @@ -40,7 +39,6 @@ public void RefreshStatistics()
AccountsNumber = _botStatistics.AccountsCount;
LastCreditClaimDateTime = _botStatistics.LastCreditClaimDateTime;
BotVersion = _botStatistics.BotVersion;

}

public void IncreaseAccountsCount(int number)
Expand Down Expand Up @@ -76,6 +74,5 @@ public void ResetNumberOfAccounts()
{
_botStatistics.AccountsCount = 0;
_logger.Log("Account count rested", _logger.ApplicationLogFilePath);

}
}
4 changes: 2 additions & 2 deletions PixaiBot/Bussines Logic/Data Management/ConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ public ConfigManager(ILogger logger)

public UserConfig GetConfig()
{
_logger.Log("Readed Config File",_logger.ApplicationLogFilePath);
_logger.Log("Readed Config File", _logger.ApplicationLogFilePath);
return _jsonReader.ReadConfigFile(ConfigFilePath);
}

public void SaveConfig(UserConfig config)
{
_logger.Log("Writed Config File",_logger.ApplicationLogFilePath);
_logger.Log("Writed Config File", _logger.ApplicationLogFilePath);
JsonWriter.WriteJson(config, ConfigFilePath);
}
}
19 changes: 9 additions & 10 deletions PixaiBot/Bussines Logic/Data Management/InitialConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public static class InitialConfiguration

static InitialConfiguration()
{

ApplicationDataPath =
$"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\PixaiAutoClaimer";
UserConfigPath =
Expand All @@ -50,7 +49,7 @@ public static void CreateDirectories()
public static void CreateConfigFile()
{
if (File.Exists(UserConfigPath)) return;

var userConfig = new UserConfig()
{
StartWithSystem = false,
Expand All @@ -63,13 +62,13 @@ public static void CreateConfigFile()
public static void CreateStatisticsFile()
{
if (File.Exists(StatisticsFilePath)) return;
var statistics = new BotStatistics()
{
AccountsCount = 0,
BotVersion = BotVersion,
LastCreditClaimDateTime = DateTime.Now,
};
JsonWriter.WriteJson(statistics, StatisticsFilePath);

var statistics = new BotStatistics()
{
AccountsCount = 0,
BotVersion = BotVersion,
LastCreditClaimDateTime = DateTime.Now
};
JsonWriter.WriteJson(statistics, StatisticsFilePath);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ public bool CheckAccountLogin(UserAccount userAccount)
{
_driver.Close();
_driver.Dispose();
_logger.Log($"Valid Account {userAccount.Email}",_logger.CreditClaimerLogFilePath);
_logger.Log($"=====Chrome Drive Disposed=====\n",_logger.CreditClaimerLogFilePath);
_logger.Log($"Valid Account {userAccount.Email}", _logger.CreditClaimerLogFilePath);
_logger.Log($"=====Chrome Drive Disposed=====\n", _logger.CreditClaimerLogFilePath);
return true;
}

_driver.Close();
_driver.Dispose();
_logger.Log("Invalid Account",_logger.CreditClaimerLogFilePath);
_logger.Log("=====Chrome Drive Disposed=====\n", _logger.CreditClaimerLogFilePath);
return false;
_logger.Log("Invalid Account", _logger.CreditClaimerLogFilePath);
_logger.Log("=====Chrome Drive Disposed=====\n", _logger.CreditClaimerLogFilePath);
return false;
}

public int CheckAllAccountsLogin(IList<UserAccount> accountsList)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,22 @@
using System.Threading.Tasks;
using OpenQA.Selenium.Chrome;

namespace PixaiBot.Bussines_Logic
namespace PixaiBot.Bussines_Logic;

public static class ChromeDriverFactory
{
public static class ChromeDriverFactory
/// <summary>
/// Creates a ChromeDriver with the default settings.
/// </summary>
/// <returns>Chrome Driver Instance</returns>
public static ChromeDriver CreateDriver()
{
/// <summary>
/// Creates a ChromeDriver with the default settings.
/// </summary>
/// <returns>Chrome Driver Instance</returns>
public static ChromeDriver CreateDriver()
{
ChromeOptions options = new ChromeOptions();
options.AddArgument("--window-position=-32000,-32000");
var options = new ChromeOptions();
options.AddArgument("--window-position=-32000,-32000");

ChromeDriverService service = ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true;
var service = ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true;

return new ChromeDriver(service, options);
}
return new ChromeDriver(service, options);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public CreditClaimer(ILogger logger)
_logger = logger;
}


/// <summary>
/// Claims credits on the given account
/// </summary>
Expand All @@ -42,16 +42,16 @@ public CreditClaimer(ILogger logger)
public void ClaimCredits(UserAccount account, IToastNotificationSender toastNotificationSender = null)
{
_driver = ChromeDriverFactory.CreateDriver();

_driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(MaxWaitTime);

_driver.Manage().Window.Minimize();
LoginModule.Login(_driver, account,_logger);
LoginModule.Login(_driver, account, _logger);
if (_driver.Url == LoginUrl)
{
toastNotificationSender?.SendNotification("Login failed", $"Login failed for {account.Email}",
NotificationType.Error);
_logger.Log("Login failed",_logger.CreditClaimerLogFilePath);
_logger.Log("Login failed", _logger.CreditClaimerLogFilePath);
_driver.Quit();
_logger.Log("=====Chrome Drive Disposed=====\n", _logger.CreditClaimerLogFilePath);
return;
Expand Down Expand Up @@ -84,8 +84,8 @@ public void ClaimCredits(UserAccount account, IToastNotificationSender toastNoti
_logger.Log($"Claiming credits completed successfully for {account.Email}", _logger.CreditClaimerLogFilePath);
_driver.Quit();
_logger.Log("=====Chrome Drive Disposed=====\n", _logger.CreditClaimerLogFilePath);

}

/// <summary>
/// Navigates to profile page
/// </summary>
Expand All @@ -111,6 +111,7 @@ private bool GoToProfile()
return false;
}
}

/// <summary>
/// Claims credits on the account
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public static class LoginModule
private const int StartWaitTime = 1000;



/// <summary>
/// Logs user to pixai.art
/// <em>This method is used by CreditClaimer and AccountChecker</em>
Expand All @@ -29,7 +28,7 @@ public static class LoginModule
public static void Login(ChromeDriver driver, UserAccount userAccount, ILogger logger)
{
logger.Log("=====Launched Chrome Driver=====", logger.CreditClaimerLogFilePath);

driver.Navigate().GoToUrl(LoginUrl);

Thread.Sleep(StartWaitTime);
Expand All @@ -53,6 +52,7 @@ public static void Login(ChromeDriver driver, UserAccount userAccount, ILogger l

return;
}

logger.Log($"Sending user credentials to textboxes", logger.CreditClaimerLogFilePath);

textInputs.ElementAt(0).Click();
Expand All @@ -71,7 +71,7 @@ public static void Login(ChromeDriver driver, UserAccount userAccount, ILogger l
}

buttons.FirstOrDefault(x => x.Text == "Login")?.Click();

logger.Log($"Login Button Clicked", logger.CreditClaimerLogFilePath);
}
}
14 changes: 8 additions & 6 deletions PixaiBot/Bussines Logic/Logging/Logger.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

using PixaiBot.Data.Interfaces;
using PixaiBot.Data.Interfaces;
using System.IO;
using System;

Expand All @@ -13,11 +12,14 @@ public class Logger : ILogger

public Logger()
{
CreditClaimerLogFilePath = $@"{InitialConfiguration.BotLogsPath}\CreditClaimer Log {DateTime.Now:yyyy-MM-dd}.txt";
CreditClaimerLogFilePath =
$@"{InitialConfiguration.BotLogsPath}\CreditClaimer Log {DateTime.Now:yyyy-MM-dd}.txt";
ApplicationLogFilePath = $@"{InitialConfiguration.BotLogsPath}\Application Log {DateTime.Now:yyyy-MM-dd}.txt";

if (!File.Exists(CreditClaimerLogFilePath)) File.Create(CreditClaimerLogFilePath); ;
if (!File.Exists(ApplicationLogFilePath)) File.Create(ApplicationLogFilePath); ;

if (!File.Exists(CreditClaimerLogFilePath)) File.Create(CreditClaimerLogFilePath);
;
if (!File.Exists(ApplicationLogFilePath)) File.Create(ApplicationLogFilePath);
;
}

public void Log(string message, string filePath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public ToastNotificationSender(ILogger logger)
}



public void SendNotification(string title, string message, NotificationType notificationType)
{
_logger.Log($"Notification Sent, NotificationType{notificationType}", _logger.ApplicationLogFilePath);
Expand Down
2 changes: 1 addition & 1 deletion PixaiBot/Data/Interfaces/ILogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ public interface ILogger

public string ApplicationLogFilePath { get; }

public void Log(string message,string filePath);
public void Log(string message, string filePath);
}
4 changes: 2 additions & 2 deletions PixaiBot/Data/Interfaces/IWindowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace PixaiBot.Data.Interfaces;

public interface IWindowHelper
{
public Action Close { get; set; }
public Action Close { get; set; }

public bool CanCloseWindow();
public bool CanCloseWindow();
}
2 changes: 1 addition & 1 deletion PixaiBot/UI/Helpers/TrayIconHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private static void HideToTray(DependencyObject d, DependencyPropertyChangedEven
if (trayIconHelper.CanHideToTray())
{
window.Hide();

var notifyIcon = new NotifyIcon()
{
Icon = new Icon("Resources/images/PixaiAutoClaimerIcon.ico"),
Expand Down
2 changes: 1 addition & 1 deletion PixaiBot/UI/View/AddAccountWindowView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public partial class AddAccountWindowView : Window
public AddAccountWindowView(IAccountsManager accountsManager, IDataValidator dataValidator)
{
InitializeComponent();
DataContext = new AddAccountWindowViewModel(accountsManager, dataValidator,new Logger());
DataContext = new AddAccountWindowViewModel(accountsManager, dataValidator, new Logger());
}

private void Border_OnMouseDown(object sender, MouseButtonEventArgs e)
Expand Down
Loading

0 comments on commit ebedcc8

Please sign in to comment.