-
Notifications
You must be signed in to change notification settings - Fork 15
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
1 parent
3958ef1
commit de81022
Showing
8 changed files
with
146 additions
and
130 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
71 changes: 71 additions & 0 deletions
71
sample/src/main/java/com/techyourchance/threadposters/FetchDataUseCase.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,71 @@ | ||
package com.techyourchance.threadposters; | ||
|
||
import android.support.annotation.UiThread; | ||
import android.support.annotation.WorkerThread; | ||
|
||
import com.techyourchance.threadposter.UiThreadPoster; | ||
import com.techyourchance.threadposter.BackgroundThreadPoster; | ||
|
||
import java.util.Collections; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
public class FetchDataUseCase { | ||
|
||
public interface Listener { | ||
void onDataFetched(String data); | ||
} | ||
|
||
private final FakeDataRetriever mFakeDataRetriever; | ||
private final BackgroundThreadPoster mBackgroundThreadPoster; | ||
private final UiThreadPoster mUiThreadPoster; | ||
|
||
private final Set<Listener> mListeners = Collections.newSetFromMap( | ||
new ConcurrentHashMap<Listener, Boolean>()); | ||
|
||
public FetchDataUseCase(FakeDataRetriever fakeDataRetriever, | ||
BackgroundThreadPoster backgroundThreadPoster, | ||
UiThreadPoster uiThreadPoster) { | ||
mFakeDataRetriever = fakeDataRetriever; | ||
mBackgroundThreadPoster = backgroundThreadPoster; | ||
mUiThreadPoster = uiThreadPoster; | ||
} | ||
|
||
public void registerListener(Listener listener) { | ||
mListeners.add(listener); | ||
} | ||
|
||
public void unregisterListener(Listener listener) { | ||
mListeners.remove(listener); | ||
} | ||
|
||
public void fetchData() { | ||
// offload work to background thread | ||
mBackgroundThreadPoster.post(new Runnable() { | ||
@Override | ||
public void run() { | ||
fetchDataSync(); | ||
} | ||
}); | ||
} | ||
|
||
@WorkerThread | ||
private void fetchDataSync() { | ||
final String data = mFakeDataRetriever.getData(); | ||
// notify listeners on UI thread | ||
mUiThreadPoster.post(new Runnable() { | ||
@Override | ||
public void run() { | ||
notifySuccess(data); | ||
} | ||
}); | ||
} | ||
|
||
@UiThread | ||
private void notifySuccess(String data) { | ||
for (Listener listener : mListeners) { | ||
listener.onDataFetched(data); | ||
} | ||
} | ||
|
||
} |
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
24 changes: 24 additions & 0 deletions
24
sample/src/main/java/com/techyourchance/threadposters/SampleApplication.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,24 @@ | ||
package com.techyourchance.threadposters; | ||
|
||
import android.app.Application; | ||
|
||
import com.techyourchance.threadposter.BackgroundThreadPoster; | ||
import com.techyourchance.threadposter.UiThreadPoster; | ||
|
||
public class SampleApplication extends Application { | ||
|
||
/* | ||
IMPORTANT: | ||
Both BackgroundThreadPoster and UiThreadPoster should be global objects (single instance). | ||
*/ | ||
private final BackgroundThreadPoster mBackgroundThreadPoster = new BackgroundThreadPoster(); | ||
private final UiThreadPoster mUiThreadPoster = new UiThreadPoster(); | ||
|
||
private final FakeDataRetriever mFakeDataRetriever = new FakeDataRetriever(); | ||
private final FetchDataUseCase mFetchDataUseCase = | ||
new FetchDataUseCase(mFakeDataRetriever, mBackgroundThreadPoster, mUiThreadPoster); | ||
|
||
public FetchDataUseCase getFetchDataUseCase() { | ||
return mFetchDataUseCase; | ||
} | ||
} |
78 changes: 0 additions & 78 deletions
78
sample/src/main/java/com/techyourchance/threadposters/SampleWorker.java
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
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