Skip to content

Commit 006a453

Browse files
committed
Preferences
1 parent 9b05a88 commit 006a453

File tree

4 files changed

+188
-5
lines changed

4 files changed

+188
-5
lines changed

App/App.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
<Reference Include="System" />
5555
<Reference Include="System.Core" />
5656
<Reference Include="Xamarin.Mac" />
57+
<Reference Include="Mono.Data.Sqlite" />
58+
<Reference Include="System.Data" />
5759
</ItemGroup>
5860
<ItemGroup>
5961
<ImageAsset Include="Assets.xcassets\AppIcon.appiconset\Contents.json" />
@@ -96,6 +98,7 @@
9698
<Compile Include="ListViewCell.designer.cs">
9799
<DependentUpon>ListViewCell.cs</DependentUpon>
98100
</Compile>
101+
<Compile Include="Preferences.cs" />
99102
</ItemGroup>
100103
<ItemGroup>
101104
<InterfaceDefinition Include="Main.storyboard" />

App/Main.storyboard

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="11762" systemVersion="16D32" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
33
<dependencies>
4-
<deployment identifier="macosx"/>
54
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="11762"/>
65
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
76
<capability name="stacking Non-gravity area distributions on NSStackView" minToolsVersion="7.0" minSystemVersion="10.11"/>

App/Preferences.cs

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
using System;
2+
using System.Data;
3+
using System.IO;
4+
using Mono.Data.Sqlite;
5+
6+
namespace SharpCpp
7+
{
8+
/// <summary>
9+
/// Very simplified preferences, just to store key/value pairs of strings.
10+
/// </summary>
11+
public static class Preferences
12+
{
13+
public const string AppName = "SharpCppApp";
14+
15+
static readonly DbHelper _dbHelper;
16+
17+
static readonly PreferencesDbHelper _preferencesDbHelper;
18+
19+
static Preferences()
20+
{
21+
_dbHelper = new DbHelper();
22+
_preferencesDbHelper = new PreferencesDbHelper(_dbHelper);
23+
}
24+
25+
class PreferencesDbHelper
26+
{
27+
const string TableName = "Preferences";
28+
29+
const string IdColumn = "id";
30+
31+
const string ValueColumn = "value";
32+
33+
readonly DbHelper DbHelper;
34+
35+
internal PreferencesDbHelper(DbHelper dbHelper)
36+
{
37+
DbHelper = dbHelper;
38+
CreateTableIfNone();
39+
}
40+
41+
void CreateTableIfNone()
42+
{
43+
using (var connection = DbHelper.CreateDbConnection()) {
44+
connection.Open();
45+
46+
var command = "create table if not exists " +
47+
$"{ TableName } (" +
48+
$" { IdColumn } text primary key, " +
49+
$" { ValueColumn } text" +
50+
")";
51+
52+
using (var c = connection.CreateCommand()) {
53+
c.CommandText = command;
54+
c.CommandType = CommandType.Text;
55+
c.ExecuteNonQuery();
56+
}
57+
58+
connection.Close();
59+
}
60+
}
61+
62+
public string GetValue(string key)
63+
{
64+
string value = null;
65+
66+
using (var connection = _dbHelper.CreateDbConnection()) {
67+
connection.Open();
68+
69+
using (var command = connection.CreateCommand()) {
70+
command.CommandText = $"SELECT {ValueColumn} FROM [{TableName}] WHERE {IdColumn}=@id";
71+
command.Parameters.AddWithValue("@id", key);
72+
73+
using (var reader = command.ExecuteReader()) {
74+
while (reader.Read()) {
75+
value = (string)reader[0];
76+
}
77+
}
78+
}
79+
80+
connection.Close();
81+
}
82+
83+
return value;
84+
}
85+
86+
public void PutValue(string key, string value)
87+
{
88+
using (var connection = _dbHelper.CreateDbConnection()) {
89+
connection.Open();
90+
91+
// insert or ignore
92+
93+
using (var command = connection.CreateCommand()) {
94+
command.CommandText = $"INSERT OR IGNORE INTO [{TableName}] ({IdColumn}, {ValueColumn}) " +
95+
"VALUES (@id, @value)";
96+
97+
command.Parameters.AddWithValue("@id", key);
98+
command.Parameters.AddWithValue("@value", value);
99+
100+
command.ExecuteNonQuery();
101+
}
102+
103+
// update
104+
105+
using (var command = connection.CreateCommand()) {
106+
command.CommandText = $"UPDATE {TableName} SET {ValueColumn}=@value WHERE {IdColumn}=@id";
107+
108+
command.Parameters.AddWithValue("@id", key);
109+
command.Parameters.AddWithValue("@value", value);
110+
111+
command.ExecuteNonQuery();
112+
}
113+
114+
connection.Close();
115+
}
116+
}
117+
}
118+
119+
class DbHelper
120+
{
121+
const string DbName = "SharpCppApp.db";
122+
123+
readonly string _dbFile;
124+
125+
internal DbHelper()
126+
{
127+
_dbFile = CreateDbIfNone();
128+
}
129+
130+
string CreateDbIfNone()
131+
{
132+
string dbFile;
133+
134+
var documents = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
135+
var appDirectory = Path.Combine(documents, AppName);
136+
137+
if (!Directory.Exists(appDirectory)) {
138+
Directory.CreateDirectory(appDirectory);
139+
}
140+
141+
dbFile = Path.Combine(appDirectory, DbName);
142+
if (!File.Exists(dbFile)) {
143+
SqliteConnection.CreateFile(dbFile);
144+
}
145+
146+
return dbFile;
147+
}
148+
149+
public SqliteConnection CreateDbConnection()
150+
{
151+
return new SqliteConnection("Data Source=" + _dbFile);
152+
}
153+
}
154+
155+
#region LastSelectedFilename
156+
157+
const string LastSelectedFilenameKey = "last_selected_filename";
158+
159+
// cached
160+
static string _selectedFilename;
161+
162+
static bool _selectedFilenameLazyLoaded;
163+
164+
public static string LastSelectedFilename {
165+
get {
166+
if (!_selectedFilenameLazyLoaded) {
167+
_selectedFilename = _preferencesDbHelper.GetValue(LastSelectedFilenameKey);
168+
_selectedFilenameLazyLoaded = true;
169+
}
170+
171+
return _selectedFilename;
172+
}
173+
set {
174+
_selectedFilename = value;
175+
_preferencesDbHelper.PutValue(LastSelectedFilenameKey, value);
176+
}
177+
}
178+
179+
#endregion
180+
181+
}
182+
}

App/ViewController.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ public ViewController(IntPtr handle) : base(handle)
6565
}
6666

6767
TVDataSource _tvDataSource;
68-
string _selectedFilename;
6968

7069
public override void ViewDidLoad()
7170
{
@@ -132,7 +131,7 @@ void UpdateTextFields(string inputCode)
132131

133132
try {
134133
// with same name
135-
UpdateGeneratedFileViews(files.Single((arg) => arg.ToString() == _selectedFilename));
134+
UpdateGeneratedFileViews(files.Single((arg) => arg.ToString() == Preferences.LastSelectedFilename));
136135
} catch (InvalidOperationException) {
137136
// firs in the list
138137
UpdateGeneratedFileViews(files[0]);
@@ -148,10 +147,10 @@ void UpdateGeneratedFileViews(TFile file)
148147
_tvDataSource.UpdateSelectedPosition(file);
149148

150149
if (file == null) {
151-
_selectedFilename = null;
150+
Preferences.LastSelectedFilename = null;
152151
GeneratedFileContent.Value = null;
153152
} else {
154-
_selectedFilename = file.ToString();
153+
Preferences.LastSelectedFilename = file.ToString();
155154
GeneratedFileContent.Value = file.Content;
156155
}
157156
}

0 commit comments

Comments
 (0)