Search in sources :

Example 1 with Story

use of io.plaidapp.data.api.designernews.model.Story in project plaid by nickbutcher.

the class StoryWeigher method weigh.

@Override
public void weigh(List<Story> stories) {
    float maxVotes = 0f;
    float maxComments = 0f;
    for (Story story : stories) {
        maxVotes = Math.max(maxVotes, story.vote_count);
        maxComments = Math.max(maxComments, story.comment_count);
    }
    for (Story story : stories) {
        float weight = 1f - ((((float) story.comment_count) / maxComments) + ((float) story.vote_count / maxVotes)) / 2f;
        story.weight = story.page + weight;
    }
}
Also used : Story(io.plaidapp.data.api.designernews.model.Story)

Example 2 with Story

use of io.plaidapp.data.api.designernews.model.Story 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 3 with Story

use of io.plaidapp.data.api.designernews.model.Story 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)

Example 4 with Story

use of io.plaidapp.data.api.designernews.model.Story in project plaid by nickbutcher.

the class FeedAdapter method createDesignerNewsStoryHolder.

@NonNull
private DesignerNewsStoryHolder createDesignerNewsStoryHolder(ViewGroup parent) {
    final DesignerNewsStoryHolder holder = new DesignerNewsStoryHolder(layoutInflater.inflate(R.layout.designer_news_story_item, parent, false), pocketIsInstalled);
    holder.itemView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            final Story story = (Story) getItem(holder.getAdapterPosition());
            CustomTabActivityHelper.openCustomTab(host, DesignerNewsStory.getCustomTabIntent(host, story, null).build(), Uri.parse(story.url));
        }
    });
    holder.comments.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View commentsView) {
            final Intent intent = new Intent();
            intent.setClass(host, DesignerNewsStory.class);
            intent.putExtra(DesignerNewsStory.EXTRA_STORY, (Story) getItem(holder.getAdapterPosition()));
            ReflowText.addExtras(intent, new ReflowText.ReflowableTextView(holder.title));
            setGridItemContentTransitions(holder.itemView);
            // on return, fade the pocket & comments buttons in
            host.setExitSharedElementCallback(new SharedElementCallback() {

                @Override
                public void onSharedElementStart(List<String> sharedElementNames, List<View> sharedElements, List<View> sharedElementSnapshots) {
                    host.setExitSharedElementCallback(null);
                    notifyItemChanged(holder.getAdapterPosition(), HomeGridItemAnimator.STORY_COMMENTS_RETURN);
                }
            });
            final ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(host, Pair.create((View) holder.title, host.getString(R.string.transition_story_title)), Pair.create(holder.itemView, host.getString(R.string.transition_story_title_background)), Pair.create(holder.itemView, host.getString(R.string.transition_story_background)));
            host.startActivity(intent, options.toBundle());
        }
    });
    if (pocketIsInstalled) {
        // grumble... no xml setter, grumble...
        holder.pocket.setImageAlpha(178);
        holder.pocket.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(final View view) {
                PocketUtils.addToPocket(host, ((Story) getItem(holder.getAdapterPosition())).url);
                // notify changed with a payload asking RV to run the anim
                notifyItemChanged(holder.getAdapterPosition(), HomeGridItemAnimator.ADD_TO_POCKET);
            }
        });
    }
    return holder;
}
Also used : CustomTabsIntent(android.support.customtabs.CustomTabsIntent) Intent(android.content.Intent) List(java.util.List) ArrayList(java.util.ArrayList) SharedElementCallback(android.app.SharedElementCallback) BindView(butterknife.BindView) View(android.view.View) TextView(android.widget.TextView) BadgedFourThreeImageView(io.plaidapp.ui.widget.BadgedFourThreeImageView) RecyclerView(android.support.v7.widget.RecyclerView) Story(io.plaidapp.data.api.designernews.model.Story) ActivityOptions(android.app.ActivityOptions) NonNull(android.support.annotation.NonNull)

Example 5 with Story

use of io.plaidapp.data.api.designernews.model.Story in project plaid by nickbutcher.

the class FeedAdapter method weighItems.

/**
     * Calculate a 'weight' [0, 1] for each data type for sorting. Each data type/source has a
     * different metric for weighing it e.g. Dribbble uses likes etc. but some sources should keep
     * the order returned by the API. Weights are 'scoped' to the page they belong to and lower
     * weights are sorted earlier in the grid (i.e. in ascending weight).
     */
private void weighItems(List<? extends PlaidItem> items) {
    if (items == null || items.isEmpty())
        return;
    PlaidItemSorting.PlaidItemGroupWeigher weigher = null;
    switch(items.get(0).dataSource) {
        // have an expectation about the order they appear in
        case SourceManager.SOURCE_DRIBBBLE_USER_SHOTS:
        case SourceManager.SOURCE_DRIBBBLE_USER_LIKES:
        case SourceManager.SOURCE_PRODUCT_HUNT:
        case PlayerShotsDataManager.SOURCE_PLAYER_SHOTS:
        case PlayerShotsDataManager.SOURCE_TEAM_SHOTS:
            if (naturalOrderWeigher == null) {
                naturalOrderWeigher = new PlaidItemSorting.NaturalOrderWeigher();
            }
            weigher = naturalOrderWeigher;
            break;
        default:
            // regular pattern of items in the grid
            if (items.get(0) instanceof Shot) {
                if (shotWeigher == null)
                    shotWeigher = new ShotWeigher();
                weigher = shotWeigher;
            } else if (items.get(0) instanceof Story) {
                if (storyWeigher == null)
                    storyWeigher = new StoryWeigher();
                weigher = storyWeigher;
            } else if (items.get(0) instanceof Post) {
                if (postWeigher == null)
                    postWeigher = new PostWeigher();
                weigher = postWeigher;
            }
    }
    weigher.weigh(items);
}
Also used : Post(io.plaidapp.data.api.producthunt.model.Post) PlaidItemSorting(io.plaidapp.data.PlaidItemSorting) StoryWeigher(io.plaidapp.data.api.designernews.StoryWeigher) PostWeigher(io.plaidapp.data.api.producthunt.PostWeigher) Shot(io.plaidapp.data.api.dribbble.model.Shot) Story(io.plaidapp.data.api.designernews.model.Story) ShotWeigher(io.plaidapp.data.api.dribbble.ShotWeigher)

Aggregations

Story (io.plaidapp.data.api.designernews.model.Story)5 Intent (android.content.Intent)2 DesignerNewsPrefs (io.plaidapp.data.prefs.DesignerNewsPrefs)2 List (java.util.List)2 ActivityOptions (android.app.ActivityOptions)1 SharedElementCallback (android.app.SharedElementCallback)1 NonNull (android.support.annotation.NonNull)1 CustomTabsIntent (android.support.customtabs.CustomTabsIntent)1 RecyclerView (android.support.v7.widget.RecyclerView)1 View (android.view.View)1 TextView (android.widget.TextView)1 BindView (butterknife.BindView)1 PlaidItemSorting (io.plaidapp.data.PlaidItemSorting)1 StoryWeigher (io.plaidapp.data.api.designernews.StoryWeigher)1 NewStoryRequest (io.plaidapp.data.api.designernews.model.NewStoryRequest)1 ShotWeigher (io.plaidapp.data.api.dribbble.ShotWeigher)1 Shot (io.plaidapp.data.api.dribbble.model.Shot)1 PostWeigher (io.plaidapp.data.api.producthunt.PostWeigher)1 Post (io.plaidapp.data.api.producthunt.model.Post)1 BadgedFourThreeImageView (io.plaidapp.ui.widget.BadgedFourThreeImageView)1