Skip to content

Commit

Permalink
adding the Recents tab
Browse files Browse the repository at this point in the history
  • Loading branch information
tibbi committed May 19, 2021
1 parent eb5e565 commit a50977c
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ import com.simplemobiletools.filemanager.pro.dialogs.ChangeSortingDialog
import com.simplemobiletools.filemanager.pro.dialogs.ChangeViewTypeDialog
import com.simplemobiletools.filemanager.pro.extensions.config
import com.simplemobiletools.filemanager.pro.extensions.tryOpenPathIntent
import com.simplemobiletools.filemanager.pro.fragments.MyViewPagerFragment
import com.simplemobiletools.filemanager.pro.helpers.MAX_COLUMN_COUNT
import com.simplemobiletools.filemanager.pro.helpers.RootHelpers
import com.simplemobiletools.filemanager.pro.helpers.TAB_FILES
import com.simplemobiletools.filemanager.pro.helpers.TAB_RECENTS
import com.stericson.RootTools.RootTools
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.items_fragment.*
import kotlinx.android.synthetic.main.items_fragment.view.*
import kotlinx.android.synthetic.main.recents_fragment.*
import java.io.File
import java.util.*

Expand Down Expand Up @@ -59,6 +63,7 @@ class MainActivity : SimpleActivity() {
handleAppPasswordProtection {
mWasProtectionHandled = it
if (it) {
initFragments()
mIsPasswordProtectionPending = false
tryInitFileManager()
checkWhatsNewDialog()
Expand All @@ -80,13 +85,13 @@ class MainActivity : SimpleActivity() {

if (storedFontSize != config.fontSize) {
getAllFragments().forEach {
it.updateFontSize()
it?.setupFontSize()
}
}

if (storedDateFormat != config.dateFormat || storedTimeFormat != getTimeFormat()) {
getAllFragments().forEach {
it.updateDateTimeFormat()
it?.setupDateTimeFormat()
}
}

Expand All @@ -97,6 +102,10 @@ class MainActivity : SimpleActivity() {
main_tabs_holder.background = ColorDrawable(config.backgroundColor)
main_tabs_holder.setSelectedTabIndicatorColor(getAdjustedPrimaryColor())
main_tabs_holder.getTabAt(main_view_pager.currentItem)?.icon?.applyColorFilter(getAdjustedPrimaryColor())

if (main_view_pager.adapter == null) {
initFragments()
}
}

override fun onPause() {
Expand All @@ -106,6 +115,7 @@ class MainActivity : SimpleActivity() {

override fun onDestroy() {
super.onDestroy()
config.lastUsedViewPagerPage = main_view_pager.currentItem
config.temporarilyShowHidden = false
}

Expand Down Expand Up @@ -172,10 +182,10 @@ class MainActivity : SimpleActivity() {
val path = savedInstanceState.getString(PICKED_PATH) ?: internalStoragePath

if (main_view_pager.adapter == null) {
initFragments()
main_view_pager.onGlobalLayout {
restorePath(path)
}
updateTabColors()
} else {
restorePath(path)
}
Expand Down Expand Up @@ -244,7 +254,7 @@ class MainActivity : SimpleActivity() {
checkOTGPath()
if (it) {
if (main_view_pager.adapter == null) {
main_view_pager.adapter = ViewPagerAdapter(this)
initFragments()
}

main_view_pager.onGlobalLayout {
Expand Down Expand Up @@ -287,6 +297,7 @@ class MainActivity : SimpleActivity() {

private fun initFragments() {
main_view_pager.adapter = ViewPagerAdapter(this)
main_view_pager.currentItem = config.lastUsedViewPagerPage
main_view_pager.onPageChangeListener {
main_tabs_holder.getTabAt(it)?.select()
invalidateOptionsMenu()
Expand Down Expand Up @@ -322,6 +333,13 @@ class MainActivity : SimpleActivity() {
}
}

private fun updateTabColors() {
getInactiveTabIndexes(main_view_pager.currentItem).forEach {
main_tabs_holder.getTabAt(it)?.icon?.applyColorFilter(config.textColor)
}
main_tabs_holder.getTabAt(main_view_pager.currentItem)?.icon?.applyColorFilter(getAdjustedPrimaryColor())
}

private fun checkOTGPath() {
ensureBackgroundThread {
if (!config.wasOTGHandled && hasPermission(PERMISSION_WRITE_STORAGE) && hasOTGConnected() && config.OTGPath.isEmpty()) {
Expand Down Expand Up @@ -512,9 +530,9 @@ class MainActivity : SimpleActivity() {
}
}

private fun getInactiveTabIndexes(activeIndex: Int) = arrayListOf(0, 1).filter { it != activeIndex }
private fun getInactiveTabIndexes(activeIndex: Int) = arrayListOf(TAB_FILES, TAB_RECENTS).filter { it != activeIndex }

private fun getAllFragments() = arrayListOf(items_fragment)
private fun getAllFragments(): ArrayList<MyViewPagerFragment?> = arrayListOf(items_fragment, recents_fragment)

private fun getCurrentFragment() = items_fragment

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor
import com.simplemobiletools.filemanager.pro.R
import com.simplemobiletools.filemanager.pro.activities.SimpleActivity
import com.simplemobiletools.filemanager.pro.extensions.config
import com.simplemobiletools.filemanager.pro.fragments.ItemsFragment
import com.simplemobiletools.filemanager.pro.fragments.MyViewPagerFragment
import com.simplemobiletools.filemanager.pro.helpers.TAB_FILES

class ViewPagerAdapter(val activity: SimpleActivity) : PagerAdapter() {
override fun instantiateItem(container: ViewGroup, position: Int): Any {
val layout = getFragment(position)
val view = activity.layoutInflater.inflate(layout, container, false)
container.addView(view)

(view as ItemsFragment).apply {
(view as MyViewPagerFragment).apply {
setupFragment(activity)
setupColors(activity.config.textColor, activity.getAdjustedPrimaryColor())
}
Expand All @@ -27,9 +28,12 @@ class ViewPagerAdapter(val activity: SimpleActivity) : PagerAdapter() {
container.removeView(item as View)
}

override fun getCount() = 1
override fun getCount() = 2

override fun isViewFromObject(view: View, item: Any) = view == item

private fun getFragment(position: Int) = R.layout.items_fragment
private fun getFragment(position: Int) = when (position) {
TAB_FILES -> R.layout.items_fragment
else -> R.layout.recents_fragment
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.simplemobiletools.filemanager.pro.fragments
import android.content.Context
import android.os.Parcelable
import android.util.AttributeSet
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.recyclerview.widget.GridLayoutManager
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.dialogs.StoragePickerDialog
Expand All @@ -30,7 +29,7 @@ import java.io.File
import java.util.*
import kotlin.collections.ArrayList

class ItemsFragment(context: Context, attributeSet: AttributeSet) : CoordinatorLayout(context, attributeSet), ItemOperationsListener, Breadcrumbs.BreadcrumbsListener {
class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet), ItemOperationsListener, Breadcrumbs.BreadcrumbsListener {
var currentPath = ""
var isGetContentIntent = false
var isGetRingtonePicker = false
Expand All @@ -47,7 +46,7 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : CoordinatorL

private var storedItems = ArrayList<ListItem>()

fun setupFragment(activity: SimpleActivity) {
override fun setupFragment(activity: SimpleActivity) {
if (this.activity == null) {
this.activity = activity
items_swipe_refresh.setOnRefreshListener { refreshItems() }
Expand All @@ -56,7 +55,7 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : CoordinatorL
}
}

fun setupColors(textColor: Int, adjustedPrimaryColor: Int) {
override fun setupColors(textColor: Int, adjustedPrimaryColor: Int) {
context!!.updateTextColors(this)
items_fastscroller.updatePrimaryColor()
storedItems = ArrayList()
Expand All @@ -69,12 +68,12 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : CoordinatorL
items_fastscroller.updateBubbleColors()
}

fun updateFontSize() {
override fun setupFontSize() {
getRecyclerAdapter()?.updateFontSizes()
breadcrumbs.updateFontSize(context!!.getTextSize())
}

fun updateDateTimeFormat() {
override fun setupDateTimeFormat() {
getRecyclerAdapter()?.updateDateTimeFormat()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.simplemobiletools.filemanager.pro.fragments

import android.content.Context
import android.util.AttributeSet
import android.widget.RelativeLayout
import com.simplemobiletools.filemanager.pro.activities.SimpleActivity

abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) : RelativeLayout(context, attributeSet) {
abstract fun setupFragment(activity: SimpleActivity)

abstract fun setupColors(textColor: Int, adjustedPrimaryColor: Int)

abstract fun setupFontSize()

abstract fun setupDateTimeFormat()
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,43 @@
package com.simplemobiletools.filemanager.pro.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.simplemobiletools.filemanager.pro.R

class RecentsFragment : Fragment() {
lateinit var mView: View

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
mView = inflater.inflate(R.layout.recents_fragment, container, false)!!
return mView
import android.content.Context
import android.provider.MediaStore
import android.util.AttributeSet
import com.simplemobiletools.commons.extensions.getIntValue
import com.simplemobiletools.commons.extensions.getStringValue
import com.simplemobiletools.commons.models.FileDirItem
import com.simplemobiletools.filemanager.pro.activities.SimpleActivity
import com.simplemobiletools.filemanager.pro.interfaces.ItemOperationsListener
import java.util.*

class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet), ItemOperationsListener {
override fun setupFragment(activity: SimpleActivity) {}

override fun setupColors(textColor: Int, adjustedPrimaryColor: Int) {}

private fun getRecents() {
val uri = MediaStore.Files.getContentUri("external")
val projection = arrayOf(MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.DATE_MODIFIED)
val sortOrder = "${MediaStore.Files.FileColumns.DATE_MODIFIED} DESC LIMIT 20"
val cursor = context?.contentResolver?.query(uri, projection, null, null, sortOrder)

cursor?.use {
if (cursor.moveToFirst()) {
do {
val path = cursor.getStringValue(MediaStore.Files.FileColumns.DATA)
val modified = cursor.getIntValue(MediaStore.Files.FileColumns.DATE_MODIFIED)
} while (cursor.moveToNext())
}
}
}

override fun refreshItems() {}

override fun deleteFiles(files: ArrayList<FileDirItem>) {}

override fun selectedPaths(paths: ArrayList<String>) {}

override fun setupFontSize() {}

override fun setupDateTimeFormat() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ const val OPEN_AS_IMAGE = 2
const val OPEN_AS_AUDIO = 3
const val OPEN_AS_VIDEO = 4
const val OPEN_AS_OTHER = 5

const val TAB_FILES = 0
const val TAB_RECENTS = 1
6 changes: 6 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
android:layout_height="wrap_content"
android:icon="@drawable/ic_folder_vector" />

<com.google.android.material.tabs.TabItem
android:id="@+id/tab_recents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/ic_clock_vector" />

</com.google.android.material.tabs.TabLayout>

<com.simplemobiletools.commons.views.MyViewPager
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/res/layout/recents_fragment.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<com.simplemobiletools.filemanager.pro.fragments.RecentsFragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/recents_wrapper"
android:id="@+id/recents_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent">

Expand All @@ -28,4 +28,4 @@
android:scrollbars="none"
app:layoutManager="com.simplemobiletools.commons.views.MyGridLayoutManager" />

</RelativeLayout>
</com.simplemobiletools.filemanager.pro.fragments.RecentsFragment>

0 comments on commit a50977c

Please sign in to comment.