PostLayout.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package com.ivalentin.margolariak;
  2. import android.annotation.SuppressLint;
  3. import android.content.Context;
  4. import android.database.Cursor;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.graphics.Bitmap;
  7. import android.graphics.BitmapFactory;
  8. import android.os.Build;
  9. import android.os.Bundle;
  10. import android.app.Fragment;
  11. import android.util.Log;
  12. import android.view.LayoutInflater;
  13. import android.view.View;
  14. import android.view.ViewGroup;
  15. import android.webkit.WebView;
  16. import android.widget.Button;
  17. import android.widget.EditText;
  18. import android.widget.ImageView;
  19. import android.widget.LinearLayout;
  20. import android.widget.TextView;
  21. import android.widget.Toast;
  22. import java.io.File;
  23. /**
  24. * Section that shows a post of the blog, with details and comments.
  25. * Contains info from almost every other section.
  26. *
  27. * @author Iñigo Valentin
  28. *
  29. * @see Fragment
  30. *
  31. */
  32. public class PostLayout extends Fragment {
  33. //Main View
  34. private View view;
  35. private Context context;
  36. @SuppressLint("InflateParams") //Throws unknown error when done properly.
  37. @SuppressWarnings("ResultOfMethodCallIgnored")
  38. @Override
  39. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  40. //Load the layout
  41. view = inflater.inflate(R.layout.fragment_layout_post, null);
  42. context = view.getContext();
  43. //Get bundled id
  44. Bundle bundle = this.getArguments();
  45. final int id = bundle.getInt("post", -1);
  46. if (id == -1){
  47. Log.e("Post error", "No such post: " + id);
  48. this.getActivity().onBackPressed();
  49. }
  50. //Get data from database
  51. SQLiteDatabase db = SQLiteDatabase.openDatabase(getActivity().getDatabasePath(GM.DB.NAME).getAbsolutePath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY);
  52. final Cursor cursor;
  53. String lang = GM.getLang();
  54. cursor = db.rawQuery("SELECT id, title_" + lang+ " AS title, text_" + lang + " AS text, dtime, permalink FROM post WHERE id = " + id + ";", null);
  55. cursor.moveToFirst();
  56. //Get display elements
  57. ImageView images[] = new ImageView[5];
  58. images[0] = (ImageView) view.findViewById(R.id.iv_post_0);
  59. images[1] = (ImageView) view.findViewById(R.id.iv_post_1);
  60. images[2] = (ImageView) view.findViewById(R.id.iv_post_2);
  61. images[3] = (ImageView) view.findViewById(R.id.iv_post_3);
  62. images[4] = (ImageView) view.findViewById(R.id.iv_post_4);
  63. TextView tvTitle = (TextView) view.findViewById(R.id.tv_post_title);
  64. TextView tvDate = (TextView) view.findViewById(R.id.tv_post_date);
  65. final TextView tvComments = (TextView) view.findViewById(R.id.tv_comments);
  66. WebView wvText = (WebView) view.findViewById(R.id.wv_post_text);
  67. if (Build.VERSION.SDK_INT >= 19) {
  68. wvText.setLayerType(View.LAYER_TYPE_HARDWARE, null);
  69. }
  70. else {
  71. wvText.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
  72. }
  73. LinearLayout llCommentList = (LinearLayout) view.findViewById(R.id.ll_comment_list);
  74. //Set fields
  75. tvTitle.setText(cursor.getString(1));
  76. ((MainActivity) getActivity()).setSectionTitle(cursor.getString(1));
  77. ((MainActivity) getActivity()).setShareLink(String.format(getString(R.string.share_with_title), cursor.getString(1)), GM.SHARE.BLOG + cursor.getString(4));
  78. wvText.loadDataWithBaseURL(null, cursor.getString(2), "text/html", "utf-8", null);
  79. tvDate.setText(GM.formatDate(cursor.getString(3), lang, true));
  80. //Get images
  81. Cursor imageCursor = db.rawQuery("SELECT image, idx FROM post_image WHERE post = " + id + " ORDER BY idx LIMIT 5;", null);
  82. int i = 0;
  83. while (imageCursor.moveToNext()) {
  84. String image = imageCursor.getString(0);
  85. //Check if image exists
  86. File f;
  87. f = new File(this.getActivity().getFilesDir().toString() + "/img/blog/preview/" + image);
  88. if (f.exists()){
  89. //If the image exists, set it.
  90. Bitmap myBitmap = BitmapFactory.decodeFile(this.getActivity().getFilesDir().toString() + "/img/blog/preview/" + image);
  91. images[i].setImageBitmap(myBitmap);
  92. }
  93. else {
  94. //If not, create directories and download asynchronously
  95. File fpath;
  96. fpath = new File(this.getActivity().getFilesDir().toString() + "/img/blog/preview/");
  97. fpath.mkdirs();
  98. new DownloadImage(GM.API.SERVER + "/img/blog/preview/" + image, this.getActivity().getFilesDir().toString() + "/img/blog/preview/" + image, images[i], GM.IMG.SIZE.PREVIEW).execute();
  99. }
  100. images[i].setVisibility(View.VISIBLE);
  101. i ++;
  102. }
  103. imageCursor.close();
  104. //Get comments
  105. Cursor commentCursor = db.rawQuery("SELECT text, dtime, username FROM post_comment WHERE post = " + id + ";", null);
  106. tvComments.setText(String.format(getResources().getQuantityString(R.plurals.comment_comments, commentCursor.getCount()), commentCursor.getCount()));
  107. //final int commentCount = commentCursor.getCount();
  108. cursor.moveToFirst();
  109. LinearLayout entry;
  110. LayoutInflater factory = LayoutInflater.from(getActivity());
  111. while (commentCursor.moveToNext()) {
  112. entry = (LinearLayout) factory.inflate(R.layout.row_comment, null);
  113. //Set user
  114. TextView tvUser = (TextView) entry.findViewById(R.id.tv_row_comment_user);
  115. tvUser.setText(commentCursor.getString(2));
  116. TextView tvCDate = (TextView) entry.findViewById(R.id.tv_row_comment_date);
  117. tvCDate.setText(GM.formatDate(commentCursor.getString(1), lang, true));
  118. TextView tvText = (TextView) entry.findViewById(R.id.tv_row_comment_text);
  119. tvText.setText(commentCursor.getString(0));
  120. //Add to the list
  121. llCommentList.addView(entry);
  122. }
  123. commentCursor.close();
  124. //Set up comment form
  125. final EditText etCommentText = (EditText) view.findViewById(R.id.et_comment_text);
  126. final EditText etCommentName = (EditText) view.findViewById(R.id.et_comment_name);
  127. Button btSendComment = (Button) view.findViewById(R.id.bt_comment_send);
  128. btSendComment.setOnClickListener(new View.OnClickListener(){
  129. @Override
  130. public void onClick(View v) {
  131. String user = etCommentName.getText().toString();
  132. String text = etCommentText.getText().toString();
  133. if (user.length() < 1){
  134. Toast.makeText(getActivity(), getString(R.string.comment_toast_user), Toast.LENGTH_LONG).show();
  135. etCommentName.requestFocus();
  136. return;
  137. }
  138. if (text.length() < 1){
  139. Toast.makeText(getActivity(), getString(R.string.comment_toast_text), Toast.LENGTH_LONG).show();
  140. etCommentText.requestFocus();
  141. return;
  142. }
  143. //Start async task
  144. LinearLayout form = (LinearLayout) view.findViewById(R.id.ll_new_comment);
  145. LinearLayout list = (LinearLayout) view.findViewById(R.id.ll_comment_list);
  146. String lang = GM.getLang();
  147. new PostComment("blog", user, text, lang, id, form, list, context).execute();
  148. }
  149. });
  150. cursor.close();
  151. db.close();
  152. return view;
  153. }
  154. }