use of io.plaidapp.data.api.designernews.model.NewStoryRequest in project plaid by nickbutcher.
the class PostStoryService method onHandleIntent.
@Override
protected void onHandleIntent(Intent intent) {
if (intent == null)
return;
if (ACTION_POST_NEW_STORY.equals(intent.getAction())) {
final boolean broadcastResult = intent.getBooleanExtra(EXTRA_BROADCAST_RESULT, false);
final DesignerNewsPrefs designerNewsPrefs = DesignerNewsPrefs.get(this);
// shouldn't happen...
if (!designerNewsPrefs.isLoggedIn())
return;
final String title = intent.getStringExtra(EXTRA_STORY_TITLE);
final String url = intent.getStringExtra(EXTRA_STORY_URL);
final String comment = intent.getStringExtra(EXTRA_STORY_COMMENT);
if (TextUtils.isEmpty(title))
return;
NewStoryRequest storyToPost = null;
if (!TextUtils.isEmpty(url)) {
storyToPost = NewStoryRequest.createWithUrl(title, url);
} else if (!TextUtils.isEmpty(comment)) {
storyToPost = NewStoryRequest.createWithComment(title, comment);
}
if (storyToPost == null)
return;
final Call<List<Story>> postStoryCall = designerNewsPrefs.getApi().postStory(storyToPost);
try {
final Response<List<Story>> response = postStoryCall.execute();
final List<Story> stories = response.body();
if (stories != null && !stories.isEmpty()) {
if (broadcastResult) {
final Intent success = new Intent(BROADCAST_ACTION_SUCCESS);
// API doesn't fill in author details so add them here
final Story returnedStory = stories.get(0);
final Story.Builder builder = Story.Builder.from(returnedStory).setUserId(designerNewsPrefs.getUserId()).setUserDisplayName(designerNewsPrefs.getUserName()).setUserPortraitUrl(designerNewsPrefs.getUserAvatar());
// API doesn't add a self URL, so potentially add one for consistency
if (TextUtils.isEmpty(returnedStory.url)) {
builder.setDefaultUrl(returnedStory.id);
}
final Story newStory = builder.build();
newStory.dataSource = SOURCE_NEW_DN_POST;
success.putExtra(EXTRA_NEW_STORY, newStory);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(success);
} else {
Toast.makeText(getApplicationContext(), "Story posted", Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
final String reason = e.getMessage();
if (broadcastResult) {
final Intent failure = new Intent(BROADCAST_ACTION_FAILURE);
failure.putExtra(BROADCAST_ACTION_FAILURE_REASON, reason);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(failure);
} else {
Toast.makeText(getApplicationContext(), reason, Toast.LENGTH_SHORT).show();
}
}
}
}
Aggregations