Skip to content

Instantly share code, notes, and snippets.

View bearprada's full-sized avatar
🎯
Focusing

PRADA Hsiung bearprada

🎯
Focusing
  • Facebook
  • London, UK
View GitHub Profile

Scope (large -> small) : Repository > Store > Factory > Others

Class Name Read Write Comment
store Y Y Provide cache machanism. imply signlton pattern
repository Y Y the same as store, but repository have more features rather than the read/write data, e.g, update/delete data, provide metadata to archive some business goal, e,g. latest update time, sorting the items based on some rules.
source Y N get the results when calling it
provider Y N provide the real implementation, we use it when we try to inject the dependencies(eliminate it)
factory Y N imply Factory pattern, reduce the complexity of creating objects
@bearprada
bearprada / TestDelayAndFlatMap.kt
Created January 2, 2019 08:06
Test Delay with FlatMap operator
fun `test delay`() {
Observable.just(listOf(1,2,3,4,5))
.flatMap { nums ->
val obs = mutableListOf<Observable<Int>>()
for (i in nums) {
obs.add(Observable.just(i).delay(i.toLong(), TimeUnit.MILLISECONDS))
}
Observable.merge(obs)
}
.subscribe { println("$it") }
@bearprada
bearprada / Test.kt
Last active January 2, 2019 07:30
TakeUntil and TakeLast Behavior
fun unsubsribeFirst() {
val input = PublishSubject.create<Int>()
val lifetime = CompletableSubject.create()
input.ignoreElements().subscribe(lifetime)
input.takeLast(2)
.subscribeUntil(lifetime) { println("$it") }
println("start")
fun showGDPRConsentDialog() {
InMobiSdk.updateGDPRConsent(getGDPRJson(false))
// Launch GDPR concent dialog
}
fun clickAgreeButton() {
InMobiSdk.updateGDPRConsent(getGDPRJson(true))
}
fun clickDisagreeButton() {
public class GenericApp {
private DataSource<FacebookItem> mFbSource = new FacebookDataSource();
private DataSource<InstagramItem> mIgSource = new InstagramDataSource();
public DataSource<? extends Item> getDataSource(int section) {
switch (section) {
case 0:
return mIgSource;
case 1:
default:
private Task<WeatherResult> queryWeather() {
final TaskCompletionSource<WeatherResult> task = new TaskCompletionSource<>();
Awareness.SnapshotApi.getWeather(mApiClient)
.setResultCallback(new ResultCallback<WeatherResult>() {
@Override
public void onResult(@NonNull WeatherResult weatherResult) {
if (!weatherResult.getStatus().isSuccess()) {
task.setError(new IllegalStateException("Could not get weather."));
return;
}
Giffle.GiffleBuilder builder = new Giffle.GiffleBuilder()
.size(320, 480) // otuput image size 320x480
.delay(100) // 100 ms
.file(File.createTempFile("PicCollage", "gif"));
final Giffle encoder = builder.build();
List<Bitmap> bitmaps = getOutputBitmaps(); // please impelemnt this block by youself
int[] colors = getTrainingPixels(bitmaps, pw, ph, totalFrame, frameMs); // this part is tricky
encoder.GenPalette(colors.length, colors);
for (Bitmap bm : bitmaps) {
encoder.AddFrame(bm);
/**
* Background database for lookups of fittest color to represent.
*/
private static List<Pair<Integer, String>> sBackgrounds = new ArrayList<>();
static {
// Color Tickets: ------------A-R-G-B-
sBackgrounds.add(new Pair<>(0xFF040404, "background_01.png"));
sBackgrounds.add(new Pair<>(0xFF040404, "background_02.png"));
sBackgrounds.add(new Pair<>(0xFFF2F2F2, "background_03.png"));
...
RxView.clicks(v.findViewById(R.id.btn_detail_new_image)).map(aVoid -> "Event_Click_Detail_New_Image")
.mergeWith(RxView.clicks(v.findViewById(R.id.btn_detail_comment)).map(aVoid -> "Event_Click_Detail_Comment"))
.mergeWith(RxView.clicks(v.findViewById(R.id.btn_detail_map)).map(aVoid -> "Event_Click_Detail_Map"))
.mergeWith(RxView.clicks(v.findViewById(R.id.btn_detail_report)).map(aVoid -> "Event_Click_Detail_Report"))
.mergeWith(RxView.clicks(v.findViewById(R.id.btn_detail_share)).map(aVoid -> "Event_Click_Detail_Share"))
.mergeWith(RxView.clicks(v.findViewById(R.id.btn_detail_like_text)).map(aVoid -> "Event_Click_Detail_Like"))
.mergeWith(RxView.clicks(v.findViewById(R.id.btn_detail_share_text)).map(aVoid -> "Event_Click_Detail_Share"))
.mergeWith(RxView.clicks(v.findViewById(R.id.detail_story)).map(aVoid -> "Event_Click_Detail_Story"))
.subscribe(s -> FlurryAgent.logEvent(s, false));
@bearprada
bearprada / sample_gson_deserializer
Created June 14, 2015 03:39
the sample code to use JsonDeserializer to handle the nest/complicate JSON format
/**
* Example :
* input : {server: { result : { data: { a:1, b:2 }} }}
**/
public class MyDeserializer implements JsonDeserializer<MyClass> {
public MyClass deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
JsonElement j = json.getAsJsonObject().get("server").getAsJsonObject().get("result").getAsJsonObject().get("data");
MyClass clz = new MyClass();