| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- package com.ivalentin.gm;
- import android.annotation.SuppressLint;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.location.LocationManager;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.support.v4.app.FragmentManager;
- import android.support.v4.app.FragmentTransaction;
- import android.text.Html;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.view.ViewGroup;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.io.File;
- import java.util.Locale;
- /**
- * Created by seavenois on 09/06/16.
- */
- public class BlogLayout extends Fragment{
- //Main View
- private View view;
- int offset = 0;
- int totalPost = 0;
- /**
- * Run when the fragment is inflated.
- * Assigns views, gets the date and does the first call to the {@link #populate} function.
- *
- * @param inflater A LayoutInflater to manage views
- * @param container The container View
- * @param savedInstanceState Bundle containing the state
- *
- * @return The fragment view
- *
- * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
- */
- @SuppressLint("InflateParams") //Throws unknown error when done properly.
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- //Load the layout
- view = inflater.inflate(R.layout.fragment_layout_blog, null);
- //Set the title
- ((MainActivity) getActivity()).setSectionTitle(view.getContext().getString(R.string.menu_blog));
- //Get total posts
- SQLiteDatabase db = getActivity().openOrCreateDatabase(GM.DB_NAME, Context.MODE_PRIVATE, null);
- Cursor cursor = db.rawQuery("SELECT id FROM post;", null);
- totalPost = cursor.getCount();
- cursor.close();
- db.close();
- //Assign pager buttons
- final Button btnPrevious = (Button) view.findViewById(R.id.bt_blog_previous);
- final Button btnNext = (Button) view.findViewById(R.id.bt_blog_next);
- btnPrevious.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- offset -= 5;
- btnNext.setVisibility(View.VISIBLE);
- populate(offset);
- if (offset == 0){
- v.setVisibility(View.GONE);
- }
- }
- });
- btnNext.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- offset += 5;
- btnPrevious.setVisibility(View.VISIBLE);
- populate(offset);
- if (offset >= totalPost - 5){
- v.setVisibility(View.GONE);
- }
- }
- });
- populate(offset);
- return view;
- }
- /**
- * Called when the fragment is brought back into the foreground.
- * Resumes the map and the location manager.
- *
- * @see android.support.v4.app.Fragment#onResume()
- */
- @Override
- public void onResume(){
- Log.e("offset", "" + offset);
- Log.e("total", "" + totalPost);
- //Assign pager buttons
- Button btnPrevious = (Button) view.findViewById(R.id.bt_blog_previous);
- Button btnNext = (Button) view.findViewById(R.id.bt_blog_next);
- if (offset == 0){
- btnPrevious.setVisibility(View.GONE);
- }
- else{
- btnPrevious.setVisibility(View.VISIBLE);
- }
- if (offset >= totalPost - 5){
- btnNext.setVisibility(View.GONE);
- }
- else{
- btnNext.setVisibility(View.VISIBLE);
- }
- super.onResume();
- }
- public void populate(){
- populate(0);
- }
- /**
- * Populates the list of activities around.
- * It uses the default layout as parent.
- */
- public void populate(int offset){
- //Assign elements
- LinearLayout llList = (LinearLayout) view.findViewById(R.id.ll_blog_list);
- LinearLayout entry;
- //An inflater
- LayoutInflater factory = LayoutInflater.from(getActivity());
- SQLiteDatabase db = getActivity().openOrCreateDatabase(GM.DB_NAME, Context.MODE_PRIVATE, null);
- final Cursor cursor;
- String currLang = Locale.getDefault().getDisplayLanguage();
- if (!currLang.equals("es") && !currLang.equals("eu")){
- currLang = "en";
- }
- //Get data from the database
- 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);
- //Clear the list
- llList.removeAllViews();
- //Loop
- while (cursor.moveToNext()){
- //Create a new row
- entry = (LinearLayout) factory.inflate(R.layout.row_blog, null);
- //Set margins
- LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
- layoutParams.setMargins(10, 10, 10, 25);
- entry.setLayoutParams(layoutParams);
- //Set title
- TextView tvTitle = (TextView) entry.findViewById(R.id.tv_row_blog_title);
- tvTitle.setText(cursor.getString(1));
- //Set text
- String text = Html.fromHtml(cursor.getString(2)).toString();
- if (text.length() > 180){
- text = text.substring(0, 180) + "...";
- }
- TextView tvText = (TextView) entry.findViewById(R.id.tv_row_blog_text);
- tvText.setText(text);
- //Set date
- TextView tvDate = (TextView) entry.findViewById(R.id.tv_row_blog_date);
- tvDate.setText(GM.formatDate(cursor.getString(3), currLang, false));
- //Set hidden id
- TextView tvId = (TextView) entry.findViewById(R.id.tv_row_blog_hidden);
- tvId.setText(cursor.getString(0));
- //Get image
- ImageView iv = (ImageView) entry.findViewById(R.id.iv_row_blog_image);
- Cursor cursorImage = db.rawQuery("SELECT image FROM post_image WHERE post = " + cursor.getString(0) +" ORDER BY idx LIMIT 1;", null);
- if (cursorImage.getCount() > 0){
- cursorImage.moveToFirst();
- String image = cursorImage.getString(0);
- //Check if image exists
- File f;
- f = new File(this.getContext().getFilesDir().toString() + "/img/blog/miniature/" + image);
- if (f.exists()){
- //If the image exists, set it.
- Bitmap myBitmap = BitmapFactory.decodeFile(this.getContext().getFilesDir().toString() + "/img/blog/miniature/" + image);
- iv.setImageBitmap(myBitmap);
- }
- else {
- //If not, create directories and download asynchronously
- File fpath;
- fpath = new File(this.getContext().getFilesDir().toString() + "/img/blog/miniature/");
- fpath.mkdirs();
- new DownloadImage(GM.SERVER + "/img/blog/miniature/" + image, this.getContext().getFilesDir().toString() + "/img/blog/miniature/" + image, iv).execute();
- }
- }
- //Count comments
- Cursor cursorComment = db.rawQuery("SELECT * FROM post_comment WHERE post = " + cursor.getString(0) +";", null);
- TextView tvDetails = (TextView) entry.findViewById(R.id.tv_row_blog_details);
- tvDetails.setText(" " + cursorComment.getCount());
- //Set onCLickListener
- entry.setOnClickListener(new View.OnClickListener(){
- @Override
- public void onClick(View v) {
- //Toast.makeText(getActivity(), "OPENING POST", Toast.LENGTH_LONG).show();
- Fragment fragment = new PostLayout();
- Bundle bundle = new Bundle();
- //Pass post id
- int id = Integer.parseInt(((TextView) v.findViewById(R.id.tv_row_blog_hidden)).getText().toString());
- bundle.putInt("post", id);
- fragment.setArguments(bundle);
- FragmentManager fm = BlogLayout.this.getActivity().getSupportFragmentManager();
- FragmentTransaction ft = fm.beginTransaction();
- ft.replace(R.id.activity_main_content_fragment, fragment);
- ft.addToBackStack("post_" + id);
- ft.commit();
- }
- });
- //Add to the list
- llList.addView(entry);
- }
- }
- }
|