Skip to content

Commit

Permalink
[enhancement] Modify notes loading flow in Home screen with added loa…
Browse files Browse the repository at this point in the history
…ding animation
  • Loading branch information
tuancoltech committed May 22, 2024
1 parent c5e1169 commit e0ebd30
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
Expand Down Expand Up @@ -98,7 +97,6 @@ class NotesFragment: Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = FragmentHomeBinding.inflate(layoutInflater)
notesViewModel.apply { init { readAllNotes() } }
}

override fun onCreateView(
Expand All @@ -120,51 +118,57 @@ class NotesFragment: Fragment() {
}
showingFloatingButtons = false
initBottomControlViews()
lifecycleScope.launchWhenStarted {
notesViewModel.getNotes {
notes = it
val adapter = NotesListAdapter(
it,
onPlayPauseClick = { path, pos, onStop ->
arkMediaPlayerViewModel.onPlayOrPauseClick(path, pos, onStop)
},
onThumbPrepare = { graphicNote, noteCanvas ->
val tempNoteViewModel: GraphicNotesViewModel by viewModels()
noteCanvas.setViewModel(viewModel = /*graphicNotesViewModel*/tempNoteViewModel)

initEmptyStateViews()

binding.pbLoading.visible()
notesViewModel.apply {
init { readAllNotes {
onNotesLoaded(it)
} }
}
}

private fun onNotesLoaded(notes: List<Note>) {
binding.pbLoading.gone()
val adapter = NotesListAdapter(
notes,
onPlayPauseClick = { path, pos, onStop ->
arkMediaPlayerViewModel.onPlayOrPauseClick(path, pos, onStop)
},
onThumbPrepare = { graphicNote, noteCanvas ->
val tempNoteViewModel: GraphicNotesViewModel by viewModels()
noteCanvas.setViewModel(viewModel = /*graphicNotesViewModel*/tempNoteViewModel)
// tempNoteViewModel.onNoteOpened(graphicNote)

}
)
val layoutManager = LinearLayoutManager(requireContext())
arkMediaPlayerViewModel.collect(
stateToUI = { state -> adapter.observeItemState = { state } },
handleSideEffect = { effect -> adapter.observeItemSideEffect = { effect } }
)
adapter.setActivity(activity)
adapter.setFragmentManager(childFragmentManager)
binding.rvPinnedNotes.apply {
this.layoutManager = layoutManager
this.adapter = adapter
}
ItemTouchHelper(mItemTouchCallback).attachToRecyclerView(binding.rvPinnedNotes)

if (it.isNotEmpty()) {
binding.layoutBottomControl.visible()
binding.groupEmptyState.gone()
binding.rvPinnedNotes.visible()
binding.edtSearch.visible()
binding.scrollViewNotes.visible()
} else {
binding.layoutBottomControl.gone()
binding.groupEmptyState.visible()
binding.rvPinnedNotes.gone()
binding.edtSearch.gone()
binding.scrollViewNotes.gone()
}
}
)
val layoutManager = LinearLayoutManager(requireContext())
arkMediaPlayerViewModel.collect(
stateToUI = { state -> adapter.observeItemState = { state } },
handleSideEffect = { effect -> adapter.observeItemSideEffect = { effect } }
)
adapter.setActivity(activity)
adapter.setFragmentManager(childFragmentManager)
binding.rvPinnedNotes.apply {
this.layoutManager = layoutManager
this.adapter = adapter
}
ItemTouchHelper(mItemTouchCallback).attachToRecyclerView(binding.rvPinnedNotes)

initEmptyStateViews()
if (notes.isNotEmpty()) {
binding.layoutBottomControl.visible()
binding.groupEmptyState.gone()
binding.rvPinnedNotes.visible()
binding.edtSearch.visible()
binding.scrollViewNotes.visible()
} else {
binding.layoutBottomControl.gone()
binding.groupEmptyState.visible()
binding.rvPinnedNotes.gone()
binding.edtSearch.gone()
binding.scrollViewNotes.gone()
}
}

override fun onResume() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import kotlinx.coroutines.withContext
import dev.arkbuilders.arkmemo.models.SaveNoteResult
import dev.arkbuilders.arkmemo.repo.NotesRepo
import dev.arkbuilders.arkmemo.di.IO_DISPATCHER
import dev.arkbuilders.arkmemo.media.ArkMediaPlayer
import dev.arkbuilders.arkmemo.models.GraphicNote
import dev.arkbuilders.arkmemo.models.Note
import dev.arkbuilders.arkmemo.models.TextNote
Expand All @@ -29,8 +28,7 @@ class NotesViewModel @Inject constructor(
@Named(IO_DISPATCHER) private val iODispatcher: CoroutineDispatcher,
private val textNotesRepo: NotesRepo<TextNote>,
private val graphicNotesRepo: NotesRepo<GraphicNote>,
private val voiceNotesRepo: NotesRepo<VoiceNote>,
private val arkMediaPlayer: ArkMediaPlayer
private val voiceNotesRepo: NotesRepo<VoiceNote>
) : ViewModel() {

private val notes = MutableStateFlow(listOf<Note>())
Expand All @@ -48,9 +46,14 @@ class NotesViewModel @Inject constructor(
}
}

fun readAllNotes() {
fun readAllNotes(onSuccess: (notes: List<Note>) -> Unit) {
viewModelScope.launch(iODispatcher) {
notes.value = textNotesRepo.read() + graphicNotesRepo.read() + voiceNotesRepo.read()
notes.collectLatest {
withContext(Dispatchers.Main) {
onSuccess(it)
}
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/drawable/ic_animated_loading.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:drawable="@drawable/ic_loading"
android:fillAfter="true"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" />
</item>
</layer-list>
13 changes: 13 additions & 0 deletions app/src/main/res/drawable/ic_loading.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="73dp"
android:height="72dp"
android:viewportWidth="73"
android:viewportHeight="72">
<path
android:pathData="M36.5,6V18M36.5,54V66M18.5,36H6.5M66.5,36H54.5M57.735,57.235L49.25,48.75M57.735,15L49.25,23.485M15.265,57.235L23.75,48.75M15.265,15L23.75,23.485"
android:strokeLineJoin="round"
android:strokeWidth="4"
android:fillColor="#00000000"
android:strokeColor="#FDB022"
android:strokeLineCap="round"/>
</vector>
17 changes: 16 additions & 1 deletion app/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/group_empty_state"
app:constraint_referenced_ids="iv_empty_state,tv_header,tv_instructions,tv_new_memo,iv_record,iv_draw,tv_paste"/>
app:constraint_referenced_ids="iv_empty_state,tv_header,tv_instructions,tv_new_memo,iv_record,iv_draw,tv_paste"
android:visibility="gone"/>

<View
android:layout_width="match_parent"
Expand Down Expand Up @@ -414,4 +415,18 @@
android:visibility="gone"
app:constraint_referenced_ids="tv_bottom_panel_paste,fab_new_action,divider_bottom_control"/>

<ProgressBar
android:id="@+id/pb_loading"
style="?android:attr/progressBarStyle"
android:layout_width="72dp"
android:layout_height="72dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:visibility="gone"
tools:visibility="visible"
android:indeterminate="true"
android:indeterminateDrawable="@drawable/ic_animated_loading"/>

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit e0ebd30

Please sign in to comment.