implement the Add and Remove favorite buttons

This commit is contained in:
tibbi 2017-03-18 17:04:13 +01:00
parent afd0411ee9
commit ad969698ae
3 changed files with 33 additions and 7 deletions

View File

@ -4,6 +4,7 @@ import android.content.Context
import com.simplemobiletools.commons.extensions.getInternalStoragePath
import com.simplemobiletools.commons.helpers.BaseConfig
import java.io.File
import java.util.*
class Config(context: Context) : BaseConfig(context) {
companion object {
@ -23,4 +24,20 @@ class Config(context: Context) : BaseConfig(context) {
return home
}
set(homeFolder) = prefs.edit().putString(HOME_FOLDER, homeFolder).apply()
fun addFavorite(path: String) {
val currFavorites = HashSet<String>(favorites)
currFavorites.add(path)
favorites = currFavorites
}
fun removeFavorite(path: String) {
val currFavorites = HashSet<String>(favorites)
currFavorites.remove(path)
favorites = currFavorites
}
var favorites: MutableSet<String>
get() = prefs.getStringSet(FAVORITES, HashSet<String>())
set(favorites) = prefs.edit().remove(FAVORITES).putStringSet(FAVORITES, favorites).apply()
}

View File

@ -6,3 +6,4 @@ val SCROLL_STATE = "scroll_state"
// shared preferences
val SHOW_HIDDEN = "show_hidden"
val HOME_FOLDER = "home_folder"
val FAVORITES = "favorites"

View File

@ -29,6 +29,7 @@ class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Br
var latestFragment: ItemsFragment? = null
var mScrollStates = HashMap<String, Parcelable>()
var mStoredTextColor = 0
var currentPath = ""
companion object {
private val STORAGE_PERMISSION = 1
@ -52,7 +53,7 @@ class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Br
if (mStoredTextColor != config.textColor) {
mStoredTextColor = config.textColor
breadcrumbs.setTextColor(mStoredTextColor)
openPath(getCurrenPath())
openPath(currentPath)
}
}
@ -96,10 +97,17 @@ class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Br
setListener(this@MainActivity)
supportFragmentManager.beginTransaction().replace(R.id.fragment_holder, this).addToBackStack(path).commitAllowingStateLoss()
}
currentPath = path
invalidateOptionsMenu()
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu, menu)
val favorites = config.favorites
menu.findItem(R.id.add_favorite).isVisible = !favorites.contains(currentPath)
menu.findItem(R.id.remove_favorite).isVisible = favorites.contains(currentPath)
return true
}
@ -121,20 +129,20 @@ class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Br
}
private fun addFavorite() {
config.addFavorite(currentPath)
invalidateOptionsMenu()
}
private fun removeFavorite() {
config.removeFavorite(currentPath)
invalidateOptionsMenu()
}
private fun setAsHome() {
config.homeFolder = getCurrenPath()
config.homeFolder = currentPath
toast(R.string.home_folder_updated)
}
private fun getCurrenPath() = (breadcrumbs.getChildAt(breadcrumbs.childCount - 1).tag as FileDirItem).path.trimEnd('/')
private fun launchAbout() {
startAboutActivity(R.string.app_name, LICENSE_KOTLIN or LICENSE_MULTISELECT, BuildConfig.VERSION_NAME)
}
@ -174,7 +182,7 @@ class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Br
override fun breadcrumbClicked(id: Int) {
if (id == 0) {
StoragePickerDialog(this@MainActivity, getCurrenPath()) {
StoragePickerDialog(this@MainActivity, currentPath) {
openPath(it)
}
} else {