Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener, LocationListener {
- private var mLocationManager: LocationManager? = null
- private var mLOcation: Location? = null
- private var storeListFragment: Fragment? = null
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- val toolbar = findViewById(R.id.toolbar) as Toolbar
- setSupportActionBar(toolbar)
- val fab = findViewById(R.id.fab) as FloatingActionButton
- fab.setOnClickListener(object : View.OnClickListener() {
- fun onClick(view: View) {
- Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
- .setAction("Action", null).show()
- }
- })
- val drawer = findViewById(R.id.drawer_layout) as DrawerLayout
- val toggle = ActionBarDrawerToggle(
- this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
- drawer.setDrawerListener(toggle)
- toggle.syncState()
- val navigationView = findViewById(R.id.nav_view) as NavigationView
- navigationView.setNavigationItemSelectedListener(this)
- navigationView.setCheckedItem(0)
- mLocationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
- // cek izin
- if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
- //minta izin
- ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
- 1)
- return
- }
- }
- mLocationManager!!.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
- 10,
- 10f, this)
- PopupUtil.showLoading(this, "", "Finding your location ...")
- }
- private fun loadStoreListFragment() {
- storeListFragment = StoreListFragment()
- supportFragmentManager.beginTransaction().replace(R.id.container, storeListFragment).commit()
- }
- override fun onRequestPermissionsResult(requestCode: Int,
- permissions: Array<String>,
- grantResults: IntArray) {
- if (requestCode == 1) {
- if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- //diizinkan
- // baru minta location
- if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
- return
- }
- mLocationManager!!.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
- 10,
- 10f, this)
- PopupUtil.showLoading(this, "", "Finding your location ...")
- }
- }
- }
- override fun onBackPressed() {
- val drawer = findViewById(R.id.drawer_layout) as DrawerLayout
- if (drawer.isDrawerOpen(GravityCompat.START)) {
- drawer.closeDrawer(GravityCompat.START)
- } else {
- super.onBackPressed()
- }
- }
- fun onCreateOptionsMenu(menu: Menu): Boolean {
- // Inflate the menu; this adds items to the action bar if it is present.
- menuInflater.inflate(R.menu.main, menu)
- return true
- }
- fun onOptionsItemSelected(item: MenuItem): Boolean {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- val id = item.getItemId()
- return if (id == R.id.action_settings) {
- true
- } else super.onOptionsItemSelected(item)
- }
- fun onNavigationItemSelected(item: MenuItem): Boolean {
- // Handle navigation view item clicks here.
- val id = item.getItemId()
- if (storeListFragment == null) {
- storeListFragment = StoreListFragment()
- }
- var fragment: Fragment? = null
- if (id == R.id.nav_camera) {
- fragment = storeListFragment
- } else if (id == R.id.nav_gallery) {
- } else if (id == R.id.nav_slideshow) {
- } else if (id == R.id.nav_manage) {
- } else if (id == R.id.nav_share) {
- } else if (id == R.id.nav_send) {
- }
- supportFragmentManager.beginTransaction().replace(R.id.container, fragment).commit()
- val drawer = findViewById(R.id.drawer_layout) as DrawerLayout
- drawer.closeDrawer(GravityCompat.START)
- return true
- }
- fun onLocationChanged(location: Location) {
- PopupUtil.dismissDialog()
- mLOcation = location
- mLocationManager!!.removeUpdates(this)
- Log.d("MainActivity",
- String.format("onLocationChanged: %f %f",
- location.getLatitude(),
- location.getLongitude()))
- loadStoreListFragment()
- }
- override fun onStatusChanged(s: String, i: Int, bundle: Bundle) {
- }
- override fun onProviderEnabled(s: String) {
- }
- override fun onProviderDisabled(s: String) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement