Search in sources :

Example 1 with DesignerNewsPrefs

use of io.plaidapp.data.prefs.DesignerNewsPrefs in project plaid by nickbutcher.

the class UpvoteStoryService method handleActionUpvote.

private void handleActionUpvote(long storyId) {
    if (storyId == 0L)
        return;
    final DesignerNewsPrefs designerNewsPrefs = DesignerNewsPrefs.get(this);
    if (!designerNewsPrefs.isLoggedIn()) {
        // TODO prompt for login
        return;
    }
    final Call<Story> upvoteStoryCall = designerNewsPrefs.getApi().upvoteStory(storyId);
    try {
        final Response<Story> response = upvoteStoryCall.execute();
    // int newVotesCount = response.body().vote_count;
    // TODO report success
    } catch (Exception e) {
    // TODO report failure
    }
}
Also used : DesignerNewsPrefs(io.plaidapp.data.prefs.DesignerNewsPrefs) Story(io.plaidapp.data.api.designernews.model.Story)

Example 2 with DesignerNewsPrefs

use of io.plaidapp.data.prefs.DesignerNewsPrefs 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();
            }
        }
    }
}
Also used : NewStoryRequest(io.plaidapp.data.api.designernews.model.NewStoryRequest) List(java.util.List) Intent(android.content.Intent) DesignerNewsPrefs(io.plaidapp.data.prefs.DesignerNewsPrefs) Story(io.plaidapp.data.api.designernews.model.Story)

Aggregations

Story (io.plaidapp.data.api.designernews.model.Story)2 DesignerNewsPrefs (io.plaidapp.data.prefs.DesignerNewsPrefs)2 Intent (android.content.Intent)1 NewStoryRequest (io.plaidapp.data.api.designernews.model.NewStoryRequest)1 List (java.util.List)1