Skip to content

Commit

Permalink
add error message if no light sensor is present
Browse files Browse the repository at this point in the history
  • Loading branch information
stephtr committed Apr 19, 2020
1 parent 1b50332 commit 50d173c
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion BrightnessSwitch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ApplicationIcon>sun.ico</ApplicationIcon>
<Version>1.0.0</Version>
<Version>1.0.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Changelog

## master
## 1.0.1

* Show message when no update is available.
* Show an error message if the device has no light sensor.

## 1.0.0

Expand Down
4 changes: 4 additions & 0 deletions LightControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public LightControl(uint sensorInterval = 10_000, float illuminanceThreshold = 5
IlluminanceThreshold = illuminanceThreshold;

Sensor = LightSensor.GetDefault();
if (Sensor == null)
{
throw new NotSupportedException("No light sensor present");
}
Sensor.ReportInterval = Math.Max(Sensor.MinimumReportInterval, sensorInterval);
Sensor.ReadingChanged += new TypedEventHandler<LightSensor, LightSensorReadingChangedEventArgs>(LightReadingChanged);

Expand Down
14 changes: 12 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,17 @@ static void Main(string[] args)
{
LoadSettings();

var lightControl = new LightControl();
LightControl lightControl = null!;
try
{
lightControl = new LightControl();
}
catch (NotSupportedException)
{
MessageBox.Show("This app can't run on your device, since it has no light sensor.", "BrightnessSwitch", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

lightControl.PredictionCallback += (float illuminanceInLux) =>
{
var (prediction, certainty) = predictionModel.Predict(Math.Log(illuminanceInLux));
Expand All @@ -89,7 +99,7 @@ static void Main(string[] args)
trayIcon.OnExit += (object? sender, int reason) => Application.Exit();
trayIcon.OnThemeSwitch += (object? sender, bool useLightTheme) =>
{
if (trayIcon.AutoSwitchEnabled) // Otherwise we don't have to learn it
if (trayIcon.AutoSwitchEnabled) // Otherwise we don't have to learn from the current action
{
var currentIlluminance = lightControl.GetCurrentIlluminance();
if (currentIlluminance > 0)
Expand Down
6 changes: 3 additions & 3 deletions TrayIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private async void CheckUpdates(bool showOptionalMessages = true)
var releaseUrl = urlMatch.Groups[1].Value;
if (currentVersion < newVersion)
{
if (MessageBox.Show("There is a new version available. Do you want to go to the download page?", "Updating BrightnessSwitch", MessageBoxButtons.YesNo) == DialogResult.Yes)
if (MessageBox.Show("There is a new version available. Do you want to go to the download page?", "Updating BrightnessSwitch", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
var psi = new ProcessStartInfo
{
Expand All @@ -158,14 +158,14 @@ private async void CheckUpdates(bool showOptionalMessages = true)
}
else if (showOptionalMessages)
{
MessageBox.Show("You are already using the latest version.", "Updating BrightnessSwitch");
MessageBox.Show("You are already using the latest version.", "Updating BrightnessSwitch", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch
{
if (showOptionalMessages)
{
MessageBox.Show("Error downloading the update information", "Updating BrightnessSwitch");
MessageBox.Show("Error downloading the update information", "Updating BrightnessSwitch", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
Expand Down

0 comments on commit 50d173c

Please sign in to comment.