Search in sources :

Example 1 with ThreadBody

use of org.edx.mobile.discussion.ThreadBody in project edx-app-android by edx.

the class DiscussionAddPostFragment method onViewCreated.

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    discussionQuestionSegmentedGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            @StringRes final int bodyHint;
            @StringRes final int submitLabel;
            @StringRes final int submitDescription;
            if (discussionQuestionSegmentedGroup.getCheckedRadioButtonId() == R.id.discussion_radio_button) {
                bodyHint = R.string.discussion_body_hint_discussion;
                submitLabel = R.string.discussion_add_post_button_label;
                submitDescription = R.string.discussion_add_post_button_description;
            } else {
                bodyHint = R.string.discussion_body_hint_question;
                submitLabel = R.string.discussion_add_question_button_label;
                submitDescription = R.string.discussion_add_question_button_description;
            }
            bodyEditText.setHint(bodyHint);
            addPostButtonText.setText(submitLabel);
            addPostButton.setContentDescription(getText(submitDescription));
        }
    });
    discussionQuestionSegmentedGroup.check(R.id.discussion_radio_button);
    getTopicList();
    topicsSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            // Even though we disabled topics that aren't supposed to be selected, Android still allows you to select them using keyboard or finger-dragging
            // So, we have to revert the user's selection if they select a topic that cannot be posted to
            final DiscussionTopicDepth item = (DiscussionTopicDepth) parent.getItemAtPosition(position);
            if (null == item || item.isPostable()) {
                selectedTopicIndex = position;
            } else {
                // Revert selection
                parent.setSelection(selectedTopicIndex);
            }
            setPostButtonEnabledState();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            setPostButtonEnabledState();
        }
    });
    ViewCompat.setBackgroundTintList(topicsSpinner, getResources().getColorStateList(R.color.edx_brand_gray_base));
    addPostButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Activity activity = getActivity();
            if (activity != null) {
                SoftKeyboardUtil.hide(activity);
            }
            final String title = titleEditText.getText().toString();
            final String body = bodyEditText.getText().toString();
            final DiscussionThread.ThreadType discussionQuestion;
            if (discussionQuestionSegmentedGroup.getCheckedRadioButtonId() == R.id.discussion_radio_button) {
                discussionQuestion = DiscussionThread.ThreadType.DISCUSSION;
            } else {
                discussionQuestion = DiscussionThread.ThreadType.QUESTION;
            }
            ThreadBody threadBody = new ThreadBody();
            threadBody.setCourseId(courseData.getCourse().getId());
            threadBody.setTitle(title);
            threadBody.setRawBody(body);
            threadBody.setTopicId(((DiscussionTopicDepth) topicsSpinner.getSelectedItem()).getDiscussionTopic().getIdentifier());
            threadBody.setType(discussionQuestion);
            addPostButton.setEnabled(false);
            createThread(threadBody);
        }
    });
    addPostButton.setEnabled(false);
    final TextWatcher textWatcher = new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            setPostButtonEnabledState();
        }
    };
    titleEditText.addTextChangedListener(textWatcher);
    bodyEditText.addTextChangedListener(textWatcher);
}
Also used : RadioGroup(android.widget.RadioGroup) ThreadBody(org.edx.mobile.discussion.ThreadBody) Activity(android.app.Activity) View(android.view.View) AdapterView(android.widget.AdapterView) InjectView(roboguice.inject.InjectView) TextView(android.widget.TextView) DiscussionTopicDepth(org.edx.mobile.discussion.DiscussionTopicDepth) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) AdapterView(android.widget.AdapterView)

Aggregations

Activity (android.app.Activity)1 Editable (android.text.Editable)1 TextWatcher (android.text.TextWatcher)1 View (android.view.View)1 AdapterView (android.widget.AdapterView)1 RadioGroup (android.widget.RadioGroup)1 TextView (android.widget.TextView)1 DiscussionTopicDepth (org.edx.mobile.discussion.DiscussionTopicDepth)1 ThreadBody (org.edx.mobile.discussion.ThreadBody)1 InjectView (roboguice.inject.InjectView)1