-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Simon Oxtoby
committed
Oct 25, 2020
1 parent
fad2669
commit 8a55d73
Showing
8 changed files
with
186 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
|
||
namespace SlackNet.Blocks | ||
{ | ||
/// <summary> | ||
/// An element which allows selection of a time of day. | ||
/// </summary> | ||
[SlackType("timepicker")] | ||
public class TimePicker : ActionElement, IInputBlockElement | ||
{ | ||
public TimePicker() : base("timepicker") { } | ||
|
||
/// <summary> | ||
/// A plain text object that defines the placeholder text shown on the timepicker. | ||
/// </summary> | ||
public PlainText Placeholder { get; set; } | ||
|
||
/// <summary> | ||
/// The initial time that is selected when the element is loaded. | ||
/// </summary> | ||
public TimeSpan? InitialTime { get; set; } | ||
} | ||
|
||
[SlackType("timepicker")] | ||
public class TimePickerAction : BlockAction | ||
{ | ||
public TimeSpan? SelectedTime { get; set; } | ||
} | ||
|
||
[SlackType("timepicker")] | ||
public class TimePickerValue : ElementValue | ||
{ | ||
public TimeSpan? SelectedTime { get; set; } | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Globalization; | ||
using System.Reflection; | ||
using Newtonsoft.Json; | ||
|
||
namespace SlackNet | ||
{ | ||
abstract class JsonConverter<T> : JsonConverter | ||
{ | ||
public override bool CanConvert(Type objectType) => | ||
typeof(T).GetTypeInfo().IsAssignableFrom(UnderlyingType(objectType).GetTypeInfo()); | ||
|
||
protected static Type UnderlyingType(Type objectType) => | ||
IsNullable(objectType) | ||
? Nullable.GetUnderlyingType(objectType) | ||
: objectType; | ||
|
||
protected static bool IsNullable(Type objectType) | ||
{ | ||
var typeInfo = objectType.GetTypeInfo(); | ||
return typeInfo.IsGenericType | ||
&& typeInfo.GetGenericTypeDefinition() == typeof(Nullable<>); | ||
} | ||
|
||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
if (reader.TokenType == JsonToken.Null) | ||
return IsNullable(objectType) | ||
? (object)null | ||
: throw new JsonSerializationException(string.Format(CultureInfo.InvariantCulture, "Cannot convert null value to {0}.", objectType)); | ||
|
||
return ReadJsonValue(reader, objectType, existingValue == null ? default : (T)existingValue, existingValue != null, serializer); | ||
} | ||
|
||
protected abstract T ReadJsonValue(JsonReader reader, Type objectType, T existingValue, bool hasExistingValue, JsonSerializer serializer); | ||
} | ||
} |
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,30 @@ | ||
using System; | ||
using System.Globalization; | ||
using Newtonsoft.Json; | ||
|
||
namespace SlackNet | ||
{ | ||
class TimeSpanConverter : JsonConverter<TimeSpan> | ||
{ | ||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
var timeSpan = (TimeSpan?)value; | ||
if (timeSpan.HasValue) | ||
writer.WriteValue(timeSpan.Value.ToString("hh\\:mm")); | ||
else | ||
writer.WriteNull(); | ||
} | ||
|
||
protected override TimeSpan ReadJsonValue(JsonReader reader, Type objectType, TimeSpan existingValue, bool hasExistingValue, JsonSerializer serializer) | ||
{ | ||
try | ||
{ | ||
return TimeSpan.Parse(reader.Value.ToString()); | ||
} | ||
catch (Exception ex) | ||
{ | ||
throw new JsonSerializationException(string.Format(CultureInfo.InvariantCulture, "Error converting value {0} to type '{1}'", reader.Value, objectType), ex); | ||
} | ||
} | ||
} | ||
} |