<commit_before>package com.koroshiya;

import android.content.Context;
import android.os.Build;
import android.support.design.widget.Snackbar;
import android.util.Log;
import android.view.View;

import com.koroshiya.archive.steppable.SteppableArchive;
import com.koroshiya.async.DecodeArchiveStreamAsync;
import com.koroshiya.async.DecodeAsync;
import com.koroshiya.async.DecodeBitmapFileAsync;
import com.koroshiya.async.GenerateThumbnailAsync;
import com.koroshiya.controls.JBitmapDrawable;
import com.koroshiya.fragments.ReadFragment;
import com.koroshiya.io_utils.ArchiveParser;
import com.koroshiya.io_utils.ImageParser;
import com.koroshiya.settings.SettingsManager;
import com.koroshiya.settings.classes.Recent;

import java.io.File;
import java.io.IOException;
import java.lang.ref.SoftReference;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ReadCache {

    private final File f;
    private int currentPage;
    private int totalPages = 0;
    private final ReadFragment fragment;
    private final SteppableArchive archive;
    private SoftReference<JBitmapDrawable> cacheForward = new SoftReference<>(null);
    private SoftReference<JBitmapDrawable> cacheBackward = new SoftReference<>(null);
    private Recent recent;
    private boolean parsingInitial = true;

    public static final int CACHE_DIRECT = 0;
    private static final int CACHE_FORWARD = 1;
    private static final int CACHE_BACKWARD = 2;

    public ReadCache(File f, int page, ReadFragment fragment) throws IOException {

        Context c;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            c = fragment.getContext();
        }else{
            c = fragment.getActivity();
        }

        if (ArchiveParser.isSupportedArchive(f)){
            archive = ArchiveParser.parseArchive(f, c);
            totalPages = archive.getTotalPages();
        }else{
            archive = null;
            if (f.isFile()) f = f.getParentFile();
            for (File file : f.listFiles()){
                if (ImageParser.isSupportedImage(file)){
                    totalPages++;
                }
            }
        }

        this.f = f;
        this.currentPage = page;
        this.fragment = fragment;

        Log.i("ReadCache", "Initiated with file: "+f.getAbsolutePath());
        Log.i("ReadCache", "Initiated with page: "+page);

    }

    public void parseInitial(Context c, View v){
        parseImage(c, CACHE_DIRECT, currentPage, v);
    }

    private void parseImage(Context c, int cacheType, int page, View v){
        Log.i("parseImage", "page: "+page);
        DecodeAsync async;
        if (archive != null){
            async = new DecodeArchiveStreamAsync(c, this, cacheType, archive, v);
        }else {
            async = new DecodeBitmapFileAsync(c, this, cacheType, v);
        }
        async.execute(f.getAbsolutePath(), Integer.toString(page));
    }

    private int getTotalPages(){
        return this.totalPages;
    }

    public String getFileName(){
        return this.f.getAbsolutePath();
    }

    public int getCurrentPage(){
        return this.currentPage;
    }

    public void setImage(SoftReference<JBitmapDrawable> image, int cacheType, String msg) {
        switch (cacheType){
            case CACHE_DIRECT:
                fragment.setImage(image.get());
                setThumb(image);
                fragment.hideProgress();

                if (cacheForward.get() == null || cacheBackward.get() == null){
                    cacheNext(fragment.getActivity());
                }
                if (parsingInitial){
                    if (msg != null){
                        View v = fragment.getActivity().findViewById(R.id.drawer_layout);
                        if (v != null){
                            Snackbar.make(v, msg, Snackbar.LENGTH_SHORT).show();
                        }
                    }
                    parsingInitial = false;
                }

                break;
            case CACHE_FORWARD:
                cacheForward = image;
                break;
            case CACHE_BACKWARD:
                cacheBackward = image;
                break;
        }
    }

    private void setThumb(SoftReference<JBitmapDrawable> image){

        if (image != null && image.get() != null) {

            Context c;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                c = fragment.getContext();
            } else {
                c = fragment.getActivity();
            }

            if (recent == null) {
                recent = Recent.get(c, f.getAbsolutePath(), true);
            }

            if (recent != null) {
                recent.setPageNumber(c, currentPage);
            } else {
                JBitmapDrawable jbd = image.get();
                recent = new Recent(f.getAbsolutePath(), currentPage);
                GenerateThumbnailAsync async = new GenerateThumbnailAsync(c);
                async.execute(
                        f.getAbsolutePath(),
                        c.getCacheDir().getAbsolutePath(),
                        Long.toString(recent.getUuid()),
                        Integer.toString(jbd.getWidth()),
                        Integer.toString(jbd.getHeight())<commit_msg>Fix for page no longer being remembered<commit_after>package com.koroshiya;

import android.content.Context;
import android.os.Build;
import android.support.design.widget.Snackbar;
import android.util.Log;
import android.view.View;

import com.koroshiya.archive.steppable.SteppableArchive;
import com.koroshiya.async.DecodeArchiveStreamAsync;
import com.koroshiya.async.DecodeAsync;
import com.koroshiya.async.DecodeBitmapFileAsync;
import com.koroshiya.async.GenerateThumbnailAsync;
import com.koroshiya.controls.JBitmapDrawable;
import com.koroshiya.fragments.ReadFragment;
import com.koroshiya.io_utils.ArchiveParser;
import com.koroshiya.io_utils.ImageParser;
import com.koroshiya.settings.SettingsManager;
import com.koroshiya.settings.classes.Recent;

import java.io.File;
import java.io.IOException;
import java.lang.ref.SoftReference;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ReadCache {

    private final File f;
    private int currentPage;
    private int totalPages = 0;
    private final ReadFragment fragment;
    private final SteppableArchive archive;
    private SoftReference<JBitmapDrawable> cacheForward = new SoftReference<>(null);
    private SoftReference<JBitmapDrawable> cacheBackward = new SoftReference<>(null);
    private Recent recent;
    private boolean parsingInitial = true;

    public static final int CACHE_DIRECT = 0;
    private static final int CACHE_FORWARD = 1;
    private static final int CACHE_BACKWARD = 2;

    public ReadCache(File f, int page, ReadFragment fragment) throws IOException {

        Context c;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            c = fragment.getContext();
        }else{
            c = fragment.getActivity();
        }

        if (ArchiveParser.isSupportedArchive(f)){
            archive = ArchiveParser.parseArchive(f, c);
            totalPages = archive.getTotalPages();
        }else{
            archive = null;
            if (f.isFile()) f = f.getParentFile();
            for (File file : f.listFiles()){
                if (ImageParser.isSupportedImage(file)){
                    totalPages++;
                }
            }
        }

        this.f = f;
        this.currentPage = page;
        this.fragment = fragment;

        Log.i("ReadCache", "Initiated with file: "+f.getAbsolutePath());
        Log.i("ReadCache", "Initiated with page: "+page);

    }

    public void parseInitial(Context c, View v){
        parseImage(c, CACHE_DIRECT, currentPage, v);
    }

    private void parseImage(Context c, int cacheType, int page, View v){
        Log.i("parseImage", "page: "+page);
        DecodeAsync async;
        if (archive != null){
            async = new DecodeArchiveStreamAsync(c, this, cacheType, archive, v);
        }else {
            async = new DecodeBitmapFileAsync(c, this, cacheType, v);
        }
        async.execute(f.getAbsolutePath(), Integer.toString(page));
    }

    private int getTotalPages(){
        return this.totalPages;
    }

    public String getFileName(){
        return this.f.getAbsolutePath();
    }

    public int getCurrentPage(){
        return this.currentPage;
    }

    public void setImage(SoftReference<JBitmapDrawable> image, int cacheType, String msg) {
        switch (cacheType){
            case CACHE_DIRECT:
                fragment.setImage(image.get());
                setThumb(image);
                fragment.hideProgress();

                if (cacheForward.get() == null || cacheBackward.get() == null){
                    cacheNext(fragment.getActivity());
                }
                if (parsingInitial){
                    if (msg != null){
                        View v = fragment.getActivity().findViewById(R.id.drawer_layout);
                        if (v != null){
                            Snackbar.make(v, msg, Snackbar.LENGTH_SHORT).show();
                        }
                    }
                    parsingInitial = false;
                }

                break;
            case CACHE_FORWARD:
                cacheForward = image;
                break;
            case CACHE_BACKWARD:
                cacheBackward = image;
                break;
        }
    }

    private void setThumb(SoftReference<JBitmapDrawable> image){

        if (image != null && image.get() != null) {

            Context c;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                c = fragment.getContext();
            } else {
                c = fragment.getActivity();
            }

            if (recent == null) {
                recent = Recent.get(c, f.getAbsolutePath(), true);
            }

            if (recent != null) {
                recent.setPageNumber(c, currentPage);
            } else {
                JBitmapDrawable jbd = image.get();
                recent = new Recent(f.getAbsolutePath(), currentPage);
                recent.setPageNumber(c, currentPage); //inserts into DB
                GenerateThumbnailAsync async = new GenerateThumbnailAsync(c);
                async.execute(
                        f.getAbsolutePath(),
                        c.getCacheDir().getAbsolutePath(),
                        Long.toString(recent.getUuid()),
                        Integer.toString(jbd.getWidth()),
                        Integer.toString(jbd.getHeight())
{'fraction_non_alphanumeric': 0.10024381095273818, 'fraction_numerical': 0.0007501875468867217, 'mean_word_length': 1.9899074852817493, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3098382', 'n_tokens_mistral': 2918, 'n_tokens_neox': 2800, 'n_words': 640}