Instantly share code, notes, and snippets.
Last active
July 6, 2017 14:27
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save su10/b83d8e5962ef0b682eb4218c25cb1732 to your computer and use it in GitHub Desktop.
【Unity】SocialWorkerをObservableに使うラッパークラスを書いた【UniRx】 ref: http://qiita.com/su10/items/d550d6c31306e8720d03
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
using SWorker; | |
using UniRx; | |
namespace Jagapippi.Social | |
{ | |
public static class ObservableSocialWorker | |
{ | |
public static IObservable<Unit> PostTwitterAsync(string message, string url, string imagePath) | |
{ | |
return Observable.Create<Unit>(observer => | |
{ | |
var gate = new object(); | |
var isDisposed = false; | |
SocialWorker.PostTwitter(message, url, imagePath, result => | |
{ | |
lock (gate) | |
{ | |
if (isDisposed) return; | |
if (result == SocialWorkerResult.Success) | |
{ | |
observer.OnNext(Unit.Default); | |
observer.OnCompleted(); | |
} | |
else | |
{ | |
observer.OnError(new SocialWorkerException(result)); | |
} | |
} | |
}); | |
return Disposable.Create(() => | |
{ | |
lock (gate) | |
{ | |
isDisposed = true; | |
} | |
}); | |
}); | |
} | |
public static IObservable<Unit> PostFacebookAsync(string imagePath) | |
{ | |
return Observable.Create<Unit>(observer => | |
{ | |
var gate = new object(); | |
var isDisposed = false; | |
SocialWorker.PostFacebook(imagePath, result => | |
{ | |
lock (gate) | |
{ | |
if (isDisposed) return; | |
if (result == SocialWorkerResult.Success) | |
{ | |
observer.OnNext(Unit.Default); | |
observer.OnCompleted(); | |
} | |
else | |
{ | |
observer.OnError(new SocialWorkerException(result)); | |
} | |
} | |
}); | |
return Disposable.Create(() => | |
{ | |
lock (gate) | |
{ | |
isDisposed = true; | |
} | |
}); | |
}); | |
} | |
public static IObservable<Unit> PostLineAsync(string message, string imagePath) | |
{ | |
return Observable.Create<Unit>(observer => | |
{ | |
var gate = new object(); | |
var isDisposed = false; | |
SocialWorker.PostLine(System.Uri.EscapeUriString(message), imagePath, result => | |
{ | |
lock (gate) | |
{ | |
if (isDisposed) return; | |
if (result == SocialWorkerResult.Success) | |
{ | |
observer.OnNext(Unit.Default); | |
observer.OnCompleted(); | |
} | |
else | |
{ | |
observer.OnError(new SocialWorkerException(result)); | |
} | |
} | |
}); | |
return Disposable.Create(() => | |
{ | |
lock (gate) | |
{ | |
isDisposed = true; | |
} | |
}); | |
}); | |
} | |
public static IObservable<Unit> PostInstagramAsync(string imagePath) | |
{ | |
return Observable.Create<Unit>(observer => | |
{ | |
var gate = new object(); | |
var isDisposed = false; | |
SocialWorker.PostInstagram(imagePath, result => | |
{ | |
lock (gate) | |
{ | |
if (isDisposed) return; | |
if (result == SocialWorkerResult.Success) | |
{ | |
observer.OnNext(Unit.Default); | |
observer.OnCompleted(); | |
} | |
else | |
{ | |
observer.OnError(new SocialWorkerException(result)); | |
} | |
} | |
}); | |
return Disposable.Create(() => | |
{ | |
lock (gate) | |
{ | |
isDisposed = true; | |
} | |
}); | |
}); | |
} | |
public static IObservable<Unit> PostMailAsync( | |
string[] to, | |
string[] cc, | |
string[] bcc, | |
string subject, | |
string message, | |
string imagePath) | |
{ | |
return Observable.Create<Unit>(observer => | |
{ | |
var gate = new object(); | |
var isDisposed = false; | |
SocialWorker.PostMail(to, cc, bcc, subject, message, imagePath, result => | |
{ | |
lock (gate) | |
{ | |
if (isDisposed) return; | |
if (result == SocialWorkerResult.Success) | |
{ | |
observer.OnNext(Unit.Default); | |
observer.OnCompleted(); | |
} | |
else | |
{ | |
observer.OnError(new SocialWorkerException(result)); | |
} | |
} | |
}); | |
return Disposable.Create(() => | |
{ | |
lock (gate) | |
{ | |
isDisposed = true; | |
} | |
}); | |
}); | |
} | |
public static IObservable<Unit> CreateChooserAsync(string message, string imagePath) | |
{ | |
return Observable.Create<Unit>(observer => | |
{ | |
var gate = new object(); | |
var isDisposed = false; | |
SocialWorker.CreateChooser(message, imagePath, result => | |
{ | |
lock (gate) | |
{ | |
if (isDisposed) return; | |
if (result == SocialWorkerResult.Success) | |
{ | |
observer.OnNext(Unit.Default); | |
observer.OnCompleted(); | |
} | |
else | |
{ | |
observer.OnError(new SocialWorkerException(result)); | |
} | |
} | |
}); | |
return Disposable.Create(() => | |
{ | |
lock (gate) | |
{ | |
isDisposed = true; | |
} | |
}); | |
}); | |
} | |
} | |
public class SocialWorkerException : System.Exception | |
{ | |
private readonly SocialWorkerResult _result; | |
public SocialWorkerResult error | |
{ | |
get { return _result; } | |
} | |
public SocialWorkerException(SocialWorkerResult result) | |
{ | |
_result = result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment