Skip to content

Commit

Permalink
feat: add fab while direct editing to switch to normal editing
Browse files Browse the repository at this point in the history
Signed-off-by: Álvaro Brey <[email protected]>
  • Loading branch information
AlvaroBrey committed Feb 27, 2023
1 parent 7872d55 commit 36fde04
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ import androidx.core.view.isVisible
import com.nextcloud.android.sso.helper.SingleAccountHelper
import io.reactivex.android.schedulers.AndroidSchedulers
import it.niedermann.owncloud.notes.BuildConfig
import it.niedermann.owncloud.notes.branding.Branded
import it.niedermann.owncloud.notes.branding.BrandingUtil
import it.niedermann.owncloud.notes.databinding.FragmentNoteDirectEditBinding
import it.niedermann.owncloud.notes.persistence.DirectEditingRepository
import it.niedermann.owncloud.notes.persistence.entity.Note
import it.niedermann.owncloud.notes.shared.model.ISyncCallback
import it.niedermann.owncloud.notes.shared.util.ExtendedFabUtil
import it.niedermann.owncloud.notes.shared.util.ExtendedFabUtil.toggleVisibilityOnScroll

class NoteDirectEditFragment : BaseNoteFragment() {
class NoteDirectEditFragment : BaseNoteFragment(), Branded {
private var _binding: FragmentNoteDirectEditBinding? = null
private val binding: FragmentNoteDirectEditBinding
get() = _binding!!
Expand All @@ -44,6 +48,7 @@ class NoteDirectEditFragment : BaseNoteFragment() {
): View {
Log.d(TAG, "onCreateView() called")
_binding = FragmentNoteDirectEditBinding.inflate(inflater, container, false)
setupFab()
// TODO prepare webview
setupWebSettings(binding.noteWebview.settings)
// TODO remove this
Expand All @@ -63,6 +68,18 @@ class NoteDirectEditFragment : BaseNoteFragment() {
return binding.root
}

private fun setupFab() {
binding.plainEditingFab.isExtended = false
ExtendedFabUtil.toggleExtendedOnLongClick(binding.plainEditingFab)
binding.noteWebview.setOnScrollChangeListener { _, _, scrollY, _, oldScrollY ->
toggleVisibilityOnScroll(binding.plainEditingFab, scrollY, oldScrollY)
}
binding.plainEditingFab.setOnClickListener {
// TODO save note?
listener.changeMode(NoteFragmentListener.Mode.EDIT)
}
}

override fun onDestroyView() {
super.onDestroyView()
binding.noteWebview.destroy()
Expand Down Expand Up @@ -147,8 +164,8 @@ class NoteDirectEditFragment : BaseNoteFragment() {
}

override fun applyBrand(color: Int) {
// TODO check if any branding needed
// nothing for now
val util = BrandingUtil.of(color, requireContext())
util.material.themeExtendedFAB(binding.plainEditingFab)
}

companion object {
Expand Down Expand Up @@ -194,6 +211,7 @@ class NoteDirectEditFragment : BaseNoteFragment() {
activity?.runOnUiThread {
binding.progress.isVisible = false
binding.noteWebview.isVisible = true
binding.plainEditingFab.isVisible = true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,7 @@ protected void onScroll(int scrollY, int oldScrollY) {
super.onScroll(scrollY, oldScrollY);
if (getSearchNextButton() == null || getSearchNextButton().getVisibility() != View.VISIBLE) {
final ExtendedFloatingActionButton directFab = getDirectEditingButton();
if (oldScrollY > 0 && scrollY > oldScrollY && directFab.isShown()) {
ExtendedFabUtil.setExtendedFabVisibility(getDirectEditingButton(), false);
} else if (scrollY < oldScrollY && !directFab.isShown()) {
ExtendedFabUtil.setExtendedFabVisibility(getDirectEditingButton(), true);
}
ExtendedFabUtil.toggleVisibilityOnScroll(directFab, scrollY, oldScrollY);
}
}

Expand All @@ -74,14 +70,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
// TODO don't show fab if direct editing not available
final ExtendedFloatingActionButton directEditingButton = getDirectEditingButton();
directEditingButton.setExtended(false);
directEditingButton.setOnLongClickListener(v -> {
if (directEditingButton.isExtended()) {
directEditingButton.shrink();
} else {
directEditingButton.extend();
}
return true;
});
ExtendedFabUtil.toggleExtendedOnLongClick(directEditingButton);
directEditingButton.setOnClickListener(v -> {
listener.changeMode(NoteFragmentListener.Mode.DIRECT_EDIT);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.google.android.material.floatingactionbutton.ExtendedFloatingActionBu

object ExtendedFabUtil {
@JvmStatic
public fun setExtendedFabVisibility(
fun setExtendedFabVisibility(
extendedFab: ExtendedFloatingActionButton,
visibility: Boolean,
) {
Expand Down Expand Up @@ -36,4 +36,30 @@ object ExtendedFabUtil {
}
}
}

@JvmStatic
fun toggleExtendedOnLongClick(extendedFab: ExtendedFloatingActionButton) {
extendedFab.setOnLongClickListener { v: View? ->
if (extendedFab.isExtended) {
extendedFab.shrink()
} else {
extendedFab.extend()
}
true
}
}

@JvmStatic
fun toggleVisibilityOnScroll(
extendedFab: ExtendedFloatingActionButton,
scrollY: Int,
oldScrollY: Int,
) {
@Suppress("ConvertTwoComparisonsToRangeCheck")
if (oldScrollY > 0 && scrollY > oldScrollY && extendedFab.isShown) {
setExtendedFabVisibility(extendedFab, false)
} else if (scrollY < oldScrollY && !extendedFab.isShown) {
setExtendedFabVisibility(extendedFab, true)
}
}
}
11 changes: 11 additions & 0 deletions app/src/main/res/drawable/ic_notes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal"
android:autoMirrored="true">
<path
android:fillColor="@android:color/white"
android:pathData="M3,18h12v-2L3,16v2zM3,6v2h18L21,6L3,6zM3,13h18v-2L3,11v2z"/>
</vector>
22 changes: 20 additions & 2 deletions app/src/main/res/layout/fragment_note_direct_edit.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent">

<WebView
android:visibility="gone"
android:id="@+id/note_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="match_parent"
tools:visibility="visible" />

<ProgressBar
android:id="@+id/progress"
Expand All @@ -19,5 +21,21 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<!-- TODO FABs? Search?-->
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/plain_editing_fab"
style="?attr/floatingActionButtonPrimaryStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/spacer_2x"
android:contentDescription="@string/noteMode_plain_edit"
android:text="@string/noteMode_plain_edit"
android:visibility="gone"
app:backgroundTint="@color/defaultBrand"
app:icon="@drawable/ic_notes"
app:layout_anchor="@id/note_webview"
app:layout_anchorGravity="bottom|end"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:visibility="visible" />

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 36fde04

Please sign in to comment.