BlogLayout.java 8.8 KB

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