<commit_before>package motocitizen.ui.activity

import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.support.v7.app.ActionBar
import android.support.v7.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.ProgressBar
import android.widget.Toast
import com.google.android.gms.maps.model.LatLng
import motocitizen.MyApp
import motocitizen.content.Content
import motocitizen.datasources.preferences.Preferences
import motocitizen.geo.geolocation.MyLocationManager
import motocitizen.geo.maps.MainMapManager
import motocitizen.main.R
import motocitizen.permissions.Permissions
import motocitizen.router.Router
import motocitizen.ui.changelog.ChangeLog
import motocitizen.ui.rows.accident.AccidentRowFactory
import motocitizen.ui.views.BounceScrollView
import motocitizen.user.User
import motocitizen.utils.getWidth

class MainScreenActivity : AppCompatActivity() {
    private val LIST: Byte = 0
    private val MAP: Byte = 1
    private lateinit var actionBar: ActionBar

    private lateinit var mapContainer: ViewGroup
    private lateinit var createAccButton: ImageButton
    private lateinit var toAccListButton: ImageButton
    private lateinit var toMapButton: ImageButton
    private lateinit var accListView: View
    private lateinit var progressBar: ProgressBar
    private lateinit var listContent: ViewGroup

    private var refreshItem: MenuItem? = null
    private lateinit var map: MainMapManager
    private var inTransaction = false
    private var currentScreen = LIST

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_screen_activity)
        actionBar = supportActionBar!!  //todo NPE
        map = MainMapManager(this)
    }

    override fun onResume() {
        super.onResume()
        wakeUpLocationUpdate()
        showChangeLogIfUpdated()
        MyLocationManager.subscribeToLocationUpdate("MAIN", this::updateStatusBar)
        disableDialOnTablets()

        bindViews()
        setUpListeners()

        setPermissions()
        showCurrentFrame()
        redraw()
        accidents
    }

    private fun wakeUpLocationUpdate() {
        Permissions.requestLocation(this, {
            MyLocationManager.wakeup()
            map.enableLocation()
        }) { }
    }

    private fun disableDialOnTablets() {
        if (packageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) return
        findViewById(R.id.dial_button).isEnabled = false
    }

    private fun showChangeLogIfUpdated() {
        if (!Preferences.newVersion) return
        val changeLogDlg = ChangeLog.getDialog(this)
        changeLogDlg.show()
        Preferences.newVersion = false
    }

    private fun setUpListeners() {
        createAccButton.setOnClickListener { Router.goTo(this, Router.Target.CREATE) }
        toAccListButton.setOnClickListener { showListFrame() }
        toMapButton.setOnClickListener { showMapFrame() }
        findViewById(R.id.dial_button).setOnClickListener { Router.dial(this, getString(R.string.phone)) }
        (findViewById(R.id.accListRefresh) as BounceScrollView).setOverScrollListener { accidents }
    }

    private fun bindViews() {
        accListView = findViewById(R.id.acc_list)
        progressBar = findViewById(R.id.progressBar) as ProgressBar
        mapContainer = findViewById(R.id.google_map) as ViewGroup
        createAccButton = findViewById(R.id.add_point_button) as ImageButton
        toAccListButton = findViewById(R.id.list_button) as ImageButton
        toMapButton = findViewById(R.id.map_button) as ImageButton
        listContent = findViewById(R.id.accListContent) as ViewGroup
    }

    override fun onPause() {
        super.onPause()
        MyLocationManager.unSubscribe("MAIN")
        Permissions.requestLocation(this, { MyLocationManager.sleep() }) { }
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        if (intent.hasExtra("toMap")) {
            //mainFragment.toMap(intent.getExtras().getInt("toMap", 0));
            toMap(intent.extras.getInt("toMap", 0))
            intent.removeExtra("toMap")
        }
        if (intent.hasExtra("toDetails")) {
            intent.removeExtra("toDetails")
        }
        setIntent(intent)
    }

    private fun setPermissions() {
        createAccButton.visibility = if (User.isStandard) View.VISIBLE else View.INVISIBLE
    }

    private fun redraw() {
        val newList = Content.getVisibleReversed().map { AccidentRowFactory.make(this, it) }
        listContent.removeAllViews()

        newList.forEach(listContent::addView)

        map.addContent()
    }
//todo WTF!?
    private val accidents: Unit
        get() {
            if (inTransaction) return
            if (MyApp.isOnline(this)) {
                startRefreshAnimation()
                Content.requestUpdate { updateCompleteCallback() }
            } else {
                Toast.makeText(this, getString(R.string.inet_not_available), Toast.LENGTH_LONG).show()
            }
        }

    private fun updateCompleteCallback() {
        runOnUiThread {
            stopRefreshAnimation()
            redraw()
        }
    }

    private fun stopRefreshAnimation() {<commit_msg>Some SOLID refactor<commit_after>package motocitizen.ui.activity

import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.support.v7.app.ActionBar
import android.support.v7.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.ProgressBar
import android.widget.Toast
import com.google.android.gms.maps.model.LatLng
import motocitizen.MyApp
import motocitizen.content.Content
import motocitizen.datasources.preferences.Preferences
import motocitizen.geo.geolocation.MyLocationManager
import motocitizen.geo.maps.MainMapManager
import motocitizen.main.R
import motocitizen.permissions.Permissions
import motocitizen.router.Router
import motocitizen.ui.changelog.ChangeLog
import motocitizen.ui.rows.accident.AccidentRowFactory
import motocitizen.ui.views.BounceScrollView
import motocitizen.user.User
import motocitizen.utils.getWidth

class MainScreenActivity : AppCompatActivity() {
    private val LIST: Byte = 0
    private val MAP: Byte = 1
    private lateinit var actionBar: ActionBar

    private lateinit var mapContainer: ViewGroup
    private lateinit var createAccButton: ImageButton
    private lateinit var toAccListButton: ImageButton
    private lateinit var toMapButton: ImageButton
    private lateinit var accListView: View
    private lateinit var progressBar: ProgressBar
    private lateinit var listContent: ViewGroup

    private var refreshItem: MenuItem? = null
    private lateinit var map: MainMapManager
    private var inTransaction = false
    private var currentScreen = LIST

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_screen_activity)
        actionBar = supportActionBar!!  //todo NPE
        map = MainMapManager(this)
    }

    override fun onResume() {
        super.onResume()
        wakeUpLocationUpdate()
        showChangeLogIfUpdated()
        MyLocationManager.subscribeToLocationUpdate("MAIN", this::updateStatusBar)
        disableDialOnTablets()

        bindViews()
        setUpListeners()

        setPermissions()
        showCurrentFrame()
        redraw()
        accidents
    }

    private fun wakeUpLocationUpdate() {
        Permissions.requestLocation(this, {
            MyLocationManager.wakeup()
            map.enableLocation()
        }) { }
    }

    private fun disableDialOnTablets() {
        if (packageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) return
        findViewById(R.id.dial_button).isEnabled = false
    }

    private fun showChangeLogIfUpdated() {
        if (!Preferences.newVersion) return
        val changeLogDlg = ChangeLog.getDialog(this)
        changeLogDlg.show()
        Preferences.newVersion = false
    }

    private fun setUpListeners() {
        createAccButton.setOnClickListener { Router.goTo(this, Router.Target.CREATE) }
        toAccListButton.setOnClickListener { showListFrame() }
        toMapButton.setOnClickListener { showMapFrame() }
        findViewById(R.id.dial_button).setOnClickListener { Router.dial(this, getString(R.string.phone)) }
        (findViewById(R.id.accListRefresh) as BounceScrollView).setOverScrollListener { accidents }
    }

    private fun bindViews() {
        accListView = findViewById(R.id.acc_list)
        progressBar = findViewById(R.id.progressBar) as ProgressBar
        mapContainer = findViewById(R.id.google_map) as ViewGroup
        createAccButton = findViewById(R.id.add_point_button) as ImageButton
        toAccListButton = findViewById(R.id.list_button) as ImageButton
        toMapButton = findViewById(R.id.map_button) as ImageButton
        listContent = findViewById(R.id.accListContent) as ViewGroup
    }

    override fun onPause() {
        super.onPause()
        MyLocationManager.unSubscribe("MAIN")
        Permissions.requestLocation(this, { MyLocationManager.sleep() }) { }
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        if (intent.hasExtra("toMap")) {
            //mainFragment.toMap(intent.getExtras().getInt("toMap", 0));
            toMap(intent.extras.getInt("toMap", 0))
            intent.removeExtra("toMap")
        }
        if (intent.hasExtra("toDetails")) {
            intent.removeExtra("toDetails")
        }
        setIntent(intent)
    }

    private fun setPermissions() {
        createAccButton.visibility = if (User.isStandard) View.VISIBLE else View.INVISIBLE
    }

    private fun redraw() {
        val newList = Content.getVisibleReversed().map { AccidentRowFactory.make(this, it) }
        listContent.removeAllViews()

        newList.forEach(listContent::addView)

        map.addContent()
    }
//todo WTF!?
    private val accidents: Unit
        get() {
            if (inTransaction) return
            if (MyApp.isOnline(this)) {
                startRefreshAnimation()
                Content.requestUpdate { updateCompleteCallback() }
            } else {
                Toast.makeText(this, getString(R.string.inet_not_available), Toast.LENGTH_LONG).show()
            }
        }

    private fun updateCompleteCallback() {
        runOnUiThread {
            stopRefreshAnimation()
            redraw()
        }
    }
//todo extract progressBar to separate class
    private fun stopRefreshAnimation() {
{'fraction_non_alphanumeric': 0.08443708609271523, 'fraction_numerical': 0.0011037527593818985, 'mean_word_length': 3.0906696764484574, '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': '5010553', 'n_tokens_mistral': 3055, 'n_tokens_neox': 2964, 'n_words': 598}