-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from Haacked/haacked/24-customize-encouragements
Add property page for encouragements
- Loading branch information
Showing
13 changed files
with
283 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using Microsoft.VisualStudio.Settings; | ||
using Microsoft.VisualStudio.Shell; | ||
using Microsoft.VisualStudio.Shell.Settings; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.Composition; | ||
using System.Diagnostics; | ||
|
||
namespace Haack.Encourage | ||
{ | ||
[Export(typeof(IEncouragements))] | ||
public class Encouragements : IEncouragements | ||
{ | ||
const string CollectionPath = "Encouragements"; | ||
const string PropertyName = "AllEncouragements"; | ||
|
||
static readonly Random random = new Random(); | ||
static readonly string[] defaultEncouragements = new[] | ||
{ | ||
"Nice Job!", | ||
"Way to go!", | ||
"Wow, nice change!", | ||
"So good!", | ||
"Bravo!", | ||
"You rock!", | ||
"Well done!", | ||
"I see what you did there!", | ||
"Genius work!", | ||
"Thumbs up!", | ||
"Coding win!", | ||
"FTW!", | ||
"Yep!", | ||
"Nnnnailed it!" | ||
}; | ||
|
||
readonly List<string> encouragements = new List<string>(defaultEncouragements); | ||
readonly WritableSettingsStore writableSettingsStore; | ||
|
||
public IEnumerable<string> AllEncouragements | ||
{ | ||
get { return encouragements; } | ||
set | ||
{ | ||
encouragements.Clear(); | ||
encouragements.AddRange(value); | ||
if (encouragements.Count == 0) | ||
{ | ||
encouragements.AddRange(defaultEncouragements); | ||
} | ||
SaveSettings(); | ||
} | ||
} | ||
|
||
[ImportingConstructor] | ||
public Encouragements(SVsServiceProvider vsServiceProvider) | ||
{ | ||
var shellSettingsManager = new ShellSettingsManager(vsServiceProvider); | ||
writableSettingsStore = shellSettingsManager.GetWritableSettingsStore(SettingsScope.UserSettings); | ||
|
||
LoadSettings(); | ||
} | ||
|
||
public string GetRandomEncouragement() | ||
{ | ||
int randomIndex = random.Next(0, encouragements.Count); | ||
return encouragements[randomIndex]; | ||
} | ||
|
||
void LoadSettings() | ||
{ | ||
try | ||
{ | ||
if (writableSettingsStore.PropertyExists(CollectionPath, PropertyName)) | ||
{ | ||
string value = writableSettingsStore.GetString(CollectionPath, PropertyName); | ||
AllEncouragements = value.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
Debug.Fail(ex.Message); | ||
} | ||
} | ||
|
||
void SaveSettings() | ||
{ | ||
try | ||
{ | ||
if (!writableSettingsStore.CollectionExists(CollectionPath)) | ||
{ | ||
writableSettingsStore.CreateCollection(CollectionPath); | ||
} | ||
|
||
string value = string.Join(Environment.NewLine, encouragements); | ||
writableSettingsStore.SetString(CollectionPath, PropertyName, value); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Debug.Fail(ex.Message); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
namespace Haack.Encourage | ||
using System.Collections.Generic; | ||
namespace Haack.Encourage | ||
{ | ||
public interface IEncouragements | ||
{ | ||
IEnumerable<string> AllEncouragements { get; set; } | ||
|
||
string GetRandomEncouragement(); | ||
} | ||
} |
Oops, something went wrong.