Skip to content

Commit

Permalink
Add 'Select Place' button in Explore screen
Browse files Browse the repository at this point in the history
* See issue #1.
  • Loading branch information
kyukyukyu committed Jun 7, 2017
1 parent b309243 commit e96b58a
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 28 deletions.
31 changes: 10 additions & 21 deletions app/src/main/java/kr/ac/kaist/launduler/ExploreActivity.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package kr.ac.kaist.launduler

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.design.widget.FloatingActionButton
import android.support.design.widget.Snackbar
import android.view.View
import android.support.design.widget.NavigationView
import android.support.v4.app.Fragment
import android.support.v4.view.GravityCompat
import android.support.v4.widget.DrawerLayout
import android.support.v7.app.ActionBarDrawerToggle
Expand All @@ -27,6 +23,8 @@ import java.util.*

class ExploreActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

lateinit var fragment: Fragment

companion object {
const val REQUEST_SELECT_PLACE = 1
}
Expand All @@ -38,7 +36,7 @@ class ExploreActivity : AppCompatActivity(), NavigationView.OnNavigationItemSele
setSupportActionBar(toolbar)

if (savedInstanceState == null) {
val fragment = ExploreFragment.newInstance()
fragment = ExploreFragment.newInstance()
val fragmentManager = supportFragmentManager
fragmentManager
.beginTransaction()
Expand Down Expand Up @@ -67,22 +65,13 @@ class ExploreActivity : AppCompatActivity(), NavigationView.OnNavigationItemSele

override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.explore, menu)
return true
}

override 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.itemId

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true
if (fragment is OptionsMenuFragment) {
fragment.setHasOptionsMenu(true)
menuInflater.inflate((fragment as OptionsMenuFragment).menuResId, menu)
} else {
menu.clear()
}

return super.onOptionsItemSelected(item)
return true
}

@SuppressWarnings("StatementWithEmptyBody")
Expand Down
25 changes: 22 additions & 3 deletions app/src/main/java/kr/ac/kaist/launduler/ExploreFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.os.Bundle
import android.support.design.widget.Snackbar
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import com.trello.rxlifecycle2.components.support.RxFragment
Expand All @@ -20,7 +21,9 @@ import kr.ac.kaist.launduler.services.laundulerService
* Use the [ExploreFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class ExploreFragment : RxFragment() {
class ExploreFragment : RxFragment(), OptionsMenuFragment {
override val menuResId: Int = R.menu.explore
var selectedPlaceId = -1L

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
Expand All @@ -29,15 +32,16 @@ class ExploreFragment : RxFragment() {

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)

// Load selected place ID from shared preferences or have it set if it's not set.
val sharedPreferences =
activity.getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE)
val strSelectedPlaceId = getString(R.string.selected_place_id)
if (sharedPreferences.contains(strSelectedPlaceId)) {
val placeId = sharedPreferences.getLong(strSelectedPlaceId, -1)
retrieveMachineList(placeId)
} else {
val intent = Intent(activity, ExploreSelectPlaceActivity::class.java)
startActivityForResult(intent, REQUEST_SELECT_PLACE)
selectPlace()
}
}

Expand All @@ -53,6 +57,7 @@ class ExploreFragment : RxFragment() {
if (placeId < 0) {
throw IllegalArgumentException("invalid place ID")
}
selectedPlaceId = placeId
val sharedPreferences =
activity.getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE)
val strSelectedPlaceId = getString(R.string.selected_place_id)
Expand All @@ -67,6 +72,20 @@ class ExploreFragment : RxFragment() {
fun retrieveMachineList(placeId: Long) {
}

private fun selectPlace() {
val intent = Intent(activity, ExploreSelectPlaceActivity::class.java)
intent.putExtra(EXTRA_PLACE_ID, selectedPlaceId)
startActivityForResult(intent, REQUEST_SELECT_PLACE)
}

override fun onOptionsItemSelected(item: MenuItem?): Boolean {
when (item?.itemId) {
R.id.action_select_place -> selectPlace()
else -> return super.onOptionsItemSelected(item)
}
return true
}

companion object {

/**
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/kr/ac/kaist/launduler/OptionsMenuFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package kr.ac.kaist.launduler

import android.view.MenuItem

/**
* Created by kyukyukyu on 06/06/2017.
*/
interface OptionsMenuFragment {
val menuResId: Int
}
Binary file added app/src/main/res/drawable-hdpi/ic_select_place.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-mdpi/ic_select_place.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xhdpi/ic_select_place.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions app/src/main/res/menu/explore.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
android:id="@+id/action_select_place"
android:title="@string/select_place"
android:icon="@drawable/ic_select_place"
app:showAsAction="always" />
</menu>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,7 @@

<string name="preference_file_key">kr.ac.kaist.launduler.PREFERENCE_FILE_KEY</string>
<string name="selected_place_id">SELECTED_PLACE_ID</string>

<!-- Explore menu -->
<string name="action_select_place">Select Place</string>
</resources>

0 comments on commit e96b58a

Please sign in to comment.