Skip to content

Commit

Permalink
added the concept of states which will allow for loading prior states…
Browse files Browse the repository at this point in the history
… (when the user presses back)
  • Loading branch information
mickstar committed Jul 14, 2020
1 parent 00145e3 commit a01854d
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ interface Contract {

fun showBasicSyncAnimation()
fun hideBasicSyncAnimation()
fun highlightMenuItem(state: LibraryModelState)
}

interface Presenter {
Expand Down Expand Up @@ -85,6 +86,8 @@ interface Contract {
fun openTrash()
fun uploadAttachment(item: Item)
fun requestForceResync()
fun backButtonPressed()
fun addFilterState(query: String)
}

interface Model {
Expand Down Expand Up @@ -113,5 +116,6 @@ interface Contract {
fun usePersonalLibrary()
fun getGroupByTitle(groupTitle: String): GroupInfo?
fun removeFromRecentlyViewed(attachment: Item)
fun loadPriorState()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.GravityCompat
import androidx.core.view.iterator
import androidx.drawerlayout.widget.DrawerLayout
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.google.android.material.navigation.NavigationView
Expand Down Expand Up @@ -55,6 +56,8 @@ class LibraryActivity : AppCompatActivity(), Contract.View,
private lateinit var firebaseAnalytics: FirebaseAnalytics
private var itemView: ItemViewFragment? = null

// used to "uncheck" the last pressed menu item if we change via code.
private var currentPressedMenuItem: MenuItem? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -111,6 +114,51 @@ class LibraryActivity : AppCompatActivity(), Contract.View,
.setIcon(R.drawable.ic_folder_black_24dp).isCheckable = true
}

override fun highlightMenuItem(state: LibraryModelState){
/* Given some collection name / group name, highlight that corresponding item.
* Needed for back button, whereby the app loads a prior state and needs the UI
* to reflect this change with respect to the item selected. */

currentPressedMenuItem?.let {
it.isChecked = false
}

if (state.isUsingGroup()){
for (menuItem: MenuItem in sharedCollections){
if (menuItem.title.toString() == state.currentGroup.name){
menuItem.isChecked = true
currentPressedMenuItem = menuItem
break
}
}
return
}

val menuItem: MenuItem? = if (state.currentCollection == "all"){
val navigationView = findViewById<NavigationView>(R.id.nav_view_library)
navigationView.menu.findItem(R.id.my_library)
} else if (state.currentCollection == "unfiled_items"){
val navigationView = findViewById<NavigationView>(R.id.nav_view_library)
navigationView.menu.findItem(MENU_ID_UNFILED_ITEMS)
} else if (state.currentCollection == "zooforzotero_Trash"){
val navigationView = findViewById<NavigationView>(R.id.nav_view_library)
navigationView.menu.findItem(MENU_ID_TRASH)
} else {
// if it reaches here that means it's a collection.
val index = collectionKeyByMenuId.indexOfValue(state.currentCollection)
if (index >= 0){
val menuId = collectionKeyByMenuId.keyAt(index)
collectionsMenu.findItem(menuId)
} else {
null
}
}
menuItem?.isChecked = true
menuItem?.let {
currentPressedMenuItem = it
}
}

/* Shows a shared Collection (group) on the sidebar. */
override fun addSharedCollection(groupInfo: GroupInfo) {
val navigationView = findViewById<NavigationView>(R.id.nav_view_library)
Expand Down Expand Up @@ -195,9 +243,9 @@ class LibraryActivity : AppCompatActivity(), Contract.View,
// we will only search on submit for android 7<=
if (android.os.Build.VERSION.SDK_INT <= 24){
presenter.filterEntries(query)
return true
}
return false
presenter.addFilterState(query)
return true
}

override fun onQueryTextChange(query: String): Boolean {
Expand All @@ -216,6 +264,11 @@ class LibraryActivity : AppCompatActivity(), Contract.View,

override fun onNavigationItemSelected(item: MenuItem): Boolean {
Log.d("zotero", "pressed ${item.groupId} - ${item.itemId}")
currentPressedMenuItem?.let {
it.isChecked = false
}

currentPressedMenuItem = item
if (item.itemId == R.id.my_library) {
presenter.setCollection("all")
} else if (item.itemId == MENU_ID_UNFILED_ITEMS) {
Expand All @@ -232,6 +285,8 @@ class LibraryActivity : AppCompatActivity(), Contract.View,
}
val drawer = findViewById<DrawerLayout>(R.id.drawerlayout_library)
drawer.closeDrawer(GravityCompat.START)
// shouldnt be neccessary but there was a bug where android wouldn't highlight on certain occassions
item.isChecked =true
return true
}

Expand Down Expand Up @@ -583,15 +638,10 @@ class LibraryActivity : AppCompatActivity(), Contract.View,
searchView.isIconified = true
presenter.closeQuery()
}

if (doubleBackToExitPressedOnce) {
super.onBackPressed()
return
else {
presenter.backButtonPressed()
}

this.doubleBackToExitPressedOnce = true
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show()

Handler().postDelayed(Runnable { doubleBackToExitPressedOnce = false }, 2000)
}

Expand Down
Loading

0 comments on commit a01854d

Please sign in to comment.