Skip to content

Commit

Permalink
Minor update to handle Xamarin support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Fe-Bell committed Apr 25, 2019
1 parent 1ded620 commit 1176b86
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 28 deletions.
126 changes: 104 additions & 22 deletions ReflectXMLDB/DatabaseHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class DatabaseHandler : DatabaseHandlerBase
/// <summary>
/// Mutex to protect file access from multiple processes.
/// </summary>
protected static Mutex mutex = new Mutex(false, "ReflectXMLDBMutex");
protected static Mutex mutex = null;

#endregion

Expand Down Expand Up @@ -127,6 +127,27 @@ public OnDatabaseExportedEventArgs(string databaseName, DateTime date)

#endregion

/// <summary>
/// Creates a new instance of DatabaseHandler.
/// </summary>
public DatabaseHandler()
{
//Xamarin does not currently support mutexes for some mobile platforms.
//When a new Mutex instance is created by Xamarin.Forms, a NotSupportedException/NotImplementedException is thrown.
//The following code handles this exception. The mutex will be ignored because it is null.
try
{
mutex = new Mutex(false, "ReflectXMLDBMutex");
}
catch (Exception ex)
{
if (ex is NotSupportedException || ex is NotImplementedException)
{
Console.WriteLine("Cross-process disabled.");
}
}
}

#region Private methods

/// <summary>
Expand Down Expand Up @@ -219,17 +240,30 @@ private string GetDatabasePath(Type type)
/// <param name="path"></param>
private void StartMonitoringDirectory(string path)
{
fileSystemWatcher = new FileSystemWatcher
//Xamarin does not currently support a unified FileSystemWatcher for mobile platforms.
//When a new FileSystemWatcher instance is created by Xamarin.Forms, a NotSupportedException/NotImplementedException is thrown.
//The following code handles this exception. The FileSystemWatcher will be ignored because it is null.
try
{
Path = path
};
fileSystemWatcher.Changed += FileSystemWatcher_Changed;
fileSystemWatcher.Created += FileSystemWatcher_Created;
fileSystemWatcher.Renamed += FileSystemWatcher_Renamed;
fileSystemWatcher.Deleted += FileSystemWatcher_Deleted;
fileSystemWatcher = new FileSystemWatcher
{
Path = path
};
fileSystemWatcher.Changed += FileSystemWatcher_Changed;
fileSystemWatcher.Created += FileSystemWatcher_Created;
fileSystemWatcher.Renamed += FileSystemWatcher_Renamed;
fileSystemWatcher.Deleted += FileSystemWatcher_Deleted;

fileSystemWatcher.NotifyFilter = NotifyFilters.LastWrite;
fileSystemWatcher.EnableRaisingEvents = true;
fileSystemWatcher.NotifyFilter = NotifyFilters.LastWrite;
fileSystemWatcher.EnableRaisingEvents = true;
}
catch(Exception ex)
{
if(ex is NotSupportedException || ex is NotImplementedException)
{
Console.WriteLine("FileWatcher disabled.");
}
}
}
/// <summary>
/// Stops monitoring a folder. This disposes the Filewatcher and its events.
Expand Down Expand Up @@ -266,7 +300,11 @@ private void StopMonitoringDirectory()
{
try
{
mutex.WaitOne();
if(!mutex.IsNull())
{
mutex.WaitOne();
}

database = path.Deserialize<T>();
}
catch
Expand All @@ -275,7 +313,10 @@ private void StopMonitoringDirectory()
}
finally
{
mutex.ReleaseMutex();
if(!mutex.IsNull())
{
mutex.ReleaseMutex();
}
}
}
catch
Expand Down Expand Up @@ -402,7 +443,11 @@ public void ClearHandler()
{
try
{
mutex.WaitOne();
if (!mutex.IsNull())
{
mutex.WaitOne();
}

xml.Save(path);
}
catch
Expand All @@ -411,7 +456,10 @@ public void ClearHandler()
}
finally
{
mutex.ReleaseMutex();
if (!mutex.IsNull())
{
mutex.ReleaseMutex();
}
}
}
catch
Expand Down Expand Up @@ -441,7 +489,11 @@ public void ClearHandler()
{
try
{
mutex.WaitOne();
if (!mutex.IsNull())
{
mutex.WaitOne();
}

File.Delete(path);
}
catch
Expand All @@ -450,7 +502,10 @@ public void ClearHandler()
}
finally
{
mutex.ReleaseMutex();
if (!mutex.IsNull())
{
mutex.ReleaseMutex();
}
}
}
catch
Expand Down Expand Up @@ -577,16 +632,30 @@ public void ClearHandler()
{
try
{
mutex.WaitOne();
if (!mutex.IsNull())
{
mutex.WaitOne();
}

xml.Save(path);

//Will fire the database changed event when the FileSystemWatcher is unavailable.
//This should happen only when using Xamarin.Forms as there is no native support for that class.
if(fileSystemWatcher.IsNull())
{
OnDatabaseChanged?.Invoke(this, new OnDatabaseChangedEventArgs(dbInfo.DatabaseType.Name, DateTime.Now));
}
}
catch
{
throw;
}
finally
{
mutex.ReleaseMutex();
if (!mutex.IsNull())
{
mutex.ReleaseMutex();
}
}
}
catch
Expand Down Expand Up @@ -694,7 +763,11 @@ public void ImportDatabase(string fileToImport, string exportPath)
{
try
{
mutex.WaitOne();
if (!mutex.IsNull())
{
mutex.WaitOne();
}

//Opens the zip file up to be read
using (ZipArchive archive = ZipFile.OpenRead(fileToImport))
{
Expand Down Expand Up @@ -727,7 +800,10 @@ public void ImportDatabase(string fileToImport, string exportPath)
}
finally
{
mutex.ReleaseMutex();
if (!mutex.IsNull())
{
mutex.ReleaseMutex();
}
}
}
catch
Expand Down Expand Up @@ -760,7 +836,10 @@ public void ExportDatabase(string pathToSave, string filename, string fileExtens
{
try
{
mutex.WaitOne();
if (!mutex.IsNull())
{
mutex.WaitOne();
}

if (!Directory.Exists(pathToSave))
{
Expand All @@ -779,7 +858,10 @@ public void ExportDatabase(string pathToSave, string filename, string fileExtens
}
finally
{
mutex.ReleaseMutex();
if (!mutex.IsNull())
{
mutex.ReleaseMutex();
}
}
}
catch
Expand Down
8 changes: 2 additions & 6 deletions ReflectXMLDB/ReflectXMLDB.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
<Company></Company>
<Description>Simple database handler based on XML serialization and reflection.

This is a simple project that aims for C#/.NET beginners to achieve database functionality in their applications. The project is written for .NET Standard 2.0, which is compatible with .NET Core 2.0+ and .NET Framework 4.6.1+.</Description>
This is a simple project that aims for C#/.NET developers to achieve database functionality in their applications. The project is written for .NET Standard 2.0, which is compatible with .NET Core 2.0+ and .NET Framework 4.6.1+.</Description>
<Copyright>Copyright© ReflectXMLDB 2018</Copyright>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<AssemblyName>ReflectXMLDB</AssemblyName>
<RootNamespace>ReflectXMLDB</RootNamespace>
<Product>ReflectXMLDB</Product>
<PackageId>ReflectXMLDB</PackageId>
<Version>0.0.2</Version>
<Version>0.0.3</Version>
<PackageLicenseUrl>https://github.com/Fe-Bell/ReflectXMLDB/blob/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/Fe-Bell/ReflectXMLDB</PackageProjectUrl>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
Expand All @@ -28,8 +28,4 @@ This is a simple project that aims for C#/.NET beginners to achieve database fun
<OutputPath>..\bin\Release\netstandard2.0\</OutputPath>
</PropertyGroup>

<ItemGroup>
<Folder Include="Interface\" />
</ItemGroup>

</Project>

0 comments on commit 1176b86

Please sign in to comment.