forked from imurvai/brickcontroller2
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Export / import a creation via QR code (imurvai#61)
* Export / import a creation via QR code * Resolve warnings * Finetune config + doc + DE resources
- Loading branch information
Showing
31 changed files
with
652 additions
and
12 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
8 changes: 8 additions & 0 deletions
8
BrickController2/BrickController2.Android/PlatformServices/Permission/CameraPermission.cs
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,8 @@ | ||
using BrickController2.PlatformServices.Permission; | ||
using static Microsoft.Maui.ApplicationModel.Permissions; | ||
|
||
namespace BrickController2.Droid.PlatformServices.Permission; | ||
|
||
internal class CameraPermission : Camera, ICameraPermission | ||
{ | ||
} |
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
8 changes: 8 additions & 0 deletions
8
BrickController2/BrickController2.iOS/PlatformServices/Permission/CameraPermission.cs
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,8 @@ | ||
using BrickController2.PlatformServices.Permission; | ||
using static Microsoft.Maui.ApplicationModel.Permissions; | ||
|
||
namespace BrickController2.iOS.PlatformServices.Permission; | ||
|
||
internal class CameraPermission : Camera, ICameraPermission | ||
{ | ||
} |
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
113 changes: 113 additions & 0 deletions
113
BrickController2/BrickController2/CreationManagement/Sharing/ShareablePayloadConverter.cs
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,113 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.IO; | ||
using System.IO.Compression; | ||
using System.Text; | ||
|
||
namespace BrickController2.CreationManagement.Sharing; | ||
|
||
internal class ShareablePayloadConverter<TModel> : JsonConverter<ShareablePayload<TModel>> | ||
where TModel : class, IShareable | ||
{ | ||
// reasonable value to optimize both json text readibility and pixel size of rendered QR | ||
private const int MaxSize = 1024; | ||
private readonly JsonSerializerSettings _settings; | ||
|
||
public ShareablePayloadConverter(JsonSerializerSettings settings) | ||
{ | ||
_settings = settings; | ||
} | ||
|
||
public override ShareablePayload<TModel> ReadJson(JsonReader reader, Type objectType, ShareablePayload<TModel>? existingValue, bool hasExistingValue, JsonSerializer serializer) | ||
{ | ||
if (reader.TokenType != JsonToken.StartObject) | ||
throw new JsonException($"Incorrect payload format. TokenType:{reader.TokenType}"); | ||
|
||
TModel payload = default!; | ||
|
||
while (reader.Read()) | ||
{ | ||
if (reader.TokenType == JsonToken.EndObject) | ||
return new(payload); | ||
|
||
if (reader.TokenType != JsonToken.PropertyName) | ||
throw new JsonException($"Incorrect payload format. TokenType:{reader.TokenType}"); | ||
|
||
var propertyName = reader.Value?.ToString(); | ||
switch (propertyName) | ||
{ | ||
case ShareablePayload<TModel>.ContentTypeProperty: | ||
// validate expected content type | ||
var contentType = reader.ReadAsString(); | ||
if (contentType != TModel.Type) | ||
throw new JsonException($"Unsuppported content type: {contentType}."); | ||
break; | ||
case ShareablePayload<TModel>.PayloadProperty: | ||
// load payload | ||
if (reader.Read()) | ||
{ | ||
payload = DeserializePayload(reader, serializer); | ||
} | ||
break; | ||
|
||
default: | ||
// unknown property | ||
throw new JsonException($"Unexpected property: {propertyName}."); | ||
} | ||
} | ||
|
||
throw new JsonException("Incorrect payload format."); | ||
} | ||
|
||
private static TModel DeserializePayload(JsonReader reader, JsonSerializer serializer) | ||
{ | ||
switch (reader.TokenType) | ||
{ | ||
case JsonToken.StartObject: | ||
// directly deserialize payload | ||
return serializer.Deserialize<TModel>(reader)!; | ||
|
||
case JsonToken.String: | ||
// unzip Base64 payload string | ||
{ | ||
using var input = new MemoryStream(Convert.FromBase64String((string)reader.Value!)); | ||
using var unzip = new GZipStream(input, CompressionMode.Decompress); | ||
using var json = new StreamReader(unzip); | ||
return (TModel)serializer.Deserialize(json, typeof(TModel))!; | ||
} | ||
|
||
default: | ||
throw new JsonException($"Unexpected token type: {reader.TokenType}."); | ||
} | ||
} | ||
|
||
public override void WriteJson(JsonWriter writer, ShareablePayload<TModel>? value, JsonSerializer serializer) | ||
{ | ||
writer.WriteStartObject(); | ||
// write content type of the model | ||
writer.WritePropertyName(ShareablePayload<TModel>.ContentTypeProperty); | ||
writer.WriteValue(TModel.Type); | ||
// write payload based on size autodetection | ||
var payload = JsonConvert.SerializeObject(value!.Payload, _settings); | ||
|
||
writer.WritePropertyName(ShareablePayload<TModel>.PayloadProperty); | ||
|
||
// autodetect final format based on source payload size | ||
if (payload.Length < MaxSize) | ||
{ | ||
writer.WriteRawValue(payload); | ||
} | ||
else | ||
{ | ||
using var output = new MemoryStream(); | ||
using var zip = new GZipStream(output, CompressionMode.Compress); | ||
zip.Write(Encoding.UTF8.GetBytes(payload)); | ||
zip.Flush(); | ||
|
||
// zipped byte[] is writen as base64 | ||
writer.WriteValue(output.ToArray()); | ||
} | ||
|
||
writer.WriteEndObject(); | ||
} | ||
} |
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
10 changes: 10 additions & 0 deletions
10
BrickController2/BrickController2/PlatformServices/Permission/ICameraPermission.cs
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,10 @@ | ||
using Microsoft.Maui.ApplicationModel; | ||
using System.Threading.Tasks; | ||
|
||
namespace BrickController2.PlatformServices.Permission; | ||
|
||
public interface ICameraPermission | ||
{ | ||
Task<PermissionStatus> CheckStatusAsync(); | ||
Task<PermissionStatus> RequestAsync(); | ||
} |
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
Oops, something went wrong.