PageRenderTime 1469ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/userVoiceSDK/src/main/java/com/uservoice/uservoicesdk/dialog/SuggestionDialogFragment.java

https://gitlab.com/adamlwalker/android-discourse
Java | 201 lines | 179 code | 22 blank | 0 comment | 17 complexity | 5aebf045213828ef1c76fd84a0ec18f6 MD5 | raw file
  1. package com.uservoice.uservoicesdk.dialog;
  2. import android.annotation.SuppressLint;
  3. import android.app.AlertDialog;
  4. import android.app.Dialog;
  5. import android.graphics.Color;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.CheckBox;
  9. import android.widget.ImageView;
  10. import android.widget.ListView;
  11. import android.widget.TextView;
  12. import android.widget.Toast;
  13. import com.uservoice.uservoicesdk.R;
  14. import com.uservoice.uservoicesdk.Session;
  15. import com.uservoice.uservoicesdk.activity.ForumActivity;
  16. import com.uservoice.uservoicesdk.activity.InstantAnswersActivity;
  17. import com.uservoice.uservoicesdk.babayaga.Babayaga;
  18. import com.uservoice.uservoicesdk.deflection.Deflection;
  19. import com.uservoice.uservoicesdk.flow.SigninManager;
  20. import com.uservoice.uservoicesdk.image.ImageCache;
  21. import com.uservoice.uservoicesdk.model.Comment;
  22. import com.uservoice.uservoicesdk.model.Suggestion;
  23. import com.uservoice.uservoicesdk.rest.Callback;
  24. import com.uservoice.uservoicesdk.ui.DefaultCallback;
  25. import com.uservoice.uservoicesdk.ui.PaginatedAdapter;
  26. import com.uservoice.uservoicesdk.ui.PaginationScrollListener;
  27. import com.uservoice.uservoicesdk.ui.Utils;
  28. import java.text.DateFormat;
  29. import java.util.ArrayList;
  30. import java.util.List;
  31. import java.util.Locale;
  32. @SuppressLint("ValidFragment")
  33. public class SuggestionDialogFragment extends DialogFragmentBugfixed {
  34. private Suggestion suggestion;
  35. private PaginatedAdapter<Comment> adapter;
  36. private View headerView;
  37. private View view;
  38. public SuggestionDialogFragment(Suggestion suggestion) {
  39. this.suggestion = suggestion;
  40. }
  41. @Override
  42. public Dialog onCreateDialog(Bundle savedInstanceState) {
  43. AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  44. setStyle(STYLE_NO_TITLE, getTheme());
  45. if (!Utils.isDarkTheme(getActivity())) {
  46. builder.setInverseBackgroundForced(true);
  47. }
  48. view = getActivity().getLayoutInflater().inflate(R.layout.uv_idea_dialog, null);
  49. headerView = getActivity().getLayoutInflater().inflate(R.layout.uv_idea_dialog_header, null);
  50. headerView.findViewById(R.id.uv_subscribe).setOnClickListener(new View.OnClickListener() {
  51. @Override
  52. public void onClick(View v) {
  53. final DefaultCallback<Suggestion> callback = new DefaultCallback<Suggestion>(getActivity()) {
  54. @Override
  55. public void onModel(Suggestion model) {
  56. if (getActivity() instanceof InstantAnswersActivity)
  57. Deflection.trackDeflection("subscribed", model);
  58. suggestionSubscriptionUpdated(model);
  59. }
  60. };
  61. if (suggestion.isSubscribed()) {
  62. suggestion.unsubscribe(callback);
  63. } else {
  64. if (Session.getInstance().getEmail() != null) {
  65. SigninManager.signinForSubscribe(getActivity(), Session.getInstance().getEmail(), new Runnable() {
  66. @Override
  67. public void run() {
  68. suggestion.subscribe(callback);
  69. }
  70. });
  71. } else {
  72. SubscribeDialogFragment dialog = new SubscribeDialogFragment(suggestion, SuggestionDialogFragment.this);
  73. dialog.show(getFragmentManager(), "SubscribeDialogFragment");
  74. }
  75. }
  76. }
  77. });
  78. headerView.findViewById(R.id.uv_post_comment).setOnClickListener(new View.OnClickListener() {
  79. @Override
  80. public void onClick(View v) {
  81. CommentDialogFragment dialog = new CommentDialogFragment(suggestion, SuggestionDialogFragment.this);
  82. dialog.show(getActivity().getSupportFragmentManager(), "CommentDialogFragment");
  83. }
  84. });
  85. ListView listView = (ListView) view.findViewById(R.id.uv_list);
  86. listView.addHeaderView(headerView);
  87. displaySuggestion(view, suggestion);
  88. adapter = getListAdapter();
  89. listView.setAdapter(adapter);
  90. listView.setDivider(null);
  91. listView.setOnScrollListener(new PaginationScrollListener(adapter));
  92. builder.setView(view);
  93. builder.setNegativeButton(R.string.uv_close, null);
  94. Babayaga.track(Babayaga.Event.VIEW_IDEA, suggestion.getId());
  95. return builder.create();
  96. }
  97. public void suggestionSubscriptionUpdated(Suggestion model) {
  98. CheckBox checkbox = (CheckBox) headerView.findViewById(R.id.uv_subscribe_checkbox);
  99. if (suggestion.isSubscribed()) {
  100. Toast.makeText(getActivity(), R.string.uv_msg_subscribe_success, Toast.LENGTH_SHORT).show();
  101. checkbox.setChecked(true);
  102. } else {
  103. Toast.makeText(getActivity(), R.string.uv_msg_unsubscribe, Toast.LENGTH_SHORT).show();
  104. checkbox.setChecked(false);
  105. }
  106. displaySuggestion(view, suggestion);
  107. if (getActivity() instanceof ForumActivity)
  108. ((ForumActivity) getActivity()).suggestionUpdated(model);
  109. }
  110. private PaginatedAdapter<Comment> getListAdapter() {
  111. return new PaginatedAdapter<Comment>(getActivity(), R.layout.uv_comment_item, new ArrayList<Comment>()) {
  112. @Override
  113. protected int getTotalNumberOfObjects() {
  114. return suggestion.getNumberOfComments();
  115. }
  116. @Override
  117. protected void customizeLayout(View view, Comment model) {
  118. TextView textView = (TextView) view.findViewById(R.id.uv_text);
  119. textView.setText(model.getText());
  120. textView = (TextView) view.findViewById(R.id.uv_name);
  121. textView.setText(model.getUserName());
  122. textView = (TextView) view.findViewById(R.id.uv_date);
  123. textView.setText(DateFormat.getDateInstance().format(model.getCreatedAt()));
  124. ImageView avatar = (ImageView) view.findViewById(R.id.uv_avatar);
  125. ImageCache.getInstance().loadImage(model.getAvatarUrl(), avatar);
  126. }
  127. @Override
  128. public boolean isEnabled(int position) {
  129. return false;
  130. }
  131. @Override
  132. protected void loadPage(int page, Callback<List<Comment>> callback) {
  133. Comment.loadComments(suggestion, page, callback);
  134. }
  135. };
  136. }
  137. public void commentPosted(Comment comment) {
  138. adapter.add(0, comment);
  139. suggestion.commentPosted(comment);
  140. displaySuggestion(view, suggestion);
  141. }
  142. private void displaySuggestion(View view, Suggestion suggestion) {
  143. TextView status = (TextView) view.findViewById(R.id.uv_status);
  144. TextView responseStatus = (TextView) view.findViewById(R.id.uv_response_status);
  145. View responseDivider = view.findViewById(R.id.uv_response_divider);
  146. TextView title = (TextView) view.findViewById(R.id.uv_title);
  147. if (suggestion.isSubscribed()) {
  148. ((CheckBox) view.findViewById(R.id.uv_subscribe_checkbox)).setChecked(true);
  149. }
  150. if (suggestion.getStatus() == null) {
  151. status.setVisibility(View.GONE);
  152. int defaultColor = Color.DKGRAY;
  153. responseStatus.setTextColor(defaultColor);
  154. responseDivider.setBackgroundColor(defaultColor);
  155. } else {
  156. int color = Color.parseColor(suggestion.getStatusColor());
  157. status.setBackgroundColor(color);
  158. status.setText(suggestion.getStatus());
  159. responseStatus.setTextColor(color);
  160. responseStatus.setText(String.format(getString(R.string.uv_admin_response_format), suggestion.getStatus().toUpperCase(Locale.getDefault())));
  161. responseDivider.setBackgroundColor(color);
  162. }
  163. title.setText(suggestion.getTitle());
  164. ((TextView) view.findViewById(R.id.uv_text)).setText(suggestion.getText());
  165. ((TextView) view.findViewById(R.id.uv_creator)).setText(String.format(view.getContext().getString(R.string.uv_posted_by_format), suggestion.getCreatorName(), DateFormat.getDateInstance().format(suggestion.getCreatedAt())));
  166. if (suggestion.getAdminResponseText() == null) {
  167. view.findViewById(R.id.uv_admin_response).setVisibility(View.GONE);
  168. } else {
  169. view.findViewById(R.id.uv_admin_response).setVisibility(View.VISIBLE);
  170. ((TextView) view.findViewById(R.id.uv_admin_name)).setText(suggestion.getAdminResponseUserName());
  171. ((TextView) view.findViewById(R.id.uv_response_date)).setText(DateFormat.getDateInstance().format(suggestion.getAdminResponseCreatedAt()));
  172. ((TextView) view.findViewById(R.id.uv_response_text)).setText(suggestion.getAdminResponseText());
  173. ImageView avatar = (ImageView) view.findViewById(R.id.uv_admin_avatar);
  174. ImageCache.getInstance().loadImage(suggestion.getAdminResponseAvatarUrl(), avatar);
  175. }
  176. ((TextView) view.findViewById(R.id.uv_comment_count)).setText(Utils.getQuantityString(view, R.plurals.uv_comments, suggestion.getNumberOfComments()).toUpperCase(Locale.getDefault()));
  177. ((TextView) view.findViewById(R.id.uv_subscriber_count)).setText(String.format(view.getContext().getResources().getQuantityString(R.plurals.uv_number_of_subscribers_format, suggestion.getNumberOfSubscribers()), Utils.getQuantityString(view, R.plurals.uv_subscribers, suggestion.getNumberOfSubscribers())));
  178. }
  179. }