Amrish-Sharma / FlipQuotes

1 stars 0 forks source link

Use the existing source of quotes #1

Open Amrish-Sharma opened 3 weeks ago

Amrish-Sharma commented 3 weeks ago

Use the big resource of quote to generate the quotes randomly

Amrish-Sharma commented 3 weeks ago

To read the quotes.json file and generate the quotes dynamically in your SlidePageAdapter, we can use the AssetManager to read the JSON file and parse it using a library like Gson. Here are the steps:

  1. Add the Gson dependency to your build.gradle file.
  2. Create a model class for the quotes.
  3. Modify the SlidePageAdapter to read and parse the JSON file.

Step 1: Add Gson Dependency

Add the following dependency to build.gradle file:

dependencies {
    implementation 'com.google.code.gson:gson:2.8.9'
}

Step 2: Create a Model Class

Create a model class for the quotes in a new file Quote.java:

public class Quote {
    private String quote;
    private String author;

    public String getQuote() {
        return quote;
    }

    public String getAuthor() {
        return author;
    }
}

Step 3: Modify SlidePageAdapter

Modify the SlidePageAdapter to read and parse the JSON file:

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Objects;

public class SlidePageAdapter extends PagerAdapter {
    private Context context;
    private List<Quote> quotes;

    SlidePageAdapter(Context context) {
        this.context = context;
        this.quotes = loadQuotesFromAssets();
    }

    private List<Quote> loadQuotesFromAssets() {
        AssetManager assetManager = context.getAssets();
        try (InputStream is = assetManager.open("quotes.json")) {
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            String json = new String(buffer, "UTF-8");
            Gson gson = new Gson();
            Type listType = new TypeToken<List<Quote>>() {}.getType();
            return gson.fromJson(json, listType);
        } catch (IOException e) {
            Log.e("SlidePageAdapter", "Error reading quotes.json", e);
            return null;
        }
    }

    @Override
    public int getCount() {
        return quotes != null ? quotes.size() : 0;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, final int position) {
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = Objects.requireNonNull(layoutInflater).inflate(R.layout.card_layout, container, false);

        final TextView heading = view.findViewById(R.id.subheading);
        final TextView content = view.findViewById(R.id.content);
        final TextView readMore = view.findViewById(R.id.read_more);

        Quote quote = quotes.get(position);
        heading.setText(quote.getAuthor());
        content.setText(quote.getQuote());
        readMore.setText("Read More");

        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }
}

This code will read the quotes.json file from the assets folder, parse it into a list of Quote objects, and use these objects to populate the views in the SlidePageAdapter.