SHARE
TWEET

MainActivity

a guest Sep 17th, 2019 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener, LocationListener {
  3.  
  4.     private var mLocationManager: LocationManager? = null
  5.     private var mLOcation: Location? = null
  6.     private var storeListFragment: Fragment? = null
  7.  
  8.     override fun onCreate(savedInstanceState: Bundle?) {
  9.         super.onCreate(savedInstanceState)
  10.         setContentView(R.layout.activity_main)
  11.         val toolbar = findViewById(R.id.toolbar) as Toolbar
  12.         setSupportActionBar(toolbar)
  13.  
  14.         val fab = findViewById(R.id.fab) as FloatingActionButton
  15.         fab.setOnClickListener(object : View.OnClickListener() {
  16.             fun onClick(view: View) {
  17.                 Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
  18.                         .setAction("Action", null).show()
  19.             }
  20.         })
  21.  
  22.         val drawer = findViewById(R.id.drawer_layout) as DrawerLayout
  23.         val toggle = ActionBarDrawerToggle(
  24.                 this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
  25.         drawer.setDrawerListener(toggle)
  26.         toggle.syncState()
  27.  
  28.         val navigationView = findViewById(R.id.nav_view) as NavigationView
  29.         navigationView.setNavigationItemSelectedListener(this)
  30.         navigationView.setCheckedItem(0)
  31.  
  32.         mLocationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
  33.  
  34.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  35.  
  36.             // cek izin
  37.             if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  38.                 //minta izin
  39.                 ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
  40.                         1)
  41.  
  42.                 return
  43.             }
  44.         }
  45.  
  46.         mLocationManager!!.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
  47.                 10,
  48.                 10f, this)
  49.  
  50.         PopupUtil.showLoading(this, "", "Finding your location ...")
  51.     }
  52.  
  53.     private fun loadStoreListFragment() {
  54.         storeListFragment = StoreListFragment()
  55.         supportFragmentManager.beginTransaction().replace(R.id.container, storeListFragment).commit()
  56.     }
  57.  
  58.     override fun onRequestPermissionsResult(requestCode: Int,
  59.                                             permissions: Array<String>,
  60.                                             grantResults: IntArray) {
  61.         if (requestCode == 1) {
  62.             if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  63.                 //diizinkan
  64.                 // baru minta location
  65.                 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  66.  
  67.                     return
  68.                 }
  69.                 mLocationManager!!.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
  70.                         10,
  71.                         10f, this)
  72.                 PopupUtil.showLoading(this, "", "Finding your location ...")
  73.  
  74.             }
  75.         }
  76.     }
  77.  
  78.     override fun onBackPressed() {
  79.         val drawer = findViewById(R.id.drawer_layout) as DrawerLayout
  80.         if (drawer.isDrawerOpen(GravityCompat.START)) {
  81.             drawer.closeDrawer(GravityCompat.START)
  82.         } else {
  83.             super.onBackPressed()
  84.         }
  85.     }
  86.  
  87.     fun onCreateOptionsMenu(menu: Menu): Boolean {
  88.         // Inflate the menu; this adds items to the action bar if it is present.
  89.         menuInflater.inflate(R.menu.main, menu)
  90.         return true
  91.     }
  92.  
  93.     fun onOptionsItemSelected(item: MenuItem): Boolean {
  94.         // Handle action bar item clicks here. The action bar will
  95.         // automatically handle clicks on the Home/Up button, so long
  96.         // as you specify a parent activity in AndroidManifest.xml.
  97.         val id = item.getItemId()
  98.  
  99.  
  100.         return if (id == R.id.action_settings) {
  101.             true
  102.         } else super.onOptionsItemSelected(item)
  103.  
  104.     }
  105.  
  106.     fun onNavigationItemSelected(item: MenuItem): Boolean {
  107.         // Handle navigation view item clicks here.
  108.         val id = item.getItemId()
  109.  
  110.         if (storeListFragment == null) {
  111.             storeListFragment = StoreListFragment()
  112.         }
  113.  
  114.         var fragment: Fragment? = null
  115.  
  116.         if (id == R.id.nav_camera) {
  117.             fragment = storeListFragment
  118.         } else if (id == R.id.nav_gallery) {
  119.  
  120.         } else if (id == R.id.nav_slideshow) {
  121.  
  122.         } else if (id == R.id.nav_manage) {
  123.  
  124.         } else if (id == R.id.nav_share) {
  125.  
  126.         } else if (id == R.id.nav_send) {
  127.  
  128.         }
  129.  
  130.         supportFragmentManager.beginTransaction().replace(R.id.container, fragment).commit()
  131.  
  132.         val drawer = findViewById(R.id.drawer_layout) as DrawerLayout
  133.         drawer.closeDrawer(GravityCompat.START)
  134.         return true
  135.     }
  136.  
  137.     fun onLocationChanged(location: Location) {
  138.         PopupUtil.dismissDialog()
  139.         mLOcation = location
  140.         mLocationManager!!.removeUpdates(this)
  141.  
  142.         Log.d("MainActivity",
  143.                 String.format("onLocationChanged: %f %f",
  144.                         location.getLatitude(),
  145.                         location.getLongitude()))
  146.  
  147.         loadStoreListFragment()
  148.     }
  149.  
  150.     override fun onStatusChanged(s: String, i: Int, bundle: Bundle) {
  151.  
  152.     }
  153.  
  154.     override fun onProviderEnabled(s: String) {
  155.  
  156.     }
  157.  
  158.     override fun onProviderDisabled(s: String) {
  159.  
  160.     }
  161. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top