Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File scoped namespace #131

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions MediaGallery/ExceptionHelper/ExceptionHelper.shared.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
namespace NativeMedia
namespace NativeMedia;

static class ExceptionHelper
{
static class ExceptionHelper
{
private static Exception NotSupportedOrImplementedException
=> new NotImplementedException("This functionality is not implemented in the portable version of this assembly. " +
"You should reference the NuGet package from your main application project in order to reference the platform-specific implementation.");
private static Exception NotSupportedOrImplementedException
=> new NotImplementedException("This functionality is not implemented in the portable version of this assembly. " +
"You should reference the NuGet package from your main application project in order to reference the platform-specific implementation.");

internal static PermissionException PermissionException(PermissionStatus status)
=> new($"{nameof(SaveMediaPermission)} was not granted: {status}");
internal static PermissionException PermissionException(PermissionStatus status)
=> new($"{nameof(SaveMediaPermission)} was not granted: {status}");

private static bool? isSupported;
private static bool? isSupported;

internal static void CheckSupport()
internal static void CheckSupport()
{
if (!isSupported.HasValue)
{
if (!isSupported.HasValue)
{
isSupported
isSupported
#if __ANDROID__
= Platform.HasSdkVersion(21);
#else
= Platform.HasOSVersion(11);
= Platform.HasOSVersion(11);
#endif
}
if (!isSupported.Value)
throw NotSupportedOrImplementedException;
}
if (!isSupported.Value)
throw NotSupportedOrImplementedException;
}

#if __ANDROID__
internal static Exception ActivityNotDetected
=> new NullReferenceException("The current Activity can not be detected. " +
$"Ensure that you have called Xamarin.Essentials.Platform.Init in your Activity or Application class.");
#else
internal static Exception ControllerNotFound
=> new InvalidOperationException("Could not find current view controller.");
internal static Exception ControllerNotFound
=> new InvalidOperationException("Could not find current view controller.");
#endif
}
}
}
33 changes: 16 additions & 17 deletions MediaGallery/MediaFile/IMediaFile.shared.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
namespace NativeMedia
namespace NativeMedia;

/// <summary>Describes and allows to open a media file</summary>
public interface IMediaFile : IDisposable
{
/// <summary>Describes and allows to open a media file</summary>
public interface IMediaFile : IDisposable
{
/// <summary>Returns the file name without extension. Can return an null or empty value</summary>
string NameWithoutExtension { get; }
/// <summary>Returns the file name without extension. Can return an null or empty value</summary>
string NameWithoutExtension { get; }

/// <summary>Returns the file extension without a dot (eg:, "png").</summary>
string Extension { get; }
/// <summary>Returns the file extension without a dot (eg:, "png").</summary>
string Extension { get; }

/// <summary>Returns the file's content type as a MIME type (eg: "image/png").</summary>
string ContentType { get; }
/// <summary>Returns the file's content type as a MIME type (eg: "image/png").</summary>
string ContentType { get; }

/// <summary>Returns the file type. Can return an null value.</summary>
MediaFileType? Type { get; }
/// <summary>Returns the file type. Can return an null value.</summary>
MediaFileType? Type { get; }

/// <summary>Opens a read-only stream containing the file.</summary>
/// <returns>The task object representing the asynchronous operation. The task returns a Stream used to read data from the file.</returns>
Task<Stream> OpenReadAsync();
}
}
/// <summary>Opens a read-only stream containing the file.</summary>
/// <returns>The task object representing the asynchronous operation. The task returns a Stream used to read data from the file.</returns>
Task<Stream> OpenReadAsync();
}
25 changes: 12 additions & 13 deletions MediaGallery/MediaFile/MediaFile.android.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using Android.Webkit;
using Uri = Android.Net.Uri;

namespace NativeMedia
namespace NativeMedia;

partial class MediaFile
{
partial class MediaFile
{
readonly Uri uri;
readonly string tempFilePath;
readonly Uri uri;
readonly string tempFilePath;

internal MediaFile(string fileName, Uri uri, string tempFilePath = null)
{
internal MediaFile(string fileName, Uri uri, string tempFilePath = null)
{
this.tempFilePath = tempFilePath;
NameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);
Extension = Path.GetExtension(fileName);
Expand All @@ -18,15 +18,14 @@ internal MediaFile(string fileName, Uri uri, string tempFilePath = null)
Type = GetFileType(ContentType);
}

Task<Stream> PlatformOpenReadAsync()
=> Task.FromResult(Platform.AppActivity.ContentResolver.OpenInputStream(uri));
Task<Stream> PlatformOpenReadAsync()
=> Task.FromResult(Platform.AppActivity.ContentResolver.OpenInputStream(uri));

void PlatformDispose()
{
void PlatformDispose()
{
if(!string.IsNullOrWhiteSpace(tempFilePath) && File.Exists(tempFilePath))
File.Delete(tempFilePath);

uri?.Dispose();
}
}
}
}
Loading