create a config context extension

This commit is contained in:
tibbi 2017-01-13 23:36:05 +01:00
parent 59096651d6
commit ef28b40186
16 changed files with 51 additions and 56 deletions

View File

@ -15,6 +15,7 @@ import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.dialogs.ResizeDialog
import com.simplemobiletools.gallery.dialogs.SaveAsDialog
import com.simplemobiletools.gallery.extensions.config
import com.simplemobiletools.gallery.extensions.getRealPathFromURI
import com.theartofdev.edmodo.cropper.CropImageView
import kotlinx.android.synthetic.main.activity_edit.*

View File

@ -20,6 +20,7 @@ import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.adapters.DirectoryAdapter
import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask
import com.simplemobiletools.gallery.dialogs.ChangeSortingDialog
import com.simplemobiletools.gallery.extensions.config
import com.simplemobiletools.gallery.extensions.launchAbout
import com.simplemobiletools.gallery.extensions.launchCamera
import com.simplemobiletools.gallery.extensions.launchSettings

View File

@ -18,10 +18,7 @@ import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.adapters.MediaAdapter
import com.simplemobiletools.gallery.asynctasks.GetMediaAsynctask
import com.simplemobiletools.gallery.dialogs.ChangeSortingDialog
import com.simplemobiletools.gallery.extensions.getHumanizedFilename
import com.simplemobiletools.gallery.extensions.launchAbout
import com.simplemobiletools.gallery.extensions.launchCamera
import com.simplemobiletools.gallery.extensions.launchSettings
import com.simplemobiletools.gallery.extensions.*
import com.simplemobiletools.gallery.helpers.*
import com.simplemobiletools.gallery.models.Medium
import com.simplemobiletools.gallery.views.MyScalableRecyclerView

View File

@ -5,6 +5,7 @@ import android.view.View
import android.widget.AdapterView
import com.simplemobiletools.commons.extensions.updateTextColors
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.extensions.config
import kotlinx.android.synthetic.main.activity_settings.*
class SettingsActivity : SimpleActivity() {

View File

@ -2,13 +2,9 @@ package com.simplemobiletools.gallery.activities
import android.os.Bundle
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.gallery.helpers.Config
open class SimpleActivity : BaseSimpleActivity() {
lateinit var config: Config
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
config = Config.newInstance(applicationContext)
}
}

View File

@ -23,8 +23,8 @@ import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.activities.SimpleActivity
import com.simplemobiletools.gallery.dialogs.CopyDialog
import com.simplemobiletools.gallery.dialogs.RenameDirectoryDialog
import com.simplemobiletools.gallery.extensions.config
import com.simplemobiletools.gallery.extensions.createSelector
import com.simplemobiletools.gallery.helpers.Config
import com.simplemobiletools.gallery.models.Directory
import kotlinx.android.synthetic.main.directory_item.view.*
import kotlinx.android.synthetic.main.directory_tmb.view.*
@ -36,7 +36,7 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList<Direc
val multiSelector = MultiSelector()
val views = ArrayList<View>()
val config = Config.newInstance(activity)
val config = activity.config
var pinnedFolders = config.pinnedFolders
companion object {
@ -66,8 +66,8 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList<Direc
}
init {
foregroundColor = Config.newInstance(activity).primaryColor
backgroundColor = Config.newInstance(activity).backgroundColor
foregroundColor = config.primaryColor
backgroundColor = config.backgroundColor
}
val multiSelectorMode = object : ModalMultiSelectorCallback(multiSelector) {

View File

@ -21,7 +21,6 @@ import com.simplemobiletools.gallery.activities.SimpleActivity
import com.simplemobiletools.gallery.dialogs.CopyDialog
import com.simplemobiletools.gallery.dialogs.RenameFileDialog
import com.simplemobiletools.gallery.extensions.*
import com.simplemobiletools.gallery.helpers.Config
import com.simplemobiletools.gallery.models.Medium
import kotlinx.android.synthetic.main.photo_video_item.view.*
import kotlinx.android.synthetic.main.photo_video_tmb.view.*
@ -32,7 +31,7 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>,
RecyclerView.Adapter<MediaAdapter.ViewHolder>() {
val multiSelector = MultiSelector()
val views = ArrayList<View>()
val config = Config.newInstance(activity)
val config = activity.config
companion object {
var actMode: ActionMode? = null
@ -62,8 +61,8 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>,
}
init {
foregroundColor = Config.newInstance(activity).primaryColor
backgroundColor = Config.newInstance(activity).backgroundColor
foregroundColor = config.primaryColor
backgroundColor = config.backgroundColor
}
val multiSelectorMode = object : ModalMultiSelectorCallback(multiSelector) {

View File

@ -6,9 +6,9 @@ import com.simplemobiletools.commons.extensions.isGif
import com.simplemobiletools.commons.extensions.isImageFast
import com.simplemobiletools.commons.extensions.isVideoFast
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.extensions.config
import com.simplemobiletools.gallery.extensions.getHumanizedFilename
import com.simplemobiletools.gallery.extensions.getParents
import com.simplemobiletools.gallery.helpers.Config
import com.simplemobiletools.gallery.helpers.IMAGES
import com.simplemobiletools.gallery.helpers.SORT_BY_DATE_MODIFIED
import com.simplemobiletools.gallery.helpers.VIDEOS
@ -19,18 +19,13 @@ import java.util.*
class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, val isPickImage: Boolean,
val callback: (dirs: ArrayList<Directory>) -> Unit) : AsyncTask<Void, Void, ArrayList<Directory>>() {
lateinit var mConfig: Config
override fun onPreExecute() {
super.onPreExecute()
mConfig = Config.newInstance(context)
}
var config = context.config
override fun doInBackground(vararg params: Void): ArrayList<Directory> {
val directories = LinkedHashMap<String, Directory>()
val media = ArrayList<Medium>()
val showMedia = mConfig.showMedia
val fileSorting = mConfig.fileSorting
val showMedia = config.showMedia
val fileSorting = config.fileSorting
val parents = context.getParents(isPickImage, isPickVideo)
parents.mapNotNull { File(it).listFiles() }
@ -71,7 +66,7 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
directory.addSize(size)
} else {
var dirName = context.getHumanizedFilename(parentDir)
if (mConfig.getIsFolderHidden(parentDir)) {
if (config.getIsFolderHidden(parentDir)) {
dirName += " ${context.resources.getString(R.string.hidden)}"
}
@ -82,7 +77,7 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
val dirs = ArrayList(directories.values.filter { File(it.path).exists() })
filterDirectories(dirs)
Directory.sorting = mConfig.directorySorting
Directory.sorting = config.directorySorting
dirs.sort()
return movePinnedToFront(dirs)
@ -90,7 +85,7 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
private fun movePinnedToFront(dirs: ArrayList<Directory>): ArrayList<Directory> {
val foundFolders = ArrayList<Directory>()
val pinnedFolders = mConfig.pinnedFolders
val pinnedFolders = config.pinnedFolders
dirs.forEach { if (pinnedFolders.contains(it.path)) foundFolders.add(it) }
dirs.removeAll(foundFolders)
@ -104,14 +99,14 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
}
private fun filterDirectories(dirs: MutableList<Directory>) {
if (!mConfig.showHiddenFolders) {
if (!config.showHiddenFolders) {
removeHiddenFolders(dirs)
removeNoMediaFolders(dirs)
}
}
private fun removeHiddenFolders(dirs: MutableList<Directory>) {
val hiddenDirs = mConfig.hiddenFolders
val hiddenDirs = config.hiddenFolders
val ignoreDirs = dirs.filter { hiddenDirs.contains(it.path) }
dirs.removeAll(ignoreDirs)
}

View File

@ -5,8 +5,12 @@ import android.os.AsyncTask
import com.simplemobiletools.commons.extensions.isGif
import com.simplemobiletools.commons.extensions.isImageFast
import com.simplemobiletools.commons.extensions.isVideoFast
import com.simplemobiletools.gallery.extensions.config
import com.simplemobiletools.gallery.extensions.getParents
import com.simplemobiletools.gallery.helpers.*
import com.simplemobiletools.gallery.helpers.IMAGES
import com.simplemobiletools.gallery.helpers.IMAGES_AND_VIDEOS
import com.simplemobiletools.gallery.helpers.SORT_BY_DATE_MODIFIED
import com.simplemobiletools.gallery.helpers.VIDEOS
import com.simplemobiletools.gallery.models.Medium
import java.io.File
import java.util.*
@ -14,15 +18,14 @@ import java.util.*
class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo: Boolean = false, val isPickImage: Boolean = false,
val showAll: Boolean, val callback: (media: ArrayList<Medium>) -> Unit) :
AsyncTask<Void, Void, ArrayList<Medium>>() {
lateinit var mConfig: Config
var config = context.config
var showMedia = IMAGES_AND_VIDEOS
var fileSorting = 0
override fun onPreExecute() {
super.onPreExecute()
mConfig = Config.newInstance(context)
showMedia = mConfig.showMedia
fileSorting = mConfig.fileSorting
showMedia = config.showMedia
fileSorting = config.fileSorting
}
override fun doInBackground(vararg params: Void): ArrayList<Medium> {

View File

@ -7,6 +7,7 @@ import android.view.View
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.activities.SimpleActivity
import com.simplemobiletools.gallery.extensions.config
import com.simplemobiletools.gallery.helpers.*
import kotlinx.android.synthetic.main.dialog_change_sorting.view.*
@ -19,7 +20,7 @@ class ChangeSortingDialog(val activity: SimpleActivity, val isDirectorySorting:
}
init {
config = Config.newInstance(activity)
config = activity.config
view = LayoutInflater.from(activity).inflate(R.layout.dialog_change_sorting, null)
AlertDialog.Builder(activity)

View File

@ -8,7 +8,7 @@ import com.simplemobiletools.commons.asynctasks.CopyMoveTask
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.activities.SimpleActivity
import com.simplemobiletools.gallery.helpers.Config
import com.simplemobiletools.gallery.extensions.config
import kotlinx.android.synthetic.main.dialog_copy_move.view.*
import java.io.File
import java.util.*
@ -65,11 +65,10 @@ class CopyDialog(val activity: SimpleActivity, val files: ArrayList<File>, val c
return@setOnClickListener
}
val config = Config.newInstance(context)
if (view.dialog_radio_group.checkedRadioButtonId == R.id.dialog_radio_copy) {
context.toast(R.string.copying)
val pair = Pair<ArrayList<File>, File>(files, destinationDir)
CopyMoveTask(context, false, config.treeUri, true, copyMoveListener).execute(pair)
CopyMoveTask(context, false, context.config.treeUri, true, copyMoveListener).execute(pair)
dismiss()
} else {
if (context.isPathOnSD(sourcePath) || context.isPathOnSD(destinationPath)) {
@ -79,7 +78,7 @@ class CopyDialog(val activity: SimpleActivity, val files: ArrayList<File>, val c
context.toast(R.string.moving)
val pair = Pair<ArrayList<File>, File>(files, destinationDir)
CopyMoveTask(context, true, config.treeUri, true, copyMoveListener).execute(pair)
CopyMoveTask(context, true, context.config.treeUri, true, copyMoveListener).execute(pair)
dismiss()
} else {
val updatedFiles = ArrayList<File>(files.size * 2)

View File

@ -10,7 +10,7 @@ import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.activities.SimpleActivity
import com.simplemobiletools.gallery.adapters.DirectoryAdapter
import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask
import com.simplemobiletools.gallery.helpers.Config
import com.simplemobiletools.gallery.extensions.config
import kotlinx.android.synthetic.main.dialog_album_picker.view.*
class PickAlbumDialog(val activity: SimpleActivity, val callback: (path: String) -> Unit) {
@ -39,7 +39,7 @@ class PickAlbumDialog(val activity: SimpleActivity, val callback: (path: String)
fun showOtherFolder() {
val initialPath = Environment.getExternalStorageDirectory().toString()
val showHidden = Config.newInstance(activity).showHiddenFolders
val showHidden = activity.config.showHiddenFolders
FilePickerDialog(activity, initialPath, false, showHidden, true) {
callback.invoke(it)
}

View File

@ -6,7 +6,7 @@ import android.view.WindowManager
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.activities.SimpleActivity
import com.simplemobiletools.gallery.helpers.Config
import com.simplemobiletools.gallery.extensions.config
import kotlinx.android.synthetic.main.rename_directory.view.*
import java.io.File
import java.util.*
@ -49,7 +49,7 @@ class RenameDirectoryDialog(val activity: SimpleActivity, val dir: File, val cal
if (activity.isShowingPermDialog(dir))
return@setOnClickListener
val document = context.getFileDocument(dir.absolutePath, Config.newInstance(context).treeUri)
val document = context.getFileDocument(dir.absolutePath, context.config.treeUri)
if (document.canWrite())
document.renameTo(newDirName)
sendSuccess(updatedFiles, newDir)

View File

@ -6,7 +6,7 @@ import android.view.WindowManager
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.activities.SimpleActivity
import com.simplemobiletools.gallery.helpers.Config
import com.simplemobiletools.gallery.extensions.config
import kotlinx.android.synthetic.main.rename_file.view.*
import java.io.File
@ -57,7 +57,7 @@ class RenameFileDialog(val activity: SimpleActivity, val file: File, val callbac
if (activity.isShowingPermDialog(file))
return@setOnClickListener
val document = context.getFileDocument(file.absolutePath, Config.newInstance(context).treeUri)
val document = context.getFileDocument(file.absolutePath, context.config.treeUri)
if (document.canWrite())
document.renameTo(newFile.name)
sendSuccess(file, newFile)

View File

@ -44,8 +44,8 @@ fun Context.launchSettings() {
fun Context.getParents(isPickImage: Boolean, isPickVideo: Boolean): ArrayList<String> {
val uri = MediaStore.Files.getContentUri("external")
val where = "${getWhereCondition(this, isPickImage, isPickVideo)} GROUP BY ( ${MediaStore.Files.FileColumns.PARENT} "
val args = getArgs(this, isPickImage, isPickVideo)
val where = "${getWhereCondition(isPickImage, isPickVideo)} GROUP BY ( ${MediaStore.Files.FileColumns.PARENT} "
val args = getArgs(isPickImage, isPickVideo)
val columns = arrayOf(MediaStore.Files.FileColumns.PARENT, MediaStore.Images.Media.DATA)
var cursor: Cursor? = null
val parents = ArrayList<String>()
@ -64,8 +64,8 @@ fun Context.getParents(isPickImage: Boolean, isPickVideo: Boolean): ArrayList<St
return parents
}
private fun getWhereCondition(context: Context, isPickImage: Boolean, isPickVideo: Boolean): String {
val showMedia = Config.newInstance(context).showMedia
fun Context.getWhereCondition(isPickImage: Boolean, isPickVideo: Boolean): String {
val showMedia = config.showMedia
return if ((isPickImage || showMedia == IMAGES) || (isPickVideo || showMedia == VIDEOS)) {
"${MediaStore.Files.FileColumns.MEDIA_TYPE} = ?)"
} else {
@ -73,8 +73,8 @@ private fun getWhereCondition(context: Context, isPickImage: Boolean, isPickVide
}
}
private fun getArgs(context: Context, isPickImage: Boolean, isPickVideo: Boolean): Array<String> {
val showMedia = Config.newInstance(context).showMedia
fun Context.getArgs(isPickImage: Boolean, isPickVideo: Boolean): Array<String> {
val showMedia = config.showMedia
return if (isPickImage || showMedia == IMAGES) {
arrayOf(MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE.toString())
} else if (isPickVideo || showMedia == VIDEOS) {
@ -83,3 +83,5 @@ private fun getArgs(context: Context, isPickImage: Boolean, isPickVideo: Boolean
arrayOf(MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE.toString(), MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO.toString())
}
}
val Context.config: Config get() = Config.newInstance(this)

View File

@ -15,9 +15,9 @@ import android.view.animation.AnimationUtils
import android.widget.SeekBar
import android.widget.TextView
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.extensions.config
import com.simplemobiletools.gallery.extensions.getNavBarHeight
import com.simplemobiletools.gallery.extensions.hasNavBar
import com.simplemobiletools.gallery.helpers.Config
import com.simplemobiletools.gallery.helpers.MEDIUM
import com.simplemobiletools.gallery.models.Medium
import kotlinx.android.synthetic.main.pager_video_item.view.*
@ -90,7 +90,7 @@ class VideoFragment : ViewPagerFragment(), View.OnClickListener, SurfaceHolder.C
super.setMenuVisibility(menuVisible)
mIsFragmentVisible = menuVisible
if (menuVisible) {
if (context != null && Config.newInstance(context).autoplayVideos) {
if (context != null && context.config.autoplayVideos) {
playVideo()
}
} else {
@ -276,7 +276,7 @@ class VideoFragment : ViewPagerFragment(), View.OnClickListener, SurfaceHolder.C
}
override fun onCompletion(mp: MediaPlayer) {
if (Config.newInstance(context).loopVideos) {
if (context.config.loopVideos) {
playVideo()
} else {
mSeekBar!!.progress = mSeekBar!!.max
@ -367,7 +367,7 @@ class VideoFragment : ViewPagerFragment(), View.OnClickListener, SurfaceHolder.C
setupTimeHolder()
setProgress(mCurrTime)
if (mIsFragmentVisible && Config.newInstance(context).autoplayVideos)
if (mIsFragmentVisible && context.config.autoplayVideos)
playVideo()
}
}