-
Notifications
You must be signed in to change notification settings - Fork 53
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
Showing
21 changed files
with
304 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
|
||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:orientation="vertical" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
|
||
</LinearLayout> |
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<string name="app_name">Ship Faster</string> | ||
<string name="swipe_success">Swipe success</string> | ||
<string name="swipe_failure">Swipe failure</string> | ||
</resources> |
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,6 @@ | ||
package com.squareup.shipfaster; | ||
|
||
import android.app.Activity; | ||
|
||
public class AuthActivity extends Activity { | ||
} |
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,11 @@ | ||
package com.squareup.shipfaster; | ||
|
||
import java.io.Serializable; | ||
|
||
public class Card implements Serializable { | ||
|
||
public static Card fakeCard() { | ||
return new Card(); | ||
} | ||
|
||
} |
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,49 @@ | ||
package com.squareup.shipfaster; | ||
|
||
import com.squareup.otto.Bus; | ||
import java.util.Random; | ||
import javax.inject.Inject; | ||
|
||
public class CardReader { | ||
|
||
@Inject Bus bus; | ||
|
||
private Thread readingThread; | ||
|
||
public void start() { | ||
// Simulating reading of cards | ||
readingThread = new Thread() { | ||
@Override public void run() { | ||
Random random = new Random(); | ||
while(true) { | ||
try { | ||
Thread.sleep(1000); | ||
} catch (InterruptedException e) { | ||
// stop() called | ||
return; | ||
} | ||
|
||
boolean success = random.nextInt(2) == 0; | ||
if (success) { | ||
onSwipeSuccess(Card.fakeCard()); | ||
} else { | ||
onSwipeFailed(); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
public void stop() { | ||
readingThread.interrupt(); | ||
readingThread = null; | ||
} | ||
|
||
private void onSwipeSuccess(Card card) { | ||
bus.postOnMainThread(new SwipeEvent(true, card)); | ||
} | ||
|
||
private void onSwipeFailed() { | ||
bus.postOnMainThread((new SwipeEvent(false, null))); | ||
} | ||
} |
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,44 @@ | ||
package com.squareup.shipfaster; | ||
|
||
import android.app.Activity; | ||
import android.os.Bundle; | ||
import com.squareup.otto.Bus; | ||
import dagger.ObjectGraph; | ||
import java.util.Set; | ||
import javax.inject.Inject; | ||
|
||
public class CartActivity extends Activity { | ||
|
||
@Inject Cart cart; | ||
|
||
@Inject @RegisterOnBus Set<Object> subscribers; | ||
@Inject Bus bus; | ||
@Inject CardReader cardReader; | ||
|
||
@Override protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
|
||
// Creation of the object graph | ||
CartModule module = new CartModule(); | ||
module.setContext(this); | ||
ObjectGraph objectGraph = ObjectGraph.create(module); | ||
objectGraph.inject(this); | ||
|
||
// Registration of bus subscribers | ||
for(Object subscriber : subscribers) { | ||
bus.register(subscriber); | ||
} | ||
|
||
setContentView(R.layout.cart); | ||
} | ||
|
||
@Override protected void onResume() { | ||
super.onResume(); | ||
cardReader.start(); | ||
} | ||
|
||
@Override protected void onPause() { | ||
super.onPause(); | ||
cardReader.stop(); | ||
} | ||
} |
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,41 @@ | ||
package com.squareup.shipfaster; | ||
|
||
import android.content.Context; | ||
import com.squareup.otto.Bus; | ||
import com.squareup.otto.OttoBus; | ||
import com.squareup.shipfaster.log.EventLogger; | ||
import dagger.Module; | ||
import dagger.Provides; | ||
import javax.inject.Singleton; | ||
|
||
import static dagger.Provides.Type.SET; | ||
|
||
@Module(injects = CartActivity.class) | ||
public class CartModule { | ||
|
||
Context context; | ||
|
||
public void setContext(Context context) { | ||
this.context= context; | ||
} | ||
|
||
@Provides Settings provideSettings(FileBackedSettings settings) { | ||
return settings; | ||
} | ||
|
||
@Provides @Singleton Bus provideBus() { | ||
return new OttoBus(); | ||
} | ||
|
||
@Provides(type = SET) @RegisterOnBus Object registerCart(Cart cart) { | ||
return cart; | ||
} | ||
|
||
@Provides(type = SET) @RegisterOnBus Object registerEventLogger(EventLogger logger) { | ||
return logger; | ||
} | ||
|
||
@Provides Context provideContext() { | ||
return context; | ||
} | ||
} |
17 changes: 0 additions & 17 deletions
17
src/main/java/com/squareup/shipfaster/RegisterActivity.java
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,13 @@ | ||
package com.squareup.shipfaster; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.RetentionPolicy.CLASS; | ||
|
||
@Documented @Target({ METHOD, FIELD}) @Retention(CLASS) | ||
public @interface RegisterOnBus { | ||
} |
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,14 @@ | ||
package com.squareup.shipfaster; | ||
|
||
|
||
public class SwipeEvent { | ||
|
||
public final boolean successfulSwipe; | ||
public final Card card; | ||
|
||
public SwipeEvent(boolean successfulSwipe, Card card) { | ||
this.successfulSwipe = successfulSwipe; | ||
this.card = card; | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/squareup/shipfaster/log/EventLogger.java
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,20 @@ | ||
package com.squareup.shipfaster.log; | ||
|
||
import com.squareup.otto.Bus; | ||
import com.squareup.otto.Subscribe; | ||
import com.squareup.shipfaster.SwipeEvent; | ||
import javax.inject.Inject; | ||
|
||
public class EventLogger { | ||
|
||
private final FileLogger fileLogger; | ||
|
||
@Inject EventLogger(FileLogger fileLogger, Bus bus) { | ||
this.fileLogger = fileLogger; | ||
bus.register(this); | ||
} | ||
|
||
@Subscribe public void logSwipe(SwipeEvent event) { | ||
fileLogger.add(new SwipeLog(event.successfulSwipe)); | ||
} | ||
} |
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,14 @@ | ||
package com.squareup.shipfaster.log; | ||
|
||
import javax.inject.Inject; | ||
|
||
public class FileLogger { | ||
|
||
@Inject FileLogger() { | ||
} | ||
|
||
public void add(Log log) { | ||
// Fake logging. | ||
System.out.println(log); | ||
} | ||
} |
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,5 @@ | ||
package com.squareup.shipfaster.log; | ||
|
||
public interface Log { | ||
String getMessage(); | ||
} |
Oops, something went wrong.