Created
May 8, 2017 09:51
-
-
Save t-kashima/2f6280cc9e516ccafea772148b85746e to your computer and use it in GitHub Desktop.
MainActivityの実装
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.unuuu.bookkeeper.activity | |
import android.content.Context | |
import android.content.Intent | |
import android.os.Bundle | |
import android.os.Handler | |
import android.support.design.widget.FloatingActionButton | |
import android.support.design.widget.NavigationView | |
import android.support.v4.app.Fragment | |
import android.support.v4.app.FragmentManager | |
import android.support.v4.view.GravityCompat | |
import android.support.v4.widget.DrawerLayout | |
import android.support.v7.app.ActionBarDrawerToggle | |
import android.support.v7.widget.AppCompatSpinner | |
import android.support.v7.widget.Toolbar | |
import android.view.MenuItem | |
import android.view.View | |
import android.widget.AdapterView | |
import butterknife.bindView | |
import com.unuuu.bookkeeper.R | |
import com.unuuu.bookkeeper.controller.SchemaController | |
import com.unuuu.bookkeeper.data.BookStatus | |
import com.unuuu.bookkeeper.event.BusHolder | |
import com.unuuu.bookkeeper.event.FabClickedEvent | |
import com.unuuu.bookkeeper.event.SpinnerChangedBookStatusEvent | |
import com.unuuu.bookkeeper.event.ToolbarClickedEvent | |
import com.unuuu.bookkeeper.extension.hide | |
import com.unuuu.bookkeeper.extension.hideKeyboard | |
import com.unuuu.bookkeeper.extension.show | |
import com.unuuu.bookkeeper.fragment.HomeFragment | |
import com.unuuu.bookkeeper.fragment.Page | |
import com.unuuu.bookkeeper.fragment.SetUpFragment | |
import com.unuuu.bookkeeper.fragment.presenter.MainContract | |
import com.unuuu.bookkeeper.view.adapter.HomeSpinnerAdapter | |
import timber.log.Timber | |
import javax.inject.Inject | |
class MainActivity : BaseActivity(), MainContract.View, | |
NavigationView.OnNavigationItemSelectedListener, | |
FragmentManager.OnBackStackChangedListener, | |
HomeFragment.OnFragmentInteractionListener { | |
companion object { | |
private const val DRAWER_CLOSE_DELAY_MILLS = 300L | |
fun getCallingIntent(context: Context): Intent = Intent(context, MainActivity::class.java) | |
fun getCallingIntent(context: Context, schemaName: String): Intent = Intent(context, MainActivity::class.java) | |
.putExtra(SchemaController.ARG_SCHEMA_NAME, schemaName) | |
} | |
@Inject | |
lateinit var activityNavigator: ActivityNavigator | |
@Inject | |
lateinit var presenter: MainContract.Actions | |
private val schemaController: SchemaController = SchemaController() | |
private val drawerLayout: DrawerLayout by bindView(R.id.activity_home_layout_drawer) | |
private val navigationView: NavigationView by bindView(R.id.activity_home_layout_nav) | |
private val toolbar: Toolbar by bindView(R.id.activity_home_toolbar) | |
private val spinner: AppCompatSpinner by bindView(R.id.activity_home_spinner) | |
private val fab: FloatingActionButton by bindView(R.id.activity_home_fab) | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
getComponent().inject(this) | |
setContentView(R.layout.activity_main) | |
presenter.onCreate(this) | |
schemaController.onCreate(this) | |
setSupportActionBar(toolbar) | |
supportActionBar?.run { | |
setDisplayHomeAsUpEnabled(true) | |
setDefaultDisplayHomeAsUpEnabled(true) | |
setDisplayShowTitleEnabled(false) | |
} | |
val toggle = object : ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close) { | |
override fun onDrawerOpened(drawerView: View?) { | |
super.onDrawerOpened(drawerView) | |
currentFocus?.let { | |
hideKeyboard(it) | |
} | |
} | |
} | |
drawerLayout.addDrawerListener(toggle) | |
toggle.syncState() | |
toolbar.setOnClickListener { | |
BusHolder.get().post(ToolbarClickedEvent()) | |
} | |
fab.setOnClickListener { | |
BusHolder.get().post(FabClickedEvent()) | |
} | |
val adapter = HomeSpinnerAdapter(this) | |
spinner.adapter = adapter | |
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { | |
override fun onNothingSelected(parent: AdapterView<*>?) { | |
} | |
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { | |
if (!spinner.isFocusable) { | |
spinner.isFocusable = true | |
return | |
} | |
Timber.d("アイテムを変更しました: " + position) | |
BusHolder.get().post(SpinnerChangedBookStatusEvent(adapter.getBookStatus(position))) | |
} | |
} | |
// 起動時にonItemSelectedが呼ばれてしまうので対応する | |
// http://d.hatena.ne.jp/nkawamura/20130824/1377345990 | |
spinner.isFocusable = false | |
navigationView.setNavigationItemSelectedListener(this) | |
navigationView.setCheckedItem(R.id.menu_drawer_home) | |
if (savedInstanceState == null) { | |
HomeFragment.newInstance().let { | |
supportFragmentManager | |
.beginTransaction() | |
.add(R.id.content, it, it.javaClass.simpleName) | |
.commit() | |
} | |
} | |
supportFragmentManager.addOnBackStackChangedListener(this) | |
} | |
override fun onResume() { | |
super.onResume() | |
val currentFragment = supportFragmentManager.findFragmentById(R.id.content) | |
presenter.onResume(Page.Home.menuId == Page.forName(currentFragment).menuId) | |
schemaController.onResume() | |
} | |
override fun showTutorial() { | |
activityNavigator.navigateToTutorial(this) | |
} | |
override fun onBackPressed() { | |
if (drawerLayout.isDrawerOpen(GravityCompat.START)) { | |
drawerLayout.closeDrawer(GravityCompat.START) | |
return | |
} | |
if (supportFragmentManager.backStackEntryCount > 0) { | |
supportFragmentManager.popBackStack() | |
return | |
} | |
super.onBackPressed() | |
} | |
override fun onNavigationItemSelected(item: MenuItem): Boolean { | |
drawerLayout.closeDrawer(GravityCompat.START) | |
val page = Page.forMenuId(item.itemId) | |
changePage(page) | |
return true | |
} | |
private fun replaceFragment(fragment: Fragment) { | |
supportFragmentManager.beginTransaction() | |
.replace(R.id.content, fragment) | |
.addToBackStack(null) | |
.commit() | |
} | |
fun changePageForSchema(page: Page) { | |
changePage(page) | |
} | |
private fun changePage(page: Page) { | |
val currentFragment = supportFragmentManager.findFragmentById(R.id.content) | |
if (page.menuId == Page.forName(currentFragment).menuId) { | |
return | |
} | |
Handler().postDelayed({ | |
supportActionBar?.run { | |
if (page.hasTitle) { | |
setTitle(page.titleResId) | |
} | |
setDisplayShowTitleEnabled(page.hasTitle) | |
} | |
if (page.hasTitle) { | |
spinner.hide() | |
} else { | |
spinner.setSelection(0) | |
spinner.show() | |
} | |
if (page.hasFab) { | |
fab.show() | |
} else { | |
fab.hide() | |
} | |
replaceFragment(page.createFragment()) | |
}, DRAWER_CLOSE_DELAY_MILLS) | |
} | |
override fun onDestroy() { | |
super.onDestroy() | |
schemaController.onDestroy() | |
supportFragmentManager.removeOnBackStackChangedListener(this) | |
} | |
override fun onBackStackChanged() { | |
val currentFragment = supportFragmentManager.findFragmentById(R.id.content) | |
if (currentFragment == null) { | |
moveTaskToBack(true) | |
return | |
} | |
val page = Page.forName(currentFragment) | |
navigationView.setCheckedItem(page.menuId) | |
supportActionBar?.run { | |
if (page.hasTitle) { | |
setTitle(page.titleResId) | |
} | |
setDisplayShowTitleEnabled(page.hasTitle) | |
} | |
if (page.hasTitle) { | |
spinner.hide() | |
} else { | |
spinner.show() | |
} | |
if (page.hasFab) { | |
fab.show() | |
} else { | |
fab.hide() | |
} | |
} | |
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | |
super.onActivityResult(requestCode, resultCode, data) | |
val fragment = supportFragmentManager.findFragmentById(R.id.content) ?: return | |
when (fragment) { | |
is SetUpFragment -> { | |
fragment.onActivityResult(requestCode, resultCode, data) | |
} | |
} | |
} | |
override fun updateSpinnerForBookStatus(bookStatus: BookStatus) { | |
if (spinner.selectedItemPosition == bookStatus.displayOrder) { | |
return | |
} | |
spinner.setSelection(bookStatus.displayOrder) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment