Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package net.opendasharchive.openarchive.features.main

import android.Manifest
import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
Expand All @@ -16,7 +14,7 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.widget.TooltipCompat
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewpager.widget.ViewPager
import androidx.viewpager2.widget.ViewPager2
import com.esafirm.imagepicker.features.ImagePickerLauncher
import com.google.android.material.snackbar.Snackbar
import com.google.android.material.snackbar.Snackbar.SnackbarLayout
Expand All @@ -33,13 +31,12 @@ import net.opendasharchive.openarchive.databinding.ActivityMainBinding
import net.opendasharchive.openarchive.db.Project
import net.opendasharchive.openarchive.db.Space
import net.opendasharchive.openarchive.features.core.BaseActivity
import net.opendasharchive.openarchive.features.folders.AddFolderActivity
import net.opendasharchive.openarchive.features.media.AddMediaDialogFragment
import net.opendasharchive.openarchive.features.media.Picker
import net.opendasharchive.openarchive.features.media.PreviewActivity
import net.opendasharchive.openarchive.features.onboarding.Onboarding23Activity
import net.opendasharchive.openarchive.features.onboarding.SpaceSetupActivity
import net.opendasharchive.openarchive.features.folders.AddFolderActivity
import net.opendasharchive.openarchive.upload.BroadcastManager
import net.opendasharchive.openarchive.upload.UploadManagerActivity
import net.opendasharchive.openarchive.upload.UploadService
import net.opendasharchive.openarchive.util.AlertHelper
Expand Down Expand Up @@ -81,31 +78,6 @@ class MainActivity : BaseActivity(), FolderAdapterListener, SpaceAdapterListener
updateBottomNavbar(value)
}

private val mCurrentFragment
get() = mPagerAdapter.getRegisteredMediaFragment(mCurrentItem)

private val mMessageReceiver: BroadcastReceiver = object : BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent) {
if (mCurrentItem >= mPagerAdapter.settingsIndex) return

val action = BroadcastManager.getAction(intent)
val mediaId = action?.mediaId ?: return

if (mediaId < 0) return

when (action) {
BroadcastManager.Action.Change -> {
mCurrentFragment?.updateItem(mediaId)
}

BroadcastManager.Action.Delete -> {
mCurrentFragment?.refresh()
}
}
}
}

private val mNewFolderResultLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if (it.resultCode == RESULT_OK) {
Expand Down Expand Up @@ -140,13 +112,10 @@ class MainActivity : BaseActivity(), FolderAdapterListener, SpaceAdapterListener
startActivity(Intent(this, UploadManagerActivity::class.java))
}

mPagerAdapter = ProjectAdapter(supportFragmentManager)
mPagerAdapter = ProjectAdapter(supportFragmentManager, lifecycle)
mBinding.pager.adapter = mPagerAdapter

// final int pageMargin = (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, 8, getResources() .getDisplayMetrics());
mBinding.pager.pageMargin = 0

mBinding.pager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
mBinding.pager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
override fun onPageScrolled(
position: Int, positionOffset: Float,
positionOffsetPixels: Int
Expand Down Expand Up @@ -223,11 +192,17 @@ class MainActivity : BaseActivity(), FolderAdapterListener, SpaceAdapterListener
true
}

supportFragmentManager.setFragmentResultListener(AddMediaDialogFragment.RESP_PHOTO_GALLERY, this) {
_, _ -> addClicked()
supportFragmentManager.setFragmentResultListener(
AddMediaDialogFragment.RESP_PHOTO_GALLERY,
this
) { _, _ ->
addClicked()
}
supportFragmentManager.setFragmentResultListener(AddMediaDialogFragment.RESP_FILES, this) {
_, _ -> addClicked(typeFiles = true)
supportFragmentManager.setFragmentResultListener(
AddMediaDialogFragment.RESP_FILES,
this
) { _, _ ->
addClicked(typeFiles = true)
}
}
}
Expand All @@ -246,14 +221,9 @@ class MainActivity : BaseActivity(), FolderAdapterListener, SpaceAdapterListener
override fun onResume() {
super.onResume()

BroadcastManager.register(this, mMessageReceiver)

refreshSpace()

if (mLastItem == mPagerAdapter.settingsIndex) {
// Display settings when returning from deeper setting activities.
mCurrentItem = mLastItem
}
mCurrentItem = mLastItem

if (Space.current?.host.isNullOrEmpty()) {
startActivity(Intent(this, Onboarding23Activity::class.java))
Expand Down Expand Up @@ -282,12 +252,6 @@ class MainActivity : BaseActivity(), FolderAdapterListener, SpaceAdapterListener
}
}

override fun onPause() {
super.onPause()

BroadcastManager.unregister(this, mMessageReceiver)
}

override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)

Expand All @@ -302,16 +266,7 @@ class MainActivity : BaseActivity(), FolderAdapterListener, SpaceAdapterListener
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.menu_delete -> {
AlertHelper.show(this, R.string.confirm_remove_media, null, buttons = listOf(
AlertHelper.positiveButton(R.string.remove) { _, _ ->
mCurrentFragment?.deleteSelected()
},
AlertHelper.negativeButton()))

}

return when (item.itemId) {
R.id.menu_folders -> {
// https://stackoverflow.com/questions/21796209/how-to-create-a-custom-navigation-drawer-in-android

Expand All @@ -320,10 +275,11 @@ class MainActivity : BaseActivity(), FolderAdapterListener, SpaceAdapterListener
} else {
mBinding.root.openDrawer(mBinding.folderBar)
}
true
}
}

return super.onOptionsItemSelected(item)
else -> super.onOptionsItemSelected(item)
}
}

fun updateAfterDelete(done: Boolean) {
Expand Down Expand Up @@ -360,12 +316,13 @@ class MainActivity : BaseActivity(), FolderAdapterListener, SpaceAdapterListener
private fun refreshProjects(setProjectId: Long? = null) {
val projects = Space.current?.projects ?: emptyList()

val project = projects.firstOrNull { it.id == setProjectId } ?: getSelectedProject()

mPagerAdapter.updateData(projects)

mBinding.pager.adapter = mPagerAdapter
mCurrentItem = mPagerAdapter.getIndex(project)

setProjectId?.let {
mCurrentItem = mPagerAdapter.getProjectIndexById(it, default = 0)
}

mFolderAdapter.update(projects)

Expand All @@ -376,7 +333,7 @@ class MainActivity : BaseActivity(), FolderAdapterListener, SpaceAdapterListener
val project = getSelectedProject()

if (project != null) {
mCurrentFragment?.refresh()
mPagerAdapter.notifyProjectChanged(project)

project.space?.setAvatar(mBinding.currentFolderIcon)
mBinding.currentFolderIcon.show()
Expand All @@ -401,8 +358,7 @@ class MainActivity : BaseActivity(), FolderAdapterListener, SpaceAdapterListener
mBinding.currentFolderCount.show()

mBinding.uploadEditButton.toggle(project.isUploading)
}
else {
} else {
mBinding.currentFolderCount.cloak()
mBinding.uploadEditButton.hide()
}
Expand Down Expand Up @@ -513,8 +469,7 @@ class MainActivity : BaseActivity(), FolderAdapterListener, SpaceAdapterListener
if (getSelectedProject() != null) {
if (typeFiles && Picker.canPickFiles(this)) {
Picker.pickFiles(mFilePickerLauncher)
}
else {
} else {
pickMedia()
}
} else {
Expand Down Expand Up @@ -548,4 +503,4 @@ class MainActivity : BaseActivity(), FolderAdapterListener, SpaceAdapterListener
mBinding.settingsButton.setIconResource(R.drawable.ic_settings)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package net.opendasharchive.openarchive.features.main

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import net.opendasharchive.openarchive.R
import net.opendasharchive.openarchive.databinding.FragmentMainMediaBinding
import net.opendasharchive.openarchive.databinding.ViewSectionBinding
import net.opendasharchive.openarchive.db.Collection
import net.opendasharchive.openarchive.db.Media
import net.opendasharchive.openarchive.db.MediaAdapter
import net.opendasharchive.openarchive.db.MediaViewHolder
import net.opendasharchive.openarchive.upload.BroadcastManager
import net.opendasharchive.openarchive.util.AlertHelper
import net.opendasharchive.openarchive.util.extensions.toggle
import kotlin.collections.set

Expand Down Expand Up @@ -39,6 +46,59 @@ class MainMediaFragment : Fragment() {

private lateinit var mBinding: FragmentMainMediaBinding

private val mMessageReceiver: BroadcastReceiver = object : BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent) {
val action = BroadcastManager.getAction(intent)
val mediaId = action?.mediaId ?: return

if (mediaId < 0) return

when (action) {
BroadcastManager.Action.Change -> {
updateItem(mediaId)
}

BroadcastManager.Action.Delete -> {
refresh()
}
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
}

override fun onStart() {
super.onStart()
BroadcastManager.register(requireContext(), mMessageReceiver)
}

override fun onStop() {
super.onStop()
BroadcastManager.unregister(requireContext(), mMessageReceiver)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.menu_delete -> {
AlertHelper.show(
requireContext(), R.string.confirm_remove_media, null, buttons = listOf(
AlertHelper.positiveButton(R.string.remove) { _, _ ->
deleteSelected()
},
AlertHelper.negativeButton()
)
)
true
}

else -> super.onOptionsItemSelected(item)
}
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand All @@ -63,8 +123,8 @@ class MainMediaFragment : Fragment() {
mCollections = ArrayList(Collection.getByProject(mProjectId))

// Remove all sections, which' collections don't exist anymore.
val toDelete = mAdapters.keys.filter {
id -> mCollections.firstOrNull { it.id == id } == null
val toDelete = mAdapters.keys.filter { id ->
mCollections.firstOrNull { it.id == id } == null
}.toMutableList()

mCollections.forEach { collection ->
Expand All @@ -82,8 +142,7 @@ class MainMediaFragment : Fragment() {
if (adapter != null) {
adapter.updateData(media)
holder?.setHeader(collection, media)
}
else if (media.isNotEmpty()) {
} else if (media.isNotEmpty()) {
val view = createMediaList(collection, media)

mBinding.mediaContainer.addView(view, 0)
Expand All @@ -94,7 +153,9 @@ class MainMediaFragment : Fragment() {
// while adding images.
deleteCollections(toDelete, false)

mBinding.addMediaHint.toggle(mCollections.isEmpty())
if (::mBinding.isInitialized) {
mBinding.addMediaHint.toggle(mCollections.isEmpty())
}
}

fun deleteSelected() {
Expand All @@ -106,8 +167,7 @@ class MainMediaFragment : Fragment() {

if (media.isEmpty()) {
toDelete.add(collection.id)
}
else {
} else {
mSection[collection.id]?.setHeader(collection, media)
}
}
Expand Down
Loading