package com.parse.starter;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;

import java.io.File;
import java.io.IOException;


public class MainActivity extends AppCompatActivity{

    private float x1,x2;
    static final int MIN_DISTANCE = 150;
    private ViewFlipper welcomeFlipper;
    int f_index;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        File file = new File(getFilesDir(), "FavouramaLicense");

        if(file.exists()){
          /*User already accepted license, move on*/
            ParseAnalytics.trackAppOpenedInBackground(getIntent());

            Intent i = new Intent(this, ACTLoginSelf.class);
            startActivity(i);
        }else {
            setContentView(R.layout.activity_main);
            welcomeFlipper = (ViewFlipper) findViewById(R.id.welcome_flipper);
            f_index = 0;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event){
        switch(event.getAction() & MotionEvent.ACTION_MASK)
        {
            case MotionEvent.ACTION_DOWN:
                x1 = event.getX();
                break;
            case MotionEvent.ACTION_UP:
                x2 = event.getX();
                float deltaX = x2 - x1;
                if (x2 - x1 >= MIN_DISTANCE)
                {
                    /*Swipe to right, show previous page*/
                    welcomeFlipper.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left));
                    welcomeFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right));
                    welcomeFlipper.showPrevious();
                    f_index--;
                    if(f_index < 0){
                        f_index = 2;
                    }
                    highlightPoint(f_index);

                } else if(x2 - x1 <= -MIN_DISTANCE){
                    /*Swipe to left, show next page*/
                    welcomeFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_in_right));
                    welcomeFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_out_left));
                    welcomeFlipper.showNext();
                    f_index++;
                    f_index%=3;
                    highlightPoint(f_index);
                }
                break;
        }

        return super.onTouchEvent(event);
    }

    public void acceptAndProceed(View view) {
        File file = new File(getFilesDir(), "FavouramaLicense");
        try {
            file.createNewFile();
            ParseAnalytics.trackAppOpenedInBackground(getIntent());

            Intent i = new Intent(this, ACTLoginSelf.class);
            startActivity(i);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void showAgreement(View view) {
        findViewById(R.id.license_agreement_container).setVisibility(View.VISIBLE);
    }

    public void highlightPoint(int index){
        switch (index){
            case 0:
                ((ImageView)findViewById(R.id.findex_i1)).setImageResource(R.drawable.f_intro_point_hl);
                ((ImageView)findViewById(R.id.findex_i2)).setImageResource(R.drawable.f_intro_point);
                ((ImageView)findViewById(R.id.findex_i3)).setImageResource(R.drawable.f_intro_point);
                break;
            case 1:
                ((ImageView)findViewById(R.id.findex_i1)).setImageResource(R.drawable.f_intro_point);
                ((ImageView)findViewById(R.id.findex_i2)).setImageResource(R.drawable.f_intro_point_hl);
                ((ImageView)findViewById(R.id.findex_i3)).setImageResource(R.drawable.f_intro_point);
                break;
            case 2:
                ((ImageView)findViewById(R.id.findex_i1)).setImageResource(R.drawable.f_intro_point);
                ((ImageView)findViewById(R.id.findex_i2)).setImageResource(R.drawable.f_intro_point);
                ((ImageView)findViewById(R.id.findex_i3)).setImageResource(R.drawable.f_intro_point_hl);
                break;
        }
    }
}
@@ -20,16 +20,17 @@
 tarter;%0A
+%0A
 import a
@@ -471,10 +471,10 @@
 E = 
-15
+30
 0;%0A 
@@ -528,16 +528,42 @@
 f_index;
+%0A    boolean doneSwapping;
 %0A%0A    @O
@@ -1149,16 +1149,50 @@
 ex = 0;%0A
+            doneSwapping = false;%0A
         
@@ -1237,40 +1237,318 @@
 ean 
-onTouchEvent(MotionEvent event)%7B
+dispatchTouchEvent(MotionEvent event)%7B%0A        this.onTouchEvent(event);%0A        return super.dispatchTouchEvent(event);%0A    %7D%0A%0A    @Override%0A    public boolean onTouchEvent(MotionEvent event)%7B%0A        if(doneSwapping)%7B%0A            doneSwapping = false;%0A            return super.onTouchEvent(event);%0A        %7D%0A
 %0A   
@@ -2411,16 +2411,56 @@
 index);%0A
+                    doneSwapping = true;
 %0A       
@@ -2927,32 +2927,73 @@
 Point(f_index);%0A
+                    doneSwapping = true;%0A
                 
@@ -4754,8 +4754,9 @@
 %0A    %7D%0A%7D
+%0A

Enable swiping when the agreement is shown in ListView so the user can still look back