Skip to content

Commit

Permalink
Added: version checker; Added: Get titles by producer that isn't yet …
Browse files Browse the repository at this point in the history
…in local db (List by Producer); Unified colors in StaticHelpers.
  • Loading branch information
Zoltanar committed Nov 13, 2016
1 parent acf4a53 commit 007aa15
Show file tree
Hide file tree
Showing 13 changed files with 236 additions and 65 deletions.
38 changes: 21 additions & 17 deletions Visual Novel Database/FormMain.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 59 additions & 7 deletions Visual Novel Database/FormMain.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
Expand All @@ -14,7 +15,11 @@
using Happy_Search.Properties;
using Microsoft.Win32;
using Newtonsoft.Json;
using Octokit;
using static Happy_Search.StaticHelpers;
using Application = System.Windows.Forms.Application;
using Label = System.Windows.Forms.Label;
// ReSharper disable LocalizableElement

namespace Happy_Search
{
Expand Down Expand Up @@ -47,6 +52,7 @@ public partial class FormMain : Form
internal bool DontTriggerEvent; //used to skip indexchanged events
private List<ListedProducer> _producerList; //contains all producers in local database
internal List<CharacterItem> CharacterList; //contains all producers in local database
internal List<ListedProducer> FavoriteProducerList; //contains all favorite producers for logged in user
private List<ListedVN> _vnList; //contains all vns in local database
private ushort _vnsAdded;
private ushort _vnsSkipped;
Expand Down Expand Up @@ -78,6 +84,7 @@ public FormMain()
ulStatusDropDown.SelectedIndex = 0;
wlStatusDropDown.SelectedIndex = 0;
customTagFilters.SelectedIndex = 0;
customTraitFilters.SelectedIndex = 0;
viewPicker.SelectedIndex = 0;
URTToggleBox.SelectedIndex = 0;
UnreleasedToggleBox.SelectedIndex = 0;
Expand Down Expand Up @@ -224,15 +231,37 @@ ObjectListView by Phillip Piper (GPLv3)

private async void OnLoadRoutines(object sender, EventArgs e)
{
//client update
var args = Environment.GetCommandLineArgs();
if (!args.Contains("-debug") && !args.Contains("-sc"))
{
await ClientUpdateAsync();
}

InitAPIConnection();

//dbstats update
LogToFile($"dbstats Update = {Settings.Default.DBStatsUpdate}, days since = {DaysSince(Settings.Default.DBStatsUpdate)}");
if (DaysSince(Settings.Default.DBStatsUpdate) > 2 || DaysSince(Settings.Default.DBStatsUpdate) == -1) GetNewDBStats();
else LoadDBStats();

//urt update
if (Settings.Default.AutoUpdateURT && UserID > 0)
{
LogToFile($"dbstats Update = {Settings.Default.DBStatsUpdate}, days since = {DaysSince(Settings.Default.DBStatsUpdate)}");
if (DaysSince(Settings.Default.DBStatsUpdate) > 2 || DaysSince(Settings.Default.DBStatsUpdate) == -1) GetNewDBStats();
else LoadDBStats();
await URTUpdateAsync();
}
if (!Settings.Default.AutoUpdateURT || UserID <= 0) return;

traitRootsDropdown.SelectedIndex = 0;
}

/// <summary>
/// Check if URT update is due and if so, execute it
/// </summary>
private async Task URTUpdateAsync()
{
LogToFile("Checking if URT Update is due...");
LogToFile($"URTUpdate= {Settings.Default.URTUpdate}, days since = {DaysSince(Settings.Default.URTUpdate)}");
LogToFile(
$"URTUpdate= {Settings.Default.URTUpdate}, days since = {DaysSince(Settings.Default.URTUpdate)}");
if (DaysSince(Settings.Default.URTUpdate) > 2 || DaysSince(Settings.Default.URTUpdate) == -1)
{
LogToFile("Updating User Related Titles...");
Expand All @@ -246,6 +275,29 @@ private async void OnLoadRoutines(object sender, EventArgs e)
}
}

/// <summary>
/// Checks if there is a new release out and if so, ask user if they want to visit releases page.
/// </summary>
private async Task ClientUpdateAsync()
{
var client = new GitHubClient(new ProductHeaderValue("Happy-Search"));
IReadOnlyList<Release> releases = await client.Repository.Release.GetAll("Zoltanar", "Happy-Search");
var latest = releases[0];
LogToFile($"The latest release is tagged at {latest.TagName} and is named {latest.Name}");
if (!latest.TagName.Equals(ClientVersion))
{
var messageBoxResult = MessageBox.Show($"There is a newer version available.\nInstalled: {ClientVersion}\nLatest: {latest.TagName}\nDo you wish to visit the releases page?",
@"Update Client?", MessageBoxButtons.YesNo);
if (messageBoxResult == DialogResult.Yes)
{
Process.Start($"{ProjectURL}/releases");
}
}
}

/// <summary>
/// Initialize API Connection.
/// </summary>
private void InitAPIConnection()
{
SplashScreen.SetStatus("Logging into VNDB API...");
Expand Down Expand Up @@ -456,7 +508,7 @@ private async Task GetAllMissingImages()
{
IEnumerable<ListedVN> vnsWithImages = _vnList.Where(x => !x.ImageURL.Equals(""));
ListedVN[] vnsMissingImages = (from vn in vnsWithImages
let photoFile = String.Format($"{VNImagesFolder}{vn.VNID}{Path.GetExtension(vn.ImageURL)}")
let photoFile = string.Format($"{VNImagesFolder}{vn.VNID}{Path.GetExtension(vn.ImageURL)}")
where !File.Exists(photoFile)
select vn).ToArray();
const int averageImageSizeBytes = 37 * 1024;
Expand Down Expand Up @@ -1192,7 +1244,7 @@ private void VNToolTip(object sender, BrightIdeasSoftware.ToolTipShowingEventArg
if (characterCount > 0) toolTipLines.Add($"Characters: {characterCount}.");
if (notes != null && !notes.Notes.Equals("")) toolTipLines.Add($"{TruncateString($"Notes: {notes.Notes}", 50)}");
if (notes != null && notes.Groups.Count != 0) toolTipLines.Add($"{TruncateString($"Groups: {string.Join(", ", notes.Groups)}", 50)}");
e.Text = string.Join("\n",toolTipLines);
e.Text = string.Join("\n", toolTipLines);
}
#endregion

Expand Down
34 changes: 34 additions & 0 deletions Visual Novel Database/FormMainAPIMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,41 @@ internal async Task<ListedVN> UpdateSingleVN(int vnid, LinkLabel updateLink)
return vn;
}

/// <summary>
/// Searches VNDB for producers by name, independent.
/// Call StartQuery prior to it and ChangeAPIStatus afterwards.
/// </summary>
internal async Task<List<ProducerItem>> AddProducersBySearchedName(string producerName, Label replyLabel)
{
string prodSearchQuery = $"get producer basic (search~\"{producerName}\") {{{MaxResultsString}}}";
var result = await TryQuery(prodSearchQuery, Resources.ps_query_error, replyLabel);
if (!result) return null;
var prodRoot = JsonConvert.DeserializeObject<ProducersRoot>(Conn.LastResponse.JsonPayload);
List<ProducerItem> prodItems = prodRoot.Items;
var moreResults = prodRoot.More;
var pageNo = 1;
while (moreResults)
{
pageNo++;
string prodSearchMoreQuery =
$"get producer basic (search~\"{producerName}\") {{{MaxResultsString}, \"page\":{pageNo}}}";
var moreResult =
await TryQuery(prodSearchMoreQuery, Resources.ps_query_error, replyLabel);
if (!moreResult) return null;
var prodMoreRoot = JsonConvert.DeserializeObject<ProducersRoot>(Conn.LastResponse.JsonPayload);
prodItems.AddRange(prodMoreRoot.Items);
moreResults = prodMoreRoot.More;
}
for (int index = prodItems.Count - 1; index >= 0; index--)
{
if(_producerList.Exists(x=>x.Name.Equals(prodItems[index].Name))) prodItems.RemoveAt(index);
}
DBConn.BeginTransaction();
foreach (var producer in prodItems) DBConn.InsertProducer((ListedProducer)producer, true);
DBConn.EndTransaction();
return prodItems;

}

/// <summary>
/// Get data about multiple visual novels.
Expand Down
7 changes: 4 additions & 3 deletions Visual Novel Database/FormMainFavoriteProducers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,19 +240,20 @@ private async Task GetProducerTitles(ListedProducer producer, Label replyLabel,

/// <summary>
/// Load Favorite Producers into ObjectListView.
/// Gets data from local database.
/// </summary>
private void LoadFPListToGui()
{
if (UserID < 1) return;
DBConn.Open();
List<ListedProducer> favoriteProducers = DBConn.GetFavoriteProducersForUser(UserID);
FavoriteProducerList = DBConn.GetFavoriteProducersForUser(UserID);
DBConn.Close();
foreach (var favoriteProducer in favoriteProducers)
foreach (var favoriteProducer in FavoriteProducerList)
{
double[] vnsWithVotes = _vnList.Where(x => x.Producer.Equals(favoriteProducer.Name) && x.Rating > 0).Select(x => x.Rating).ToArray();
favoriteProducer.GeneralRating = vnsWithVotes.Any() ? Math.Round(vnsWithVotes.Average(), 2) : -1;
}
olFavoriteProducers.SetObjects(favoriteProducers);
olFavoriteProducers.SetObjects(FavoriteProducerList);
olFavoriteProducers.Sort(ol2Name.Index);
}

Expand Down
Loading

0 comments on commit 007aa15

Please sign in to comment.