Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to ViewPager2 (got a deprecation warning for ViewPager) #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies {
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.navigation:navigation-fragment:2.0.0'
implementation 'androidx.navigation:navigation-ui:2.0.0'
implementation 'androidx.viewpager2:viewpager2:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
Expand Down
49 changes: 27 additions & 22 deletions app/src/main/java/atorch/statspuzzles/SolvePuzzle.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,26 @@
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.ShareActionProvider;
import androidx.core.view.MenuItemCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there might be some other unused imports I can remove


import org.mariuszgromada.math.mxparser.Expression;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Random;

import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;


public class SolvePuzzle extends AppCompatActivity {
// Following example at https://developer.android.com/training/sharing/shareaction.html
private ShareActionProvider mShareActionProvider;
Expand All @@ -44,7 +49,8 @@ public class SolvePuzzle extends AppCompatActivity {
public static final String LEVEL = "atorch.statspuzzles.LEVEL";
private static final int roundingScale = 4;

private ViewPager puzzlePager; // Displays puzzles one at a time
// Displays puzzles one at a time
private ViewPager2 puzzlePager;

private int level;

Expand All @@ -64,18 +70,14 @@ public void onCreate(Bundle savedInstanceState) {
level = intent.getIntExtra(PuzzleSelection.LEVEL, 0);
}

FragmentManager fragmentManager = getSupportFragmentManager();

res = new Res(getResources());
puzzlePager = findViewById(R.id.pager);
puzzlePager.setAdapter(new AppSectionsPagerAdapter(fragmentManager, level, res));
puzzlePager.setAdapter(new AppSectionsPagerAdapter(this, level, res));

puzzlePager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override public void onPageScrollStateChanged(int arg0) {
}
@Override public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override public void onPageSelected(int puzzleIndex) {
puzzlePager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int puzzleIndex) {
super.onPageSelected(puzzleIndex);
prepareSharePuzzle(res.getPuzzle(level, puzzleIndex));
}
});
Expand Down Expand Up @@ -209,30 +211,32 @@ String getFirstCongratulation(int level) {
}
}

public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {
// TODO FragmentPagerAdapter is deprecated
public static class AppSectionsPagerAdapter extends FragmentStateAdapter {

private final int level;
private final Res res;

public AppSectionsPagerAdapter(FragmentManager fm, int level, Res res) {
super(fm);
public AppSectionsPagerAdapter(@NonNull FragmentActivity fa, int level, Res res) {
super(fa);
this.level = level;
this.res = res;
}

@NonNull
@Override
public Fragment getItem(int puzzleIndex) {
public Fragment createFragment(int puzzleIndex) {
Fragment fragment = new SolvePuzzleFragment();
Bundle args = new Bundle();
args.putInt(LEVEL, level);
args.putInt(PUZZLE_INDEX, puzzleIndex);

fragment.setArguments(args);
return fragment;
}


@Override
public int getCount() {
public int getItemCount() {
return res.getPuzzleCount(level);
}
}
Expand All @@ -242,7 +246,7 @@ public static class SolvePuzzleFragment extends Fragment {
private int level;
private int puzzleIndex;
private double correctAnswer;
private ViewPager mViewPager;
private ViewPager2 mViewPager;
private Res res;

@Override
Expand Down Expand Up @@ -335,7 +339,8 @@ private void onSubmit(View view) {
Expression answerExpression = new Expression(answerString);
double answer = answerExpression.calculate();
if (!answerExpression.checkSyntax()) {
openTroubleParsingDialog(view); // TODO Don't show if user answer is empty string?
// TODO Don't show if user answer is empty string?
openTroubleParsingDialog();
hadTroubleParsing = true;
}
if (!Double.isNaN(answer) && !Double.isInfinite(answer)) {
Expand All @@ -349,13 +354,13 @@ private void onSubmit(View view) {
if (!Double.isNaN(answer) && Math.abs(answer - correctAnswer) < 0.00001) {
openCongratulationsAlert(view);
} else if (!Double.isNaN(answer) && Math.abs(answer - correctAnswer) < 0.001) {
openAccuracyAlert(view);
openAccuracyAlert();
} else if (!hadTroubleParsing) {
openIncorrectAnswerToast(); // User answer could parse to NaN
}
}

public void openTroubleParsingDialog(View view) {
public void openTroubleParsingDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(getString(R.string.trouble_parsing_answer));
builder.setCancelable(true);
Expand Down Expand Up @@ -443,7 +448,7 @@ public void openIncorrectAnswerToast() {
Toast.makeText(context, res.getRandomIncorrect(), Toast.LENGTH_LONG).show();
}

public void openAccuracyAlert(View view) {
public void openAccuracyAlert() {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(getString(R.string.accuracy));
builder.setCancelable(true);
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout/activity_solve_puzzle.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.viewpager.widget.ViewPager
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/pager"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

</androidx.viewpager.widget.ViewPager>
</androidx.viewpager2.widget.ViewPager2>
Loading