2020-02-28 17:08:54 +01:00
|
|
|
package com.h.pixeldroid
|
|
|
|
|
2020-03-06 08:49:26 +01:00
|
|
|
import android.content.Intent
|
2020-02-28 17:08:54 +01:00
|
|
|
import android.os.Bundle
|
2020-03-05 21:07:10 +01:00
|
|
|
import android.view.MenuItem
|
2020-03-08 12:54:18 +01:00
|
|
|
import android.widget.Button
|
2020-03-12 22:23:25 +01:00
|
|
|
import android.widget.ImageButton
|
|
|
|
import android.widget.LinearLayout
|
2020-03-05 21:07:10 +01:00
|
|
|
import androidx.annotation.NonNull
|
2020-03-08 12:54:18 +01:00
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2020-03-05 21:07:10 +01:00
|
|
|
import androidx.core.view.GravityCompat
|
|
|
|
import androidx.drawerlayout.widget.DrawerLayout
|
2020-03-12 22:23:25 +01:00
|
|
|
import androidx.fragment.app.Fragment
|
2020-03-05 21:07:10 +01:00
|
|
|
import com.google.android.material.navigation.NavigationView
|
2020-03-12 22:23:25 +01:00
|
|
|
import com.h.pixeldroid.fragments.HomeFragment
|
|
|
|
import com.h.pixeldroid.fragments.ProfileFragment
|
|
|
|
import com.h.pixeldroid.motions.OnSwipeListener
|
2020-02-28 17:08:54 +01:00
|
|
|
|
2020-03-05 21:07:10 +01:00
|
|
|
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
|
|
|
|
|
|
|
|
private lateinit var drawerLayout: DrawerLayout
|
2020-02-28 17:08:54 +01:00
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
setContentView(R.layout.activity_main)
|
2020-03-05 21:07:10 +01:00
|
|
|
|
2020-03-12 22:23:25 +01:00
|
|
|
val mainLinearLayout : LinearLayout = findViewById(R.id.main_linear_layout)
|
|
|
|
val homepageButton : ImageButton = findViewById(R.id.activity_main_home_btn)
|
|
|
|
val accountButton : ImageButton = findViewById(R.id.activity_main_account_btn)
|
|
|
|
|
|
|
|
homepageButton.setOnClickListener {
|
|
|
|
launchFragment(HomeFragment())
|
|
|
|
}
|
|
|
|
accountButton.setOnClickListener {
|
|
|
|
launchFragment(ProfileFragment())
|
|
|
|
}
|
|
|
|
|
2020-03-05 21:48:25 +01:00
|
|
|
// Setup the drawer
|
2020-03-05 21:07:10 +01:00
|
|
|
drawerLayout = findViewById(R.id.drawer_layout)
|
|
|
|
val navigationView: NavigationView = findViewById(R.id.nav_view)
|
|
|
|
navigationView.setNavigationItemSelectedListener(this)
|
|
|
|
|
2020-03-12 22:23:25 +01:00
|
|
|
val onSwipeListener = object: OnSwipeListener(this) {
|
|
|
|
override fun onSwipeRight() = swipeRight()
|
|
|
|
override fun onSwipeLeft() = swipeLeft()
|
|
|
|
}
|
|
|
|
mainLinearLayout.setOnTouchListener(onSwipeListener)
|
|
|
|
|
|
|
|
// default fragment that displays when we open the app
|
|
|
|
launchFragment(HomeFragment())
|
|
|
|
}
|
2020-03-04 18:04:55 +01:00
|
|
|
|
2020-03-12 22:23:25 +01:00
|
|
|
private fun swipeRight() {
|
|
|
|
// TODO: correctly switch between tabs
|
|
|
|
drawerLayout.openDrawer(GravityCompat.START)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun swipeLeft() {
|
|
|
|
// TODO: correctly switch between tabs
|
|
|
|
supportFragmentManager.beginTransaction()
|
|
|
|
.setCustomAnimations(R.anim.slide_from_right, R.anim.slide_to_left)
|
|
|
|
.replace(R.id.fragment_container, ProfileFragment()).commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Launches the given fragment and put it as the current "activity"
|
|
|
|
*/
|
|
|
|
private fun launchFragment(fragment: Fragment) {
|
|
|
|
supportFragmentManager.beginTransaction().replace(R.id.fragment_container, fragment).commit()
|
2020-03-05 21:07:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-03-05 21:48:25 +01:00
|
|
|
When clicking in the drawer menu, go to the corresponding activity
|
2020-03-05 21:07:10 +01:00
|
|
|
*/
|
|
|
|
override fun onNavigationItemSelected(@NonNull item: MenuItem): Boolean {
|
|
|
|
when (item.itemId){
|
2020-03-08 12:54:18 +01:00
|
|
|
R.id.nav_settings -> launchActivity(SettingsActivity())
|
2020-03-12 22:23:25 +01:00
|
|
|
R.id.nav_account -> launchFragment(ProfileFragment())
|
2020-03-05 21:07:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
drawerLayout.closeDrawer(GravityCompat.START)
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-03-05 22:49:16 +01:00
|
|
|
/*
|
2020-03-08 20:41:01 +01:00
|
|
|
Launches the given activity and put it as the current one
|
2020-03-05 22:49:16 +01:00
|
|
|
*/
|
2020-03-08 12:54:18 +01:00
|
|
|
private fun launchActivity(activity: AppCompatActivity) {
|
|
|
|
val intent = Intent(this, activity::class.java)
|
|
|
|
startActivity(intent)
|
2020-03-05 22:49:16 +01:00
|
|
|
}
|
|
|
|
|
2020-03-05 21:07:10 +01:00
|
|
|
/*
|
2020-03-08 20:41:01 +01:00
|
|
|
Closes the drawer if we are clicking behind it
|
2020-03-05 21:07:10 +01:00
|
|
|
*/
|
|
|
|
override fun onBackPressed() {
|
|
|
|
if(drawerLayout.isDrawerOpen(GravityCompat.START)){
|
|
|
|
drawerLayout.closeDrawer(GravityCompat.START)
|
|
|
|
} else {
|
|
|
|
super.onBackPressed()
|
|
|
|
}
|
2020-02-28 17:08:54 +01:00
|
|
|
}
|
2020-03-04 18:04:55 +01:00
|
|
|
|
2020-02-28 17:08:54 +01:00
|
|
|
}
|