BlogLayout.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package com.ivalentin.gm;
  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.Bundle;
  9. import android.app.Fragment;
  10. import android.app.FragmentManager;
  11. import android.app.FragmentTransaction;
  12. import android.text.Html;
  13. import android.util.Log;
  14. import android.view.LayoutInflater;
  15. import android.view.View;
  16. import android.widget.Button;
  17. import android.widget.ImageView;
  18. import android.widget.LinearLayout;
  19. import android.view.ViewGroup;
  20. import android.widget.TextView;
  21. import java.io.File;
  22. import java.util.Locale;
  23. /**
  24. * Created by seavenois on 09/06/16.
  25. */
  26. public class BlogLayout extends Fragment{
  27. //Main View
  28. private View view;
  29. private int offset = 0;
  30. private int totalPost = 0;
  31. /**
  32. * Run when the fragment is inflated.
  33. * Assigns views, gets the date and does the first call to the {@link #populate} function.
  34. *
  35. * @param inflater A LayoutInflater to manage views
  36. * @param container The container View
  37. * @param savedInstanceState Bundle containing the state
  38. *
  39. * @return The fragment view
  40. *
  41. * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
  42. */
  43. @SuppressLint("InflateParams") //Throws unknown error when done properly.
  44. @Override
  45. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  46. //Load the layout
  47. view = inflater.inflate(R.layout.fragment_layout_blog, null);
  48. //Set the title
  49. ((MainActivity) getActivity()).setSectionTitle(view.getContext().getString(R.string.menu_blog));
  50. //Get total posts
  51. SQLiteDatabase db = getActivity().openOrCreateDatabase(GM.DB_NAME, Context.MODE_PRIVATE, null);
  52. Cursor cursor = db.rawQuery("SELECT id FROM post;", null);
  53. totalPost = cursor.getCount();
  54. cursor.close();
  55. db.close();
  56. //Assign pager buttons
  57. final Button btnPrevious = (Button) view.findViewById(R.id.bt_blog_previous);
  58. final Button btnNext = (Button) view.findViewById(R.id.bt_blog_next);
  59. btnPrevious.setOnClickListener(new View.OnClickListener() {
  60. @Override
  61. public void onClick(View v) {
  62. offset -= 5;
  63. btnNext.setVisibility(View.VISIBLE);
  64. populate(offset);
  65. if (offset == 0){
  66. v.setVisibility(View.GONE);
  67. }
  68. }
  69. });
  70. btnNext.setOnClickListener(new View.OnClickListener() {
  71. @Override
  72. public void onClick(View v) {
  73. offset += 5;
  74. btnPrevious.setVisibility(View.VISIBLE);
  75. populate(offset);
  76. if (offset >= totalPost - 5){
  77. v.setVisibility(View.GONE);
  78. }
  79. }
  80. });
  81. populate(offset);
  82. return view;
  83. }
  84. /**
  85. * Called when the fragment is brought back into the foreground.
  86. * Resumes the map and the location manager.
  87. *
  88. * @see android.support.v4.app.Fragment#onResume()
  89. */
  90. @Override
  91. public void onResume(){
  92. Log.e("offset", "" + offset);
  93. Log.e("total", "" + totalPost);
  94. //Assign pager buttons
  95. Button btnPrevious = (Button) view.findViewById(R.id.bt_blog_previous);
  96. Button btnNext = (Button) view.findViewById(R.id.bt_blog_next);
  97. if (offset == 0){
  98. btnPrevious.setVisibility(View.GONE);
  99. }
  100. else{
  101. btnPrevious.setVisibility(View.VISIBLE);
  102. }
  103. if (offset >= totalPost - 5){
  104. btnNext.setVisibility(View.GONE);
  105. }
  106. else{
  107. btnNext.setVisibility(View.VISIBLE);
  108. }
  109. super.onResume();
  110. }
  111. public void populate(){
  112. populate(0);
  113. }
  114. /**
  115. * Populates the list of activities around.
  116. * It uses the default layout as parent.
  117. */
  118. public void populate(int offset){
  119. //Assign elements
  120. LinearLayout llList = (LinearLayout) view.findViewById(R.id.ll_blog_list);
  121. LinearLayout entry;
  122. //An inflater
  123. LayoutInflater factory = LayoutInflater.from(getActivity());
  124. SQLiteDatabase db = getActivity().openOrCreateDatabase(GM.DB_NAME, Context.MODE_PRIVATE, null);
  125. final Cursor cursor;
  126. String currLang = Locale.getDefault().getDisplayLanguage();
  127. if (!currLang.equals("es") && !currLang.equals("eu")){
  128. currLang = "en";
  129. }
  130. //Get data from the database
  131. cursor = db.rawQuery("SELECT id, title_" + currLang+ " AS title, text_" + currLang + " AS text, dtime FROM post ORDER BY dtime DESC LIMIT 5 OFFSET " + offset + ";", null);
  132. //Clear the list
  133. llList.removeAllViews();
  134. //Loop
  135. while (cursor.moveToNext()){
  136. //Create a new row
  137. entry = (LinearLayout) factory.inflate(R.layout.row_blog, null);
  138. //Set margins
  139. LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  140. layoutParams.setMargins(10, 10, 10, 25);
  141. entry.setLayoutParams(layoutParams);
  142. //Set title
  143. TextView tvTitle = (TextView) entry.findViewById(R.id.tv_row_blog_title);
  144. tvTitle.setText(cursor.getString(1));
  145. //Set text
  146. String text = Html.fromHtml(cursor.getString(2)).toString();
  147. if (text.length() > 180){
  148. text = text.substring(0, 180) + "...";
  149. }
  150. TextView tvText = (TextView) entry.findViewById(R.id.tv_row_blog_text);
  151. tvText.setText(text);
  152. //Set date
  153. TextView tvDate = (TextView) entry.findViewById(R.id.tv_row_blog_date);
  154. tvDate.setText(GM.formatDate(cursor.getString(3), currLang, false));
  155. //Set hidden id
  156. TextView tvId = (TextView) entry.findViewById(R.id.tv_row_blog_hidden);
  157. tvId.setText(cursor.getString(0));
  158. //Get image
  159. ImageView iv = (ImageView) entry.findViewById(R.id.iv_row_blog_image);
  160. Cursor cursorImage = db.rawQuery("SELECT image FROM post_image WHERE post = " + cursor.getString(0) +" ORDER BY idx LIMIT 1;", null);
  161. if (cursorImage.getCount() > 0){
  162. cursorImage.moveToFirst();
  163. String image = cursorImage.getString(0);
  164. //Check if image exists
  165. File f;
  166. f = new File(this.getActivity().getFilesDir().toString() + "/img/blog/miniature/" + image);
  167. if (f.exists()){
  168. //If the image exists, set it.
  169. Bitmap myBitmap = BitmapFactory.decodeFile(this.getActivity().getFilesDir().toString() + "/img/blog/miniature/" + image);
  170. iv.setImageBitmap(myBitmap);
  171. }
  172. else {
  173. //If not, create directories and download asynchronously
  174. File fpath;
  175. fpath = new File(this.getActivity().getFilesDir().toString() + "/img/blog/miniature/");
  176. fpath.mkdirs();
  177. new DownloadImage(GM.SERVER + "/img/blog/miniature/" + image, this.getActivity().getFilesDir().toString() + "/img/blog/miniature/" + image, iv).execute();
  178. }
  179. }
  180. //Count comments
  181. Cursor cursorComment = db.rawQuery("SELECT * FROM post_comment WHERE post = " + cursor.getString(0) +";", null);
  182. TextView tvDetails = (TextView) entry.findViewById(R.id.tv_row_blog_details);
  183. tvDetails.setText(" " + cursorComment.getCount());
  184. //Set onCLickListener
  185. entry.setOnClickListener(new View.OnClickListener(){
  186. @Override
  187. public void onClick(View v) {
  188. //Toast.makeText(getActivity(), "OPENING POST", Toast.LENGTH_LONG).show();
  189. Fragment fragment = new PostLayout();
  190. Bundle bundle = new Bundle();
  191. //Pass post id
  192. int id = Integer.parseInt(((TextView) v.findViewById(R.id.tv_row_blog_hidden)).getText().toString());
  193. bundle.putInt("post", id);
  194. fragment.setArguments(bundle);
  195. FragmentManager fm = BlogLayout.this.getActivity().getFragmentManager();
  196. FragmentTransaction ft = fm.beginTransaction();
  197. ft.replace(R.id.activity_main_content_fragment, fragment);
  198. ft.addToBackStack("post_" + id);
  199. ft.commit();
  200. }
  201. });
  202. //Add to the list
  203. llList.addView(entry);
  204. }
  205. }
  206. }