<commit_before>/*
 * Copyright (c) 2016 NECTEC
 *   National Electronics and Computer Technology Center, Thailand
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package th.or.nectec.tanrabad.survey.presenter;

import android.animation.AnimatorInflater;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.AnimRes;
import android.support.annotation.IdRes;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;

import com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader;

import java.util.List;

import th.or.nectec.tanrabad.domain.place.PlaceWithSurveyHistoryChooser;
import th.or.nectec.tanrabad.domain.place.PlaceWithSurveyHistoryListPresenter;
import th.or.nectec.tanrabad.entity.Place;
import th.or.nectec.tanrabad.entity.User;
import th.or.nectec.tanrabad.survey.R;
import th.or.nectec.tanrabad.survey.job.SyncJobBuilder;
import th.or.nectec.tanrabad.survey.job.SyncJobRunner;
import th.or.nectec.tanrabad.survey.repository.BrokerSurveyRepository;
import th.or.nectec.tanrabad.survey.repository.BrokerUserRepository;
import th.or.nectec.tanrabad.survey.utils.alert.Alert;
import th.or.nectec.tanrabad.survey.utils.android.NetworkChangeReceiver;

public class MainActivity extends TanrabadActivity implements View.OnClickListener,
        PlaceWithSurveyHistoryListPresenter, AdapterView.OnItemClickListener {

    private PlaceSurveyAdapter recentSurveyPlaceAdapter;
    private CardView recentSurveyPlaceCardView;
    private NetworkChangeReceiver networkChangeReceiver;
    private ObjectAnimator syncProgressAnimator;
    private PlaceWithSurveyHistoryChooser recentSurveyPlaceChooser;

    public static void open(Activity activity) {
        Intent intent = new Intent(activity, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setupViewOnClick();
        setupNetworkChangeReceiver();
        setupList();
        setupSyncAnimator();
        showRecentSurveyCard();
        doLoadingRecentSurveyData();
        if (!isUiTesting()) {
            startAnimation();
        }
    }

    private void setupViewOnClick() {
        findViewById(R.id.start_survey).setOnClickListener(this);
        findViewById(R.id.root).setOnClickListener(this);
        findViewById(R.id.magnifier).setOnClickListener(this);
        findViewById(R.id.sync_data).setOnClickListener(this);
    }

    private void setupNetworkChangeReceiver() {
        networkChangeReceiver = new NetworkChangeReceiver(new NetworkChangeReceiver.OnNetworkChangedListener() {
            @Override
            public void onNetworkChanged(boolean isConnected) {
                if (isConnected) {
                    findViewById(R.id.sync_data).setVisibility(View.VISIBLE);
                } else {
                    findViewById(R.id.sync_data).setVisibility(View.GONE);
                }
            }
        });
        registerReceiver(networkChangeReceiver, NetworkChangeReceiver.getIntentFilter());
    }

    private void setupList() {
        RecyclerView recentSurveyList = (RecyclerView) findViewById(R.id.place_history_list);
        recentSurveyPlaceAdapter = new PlaceSurveyAdapter(this, getSupportFragmentManager());
        recentSurveyList.setAdapter(recentSurveyPlaceAdapter);
        recentSurveyList.setLayoutManager(new LinearLayoutManager(this));
        recentSurveyList.addItemDecoration(new SimpleDividerItemDecoration(this));
        recentSurveyPlaceAdapter.setOnItemClickListener(this);
        RecyclerViewHeader recyclerViewHeader = (RecyclerViewHeader) findViewById(R.id.card_header);
        recyclerViewHeader.attachTo(recentSurveyList, true);
    }

    private void setupSyncAnimator() {
        syncProgressAnimator = (ObjectAnimator) AnimatorInflater.loadAnimator(
                this, R.animator.water_spin);
        syncProgressAnimator.setTarget(findViewById(R.id.water_shadow));
    }

    private void showRecentSurveyCard() {
        recentSurveyPlaceCardView = (CardView) findViewById(R.id.card_layout);
        recentSurveyPlaceChooser = new PlaceWithSurveyHistoryChooser(
                BrokerUserRepository.getInstance(),
                BrokerSurveyRepository.getInstance(),
                this);
    }

    private void doLoadingRecentSurveyData() {
        User user = AccountUtils.getUser();
        if (user != null)
            recentSurveyPlaceChooser.showSurveyPlaceList(user.getUsername());
        else finish();
    }

    private void startAnimation() {
        startAnimation(R.id.magnifier, R.anim.magnifier);
        startAnimation(R.id.larvae, R.anim.larvae);
        startAnimation(R.id.water_shadow, R.anim.water_shadow);
        startAnimation(R.id.larvae_deep, R.anim.larvae_deep);
        startAnimation(R.id.start_survey_hint, R.anim.scale_in);
    }

    private void startAnimation(@IdRes int viewId, @AnimRes int animId) {
        Animation anim = AnimationUtils.loadAnimation(this, animId);
        findViewById(viewId).startAnimation(anim);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        doLoadingRecentSurveyData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(networkChangeReceiver);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.start_survey:
                PlaceListActivity.open(this);
                break;
            case R.id.magnifier:
            case R.id.root:
                startAnimation(R.id.larvae, R.anim.dook_digg);
                break;
            case R.id.sync_data:
                startOrResumeSyncAnimation();
                findViewById(R.id.sync_data).setEnabled(false);
                new SyncJobBuilder().build(new MainSyncJobRunner()).start();
                break;
        }
    }

    private void startOrResumeSyncAnimation() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
                && syncProgressAnimator.isPaused()) {
            syncProgressAnimator.resume();
        } else {
            syncProgressAnimator.start();
        }
    }

    @Override
    public void displaySurveyPlaceList(List<Place> surveyPlace) {
        recentSurveyPlaceAdapter.updateData(surveyPlace);
        recentSurveyPlaceCardView.setVisibility(View.VISIBLE);
    }

    @Override<commit_msg>เพิ่มกด back 2 ครั้ง เพื่อออกจากแอปฯ<commit_after>/*
 * Copyright (c) 2016 NECTEC
 *   National Electronics and Computer Technology Center, Thailand
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package th.or.nectec.tanrabad.survey.presenter;

import android.animation.AnimatorInflater;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.AnimRes;
import android.support.annotation.IdRes;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;

import com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader;

import java.util.List;

import th.or.nectec.tanrabad.domain.place.PlaceWithSurveyHistoryChooser;
import th.or.nectec.tanrabad.domain.place.PlaceWithSurveyHistoryListPresenter;
import th.or.nectec.tanrabad.entity.Place;
import th.or.nectec.tanrabad.entity.User;
import th.or.nectec.tanrabad.survey.R;
import th.or.nectec.tanrabad.survey.job.SyncJobBuilder;
import th.or.nectec.tanrabad.survey.job.SyncJobRunner;
import th.or.nectec.tanrabad.survey.repository.BrokerSurveyRepository;
import th.or.nectec.tanrabad.survey.repository.BrokerUserRepository;
import th.or.nectec.tanrabad.survey.utils.alert.Alert;
import th.or.nectec.tanrabad.survey.utils.android.NetworkChangeReceiver;
import th.or.nectec.tanrabad.survey.utils.android.TwiceBackPressed;

public class MainActivity extends TanrabadActivity implements View.OnClickListener,
        PlaceWithSurveyHistoryListPresenter, AdapterView.OnItemClickListener {

    private PlaceSurveyAdapter recentSurveyPlaceAdapter;
    private CardView recentSurveyPlaceCardView;
    private NetworkChangeReceiver networkChangeReceiver;
    private ObjectAnimator syncProgressAnimator;
    private PlaceWithSurveyHistoryChooser recentSurveyPlaceChooser;
    private TwiceBackPressed twiceBackPressed;

    public static void open(Activity activity) {
        Intent intent = new Intent(activity, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        twiceBackPressed = new TwiceBackPressed(this);
        setupViewOnClick();
        setupNetworkChangeReceiver();
        setupList();
        setupSyncAnimator();
        showRecentSurveyCard();
        doLoadingRecentSurveyData();
        if (!isUiTesting()) {
            startAnimation();
        }
    }

    private void setupViewOnClick() {
        findViewById(R.id.start_survey).setOnClickListener(this);
        findViewById(R.id.root).setOnClickListener(this);
        findViewById(R.id.magnifier).setOnClickListener(this);
        findViewById(R.id.sync_data).setOnClickListener(this);
    }

    private void setupNetworkChangeReceiver() {
        networkChangeReceiver = new NetworkChangeReceiver(new NetworkChangeReceiver.OnNetworkChangedListener() {
            @Override
            public void onNetworkChanged(boolean isConnected) {
                if (isConnected) {
                    findViewById(R.id.sync_data).setVisibility(View.VISIBLE);
                } else {
                    findViewById(R.id.sync_data).setVisibility(View.GONE);
                }
            }
        });
        registerReceiver(networkChangeReceiver, NetworkChangeReceiver.getIntentFilter());
    }

    private void setupList() {
        RecyclerView recentSurveyList = (RecyclerView) findViewById(R.id.place_history_list);
        recentSurveyPlaceAdapter = new PlaceSurveyAdapter(this, getSupportFragmentManager());
        recentSurveyList.setAdapter(recentSurveyPlaceAdapter);
        recentSurveyList.setLayoutManager(new LinearLayoutManager(this));
        recentSurveyList.addItemDecoration(new SimpleDividerItemDecoration(this));
        recentSurveyPlaceAdapter.setOnItemClickListener(this);
        RecyclerViewHeader recyclerViewHeader = (RecyclerViewHeader) findViewById(R.id.card_header);
        recyclerViewHeader.attachTo(recentSurveyList, true);
    }

    private void setupSyncAnimator() {
        syncProgressAnimator = (ObjectAnimator) AnimatorInflater.loadAnimator(
                this, R.animator.water_spin);
        syncProgressAnimator.setTarget(findViewById(R.id.water_shadow));
    }

    private void showRecentSurveyCard() {
        recentSurveyPlaceCardView = (CardView) findViewById(R.id.card_layout);
        recentSurveyPlaceChooser = new PlaceWithSurveyHistoryChooser(
                BrokerUserRepository.getInstance(),
                BrokerSurveyRepository.getInstance(),
                this);
    }

    private void doLoadingRecentSurveyData() {
        User user = AccountUtils.getUser();
        if (user != null)
            recentSurveyPlaceChooser.showSurveyPlaceList(user.getUsername());
        else finish();
    }

    private void startAnimation() {
        startAnimation(R.id.magnifier, R.anim.magnifier);
        startAnimation(R.id.larvae, R.anim.larvae);
        startAnimation(R.id.water_shadow, R.anim.water_shadow);
        startAnimation(R.id.larvae_deep, R.anim.larvae_deep);
        startAnimation(R.id.start_survey_hint, R.anim.scale_in);
    }

    private void startAnimation(@IdRes int viewId, @AnimRes int animId) {
        Animation anim = AnimationUtils.loadAnimation(this, animId);
        findViewById(viewId).startAnimation(anim);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(networkChangeReceiver);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.start_survey:
                PlaceListActivity.open(this);
                break;
            case R.id.magnifier:
            case R.id.root:
                startAnimation(R.id.larvae, R.anim.dook_digg);
                break;
            case R.id.sync_data:
                startOrResumeSyncAnimation();
                findViewById(R.id.sync_data).setEnabled(false);
                new SyncJobBuilder().build(new MainSyncJobRunner()).start();
                break;
        }
    }

    private void startOrResumeSyncAnimation() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
                && syncProgressAnimator.isPaused()) {
            syncProgressAnimator.resume();
        } else {
            syncProgressAnimator.start();
        }
    }

    @Override
    public void onBackPressed() {
        if (twiceBackPressed.onTwiceBackPressed()) {
            finish();
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        doLoadingRecentSurveyData();
    }

    @Override
    public void displaySurveyPlaceList(List<Place> surveyPlace) {
        recentSurveyPlaceAdapter.updateData(surveyPlace);
        recentSurveyPlaceCardView.setVisibility(View.VISIBLE);
    }

    @Override
{'fraction_non_alphanumeric': 0.08952669343690343, 'fraction_numerical': 0.0015140543743005727, 'mean_word_length': 3.257847533632287, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3404978', 'n_tokens_mistral': 4353, 'n_tokens_neox': 4156, 'n_words': 748}