<commit_before>package com.satsumasoftware.timetable.ui.start

import android.app.DatePickerDialog
import android.os.Bundle
import android.support.design.widget.Snackbar
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.view.ViewPager
import android.support.v7.app.AppCompatActivity
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import com.satsumasoftware.timetable.R
import com.satsumasoftware.timetable.db.handler.TimetableHandler
import com.satsumasoftware.timetable.framework.Timetable
import org.threeten.bp.LocalDate
import org.threeten.bp.format.DateTimeFormatter

class WelcomeActivity : AppCompatActivity() {

    private var mViewPager: ViewPager? = null
    private var mProgressText: TextView? = null

    companion object{
        private var sName: String? = null
        private var sStartDate: LocalDate? = null
        private var sEndDate: LocalDate? = null
        private var sWeekRotations: Int? = null
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_welcome)

        setupLayout()
    }

    private fun setupLayout() {
        mViewPager = findViewById(R.id.viewPager) as ViewPager
        mViewPager!!.adapter = PagerAdapter(supportFragmentManager)

        mProgressText = findViewById(R.id.textView_progress) as TextView
        updateProgressText()

        val prevButton = findViewById(R.id.button_previous) as Button
        prevButton.setOnClickListener {
            changePage(true)
            updateProgressText()
        }

        val nextButton = findViewById(R.id.button_next) as Button
        nextButton.setOnClickListener {
            changePage()
            updateProgressText()
        }
    }

    private fun updateProgressText() {
        mProgressText!!.text =
                (mViewPager!!.currentItem + 1).toString() + " / " + PagerAdapter.PAGES_COUNT
    }

    private fun changePage(goBack: Boolean = false) {
        mViewPager!!.currentItem = if (goBack) {
            mViewPager!!.currentItem - 1

        } else {
            if (hasMissingInputs()) {
                Snackbar.make(
                        findViewById(R.id.rootLayout),
                        R.string.welcome_inputs_missing,
                        Snackbar.LENGTH_SHORT
                ).show()
                return
            }

            if (!checkInvalidInputs()) {
                return
            }

            mViewPager!!.currentItem + 1
        }
    }

    /**
     * @return true if there is at least one missing input.
     */
    private fun hasMissingInputs(): Boolean {
        return when (mViewPager!!.currentItem) {
            PagerAdapter.PAGE_TIMETABLE_NAME -> sName.isNullOrEmpty()
            PagerAdapter.PAGE_TIMETABLE_DETAILS -> sStartDate == null || sEndDate == null
            else -> false
        }
    }

    /**
     * Checks the inputs for the current view pager item.
     * It will display Snackbar messages accordingly.
     *
     * @return whether there are any invalid inputs (i.e. true for okay inputs, false for invalid).
     */
    private fun checkInvalidInputs(): Boolean {
        when (mViewPager!!.currentItem) {
            PagerAdapter.PAGE_TIMETABLE_DETAILS -> {
                if (sStartDate!!.isAfter(sEndDate!!)) {
                    Snackbar.make(
                            findViewById(R.id.rootLayout),
                            R.string.message_start_date_after_end_date,
                            Snackbar.LENGTH_SHORT).show()
                    return false
                }
            }
        }

        return true
    }

    fun createTimetable(): Timetable {
        return Timetable(
                TimetableHandler(this).getHighestItemId() + 1,
                checkNotNull(sName),
                checkNotNull(sStartDate),
                checkNotNull(sEndDate),
                checkNotNull(sWeekRotations))
    }

    private class PagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {

        companion object {

            const val PAGES_COUNT = 4

            const val PAGE_START = 0
            const val PAGE_TIMETABLE_NAME = 1
            const val PAGE_TIMETABLE_DETAILS = 2<commit_msg>Add functionality to go to the home page after the welcome<commit_after>package com.satsumasoftware.timetable.ui.start

import android.app.DatePickerDialog
import android.content.Intent
import android.os.Bundle
import android.support.design.widget.Snackbar
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.view.ViewPager
import android.support.v7.app.AppCompatActivity
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import com.satsumasoftware.timetable.R
import com.satsumasoftware.timetable.TimetableApplication
import com.satsumasoftware.timetable.db.handler.TimetableHandler
import com.satsumasoftware.timetable.framework.Timetable
import com.satsumasoftware.timetable.ui.MainActivity
import org.threeten.bp.LocalDate
import org.threeten.bp.format.DateTimeFormatter

class WelcomeActivity : AppCompatActivity() {

    private var mViewPager: ViewPager? = null
    private var mProgressText: TextView? = null

    companion object{
        private var sName: String? = null
        private var sStartDate: LocalDate? = null
        private var sEndDate: LocalDate? = null
        private var sWeekRotations: Int? = null
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_welcome)

        setupLayout()
    }

    private fun setupLayout() {
        mViewPager = findViewById(R.id.viewPager) as ViewPager
        mViewPager!!.adapter = PagerAdapter(supportFragmentManager)

        mProgressText = findViewById(R.id.textView_progress) as TextView
        updateProgressText()

        val prevButton = findViewById(R.id.button_previous) as Button
        prevButton.setOnClickListener {
            changePage(true)
            updateProgressText()
        }

        val nextButton = findViewById(R.id.button_next) as Button
        nextButton.setOnClickListener {
            if (mViewPager!!.currentItem == PagerAdapter.PAGE_END) {
                saveAndExit()
                return@setOnClickListener
            }

            changePage()
            updateProgressText()
        }
    }

    private fun updateProgressText() {
        mProgressText!!.text =
                (mViewPager!!.currentItem + 1).toString() + " / " + PagerAdapter.PAGES_COUNT
    }

    private fun changePage(goBack: Boolean = false) {
        mViewPager!!.currentItem = if (goBack) {
            mViewPager!!.currentItem - 1

        } else {
            if (hasMissingInputs()) {
                Snackbar.make(
                        findViewById(R.id.rootLayout),
                        R.string.welcome_inputs_missing,
                        Snackbar.LENGTH_SHORT
                ).show()
                return
            }

            if (!checkInvalidInputs()) {
                return
            }

            mViewPager!!.currentItem + 1
        }
    }

    /**
     * @return true if there is at least one missing input.
     */
    private fun hasMissingInputs(): Boolean {
        return when (mViewPager!!.currentItem) {
            PagerAdapter.PAGE_TIMETABLE_NAME -> sName.isNullOrEmpty()
            PagerAdapter.PAGE_TIMETABLE_DETAILS -> sStartDate == null || sEndDate == null
            else -> false
        }
    }

    /**
     * Checks the inputs for the current view pager item.
     * It will display Snackbar messages accordingly.
     *
     * @return whether there are any invalid inputs (i.e. true for okay inputs, false for invalid).
     */
    private fun checkInvalidInputs(): Boolean {
        when (mViewPager!!.currentItem) {
            PagerAdapter.PAGE_TIMETABLE_DETAILS -> {
                if (sStartDate!!.isAfter(sEndDate!!)) {
                    Snackbar.make(
                            findViewById(R.id.rootLayout),
                            R.string.message_start_date_after_end_date,
                            Snackbar.LENGTH_SHORT).show()
                    return false
                }
            }
        }

        return true
    }

    /**
     * Saves the timetable to the database and exits to the main page.
     */
    private fun saveAndExit() {
        val dataHandler = TimetableHandler(this)

        val timetable = Timetable(
                dataHandler.getHighestItemId() + 1,
                checkNotNull(sName),
                checkNotNull(sStartDate),
                checkNotNull(sEndDate),
                1)  // default week rotations is 1

        dataHandler.addItem(timetable)

        (application as TimetableApplication).setCurrentTimetable(this, timetable)

        startActivity(Intent(this, MainActivity::class.java))
    }

    private class PagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {

        companion object {

            const val PAGES_COUNT = 4

            const val PAGE_START = 0
            const val PAGE_TIMETABLE_NAME = 1
            const val PAGE_TIMETABLE_DETAILS = 2
{'fraction_non_alphanumeric': 0.07982329977398808, 'fraction_numerical': 0.0028765153071707416, 'mean_word_length': 2.166883539362394, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '4266520', 'n_tokens_mistral': 2739, 'n_tokens_neox': 2575, 'n_words': 556}