<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package info.zamojski.soft.towercollector.views;

import info.zamojski.soft.towercollector.MyApplication;
import info.zamojski.soft.towercollector.R;
import info.zamojski.soft.towercollector.controls.ISwipingController;
import info.zamojski.soft.towercollector.utils.PermissionUtils;

import android.Manifest;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;

import org.jetbrains.annotations.NotNull;

public class MainActivityPagerAdapter extends FragmentStatePagerAdapter implements ISwipingController {

    private static final int MainLastFragmentIndex = 0;
    private static final int MainStatsFragmentIndex = 1;
    private static final int MainMapFragmentIndex = 2;

    private boolean wasMapFullyConfiguredRecently = false;

    private final Context context;

    public MainActivityPagerAdapter(FragmentManager fm, Context context) {
        super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        this.context = context;
    }

    @NotNull
    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case MainLastFragmentIndex:
                return new MainLastFragment();
            case MainStatsFragmentIndex:
                return new MainStatsFragment();
            case MainMapFragmentIndex:
                if (isMapFullyConfigured()) {
                    return new MainMapFragment();
                }
                return new MainMapConfigureFragment();
            default:
                throw new UnsupportedOperationException("Cannot find view at position " + position);
        }
    }

    private boolean isMapFullyConfigured() {
        return wasMapFullyConfiguredRecently = (MyApplication.getPreferencesProvider().isMainMapConfigured()
                && PermissionUtils.hasPermissions(MyApplication.getApplication(), Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION));
    }
<commit_msg>Fix swiping enabled on startup when the map was the last active tab.<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package info.zamojski.soft.towercollector.views;

import info.zamojski.soft.towercollector.MyApplication;
import info.zamojski.soft.towercollector.R;
import info.zamojski.soft.towercollector.controls.ISwipingController;
import info.zamojski.soft.towercollector.utils.PermissionUtils;

import android.Manifest;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;

import org.jetbrains.annotations.NotNull;

public class MainActivityPagerAdapter extends FragmentStatePagerAdapter implements ISwipingController {

    private static final int MainLastFragmentIndex = 0;
    private static final int MainStatsFragmentIndex = 1;
    private static final int MainMapFragmentIndex = 2;

    private boolean wasMapFullyConfiguredRecently = false;

    private final Context context;

    public MainActivityPagerAdapter(FragmentManager fm, Context context) {
        super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        this.context = context;
        isMapFullyConfigured(); // initialize, otherwise swiping not applied on startup
    }

    @NotNull
    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case MainLastFragmentIndex:
                return new MainLastFragment();
            case MainStatsFragmentIndex:
                return new MainStatsFragment();
            case MainMapFragmentIndex:
                if (isMapFullyConfigured()) {
                    return new MainMapFragment();
                }
                return new MainMapConfigureFragment();
            default:
                throw new UnsupportedOperationException("Cannot find view at position " + position);
        }
    }

    private boolean isMapFullyConfigured() {
        return wasMapFullyConfiguredRecently = (MyApplication.getPreferencesProvider().isMainMapConfigured()
                && PermissionUtils.hasPermissions(MyApplication.getApplication(), Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION));
    }

{'fraction_non_alphanumeric': 0.07005291005291005, 'fraction_numerical': 0.002962962962962963, 'mean_word_length': 3.429240862230553, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1093629', 'n_tokens_mistral': 1265, 'n_tokens_neox': 1212, 'n_words': 314}