Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
alialbaali committed Jul 30, 2021
1 parent 7e67e73 commit 84cb465
Show file tree
Hide file tree
Showing 20 changed files with 69 additions and 139 deletions.
28 changes: 0 additions & 28 deletions .idea/deploymentTargetDropDown.xml

This file was deleted.

57 changes: 10 additions & 47 deletions app/src/main/java/com/chatychaty/app/chat/ChatItemDialogFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class ChatItemDialogFragment : BaseBottomSheetDialogFragment() {

@SuppressLint("SetTextI18n")
private fun collectState() {

viewModel.chat
.onEach { state ->
when (state) {
Expand Down Expand Up @@ -88,63 +87,38 @@ class ChatItemDialogFragment : BaseBottomSheetDialogFragment() {
}

private fun setupListeners() {
val parentFragmentFab = requireParentFragment()
.requireView()
.findViewById<FloatingActionButton>(R.id.fab)

val parentView = requireParentFragment().requireView()
val parentFab = parentView.findViewById<FloatingActionButton>(R.id.fab)

binding.tvArchiveChat.setOnClickListener {
dismiss()
if ((viewModel.chat.value as UiState.Success).value.isArchived) {
viewModel.unarchiveChat()
requireParentFragment()
.requireView()
.snackbar(getString(R.string.chat_is_unarchived))
parentView.snackbar(getString(R.string.chat_is_unarchived))
} else {
viewModel.archiveChat()

requireParentFragment()
.requireView()
.snackbar(
getString(R.string.chat_is_archived),
anchorView = parentFragmentFab,
)
parentView.snackbar(getString(R.string.chat_is_archived), anchorView = parentFab)
}
}

binding.tvChangeColor.setOnClickListener {
dismiss()
requireParentFragment()
.requireView()
.snackbar("TODO", anchorView = parentFragmentFab)
parentView.snackbar("TODO", anchorView = parentFab)
}

binding.tvDeleteChat.setOnClickListener {
dismiss()
requireParentFragment()
.requireView()
.snackbar("TODO", anchorView = parentFragmentFab)
parentView.snackbar("TODO", anchorView = parentFab)
}

binding.tvPin.setOnClickListener {
dismiss()
if ((viewModel.chat.value as UiState.Success).value.isPinned) {
viewModel.unpinChat()
requireParentFragment()
.requireView()
.snackbar(
getString(R.string.chat_is_unpinned),
anchorView = parentFragmentFab
)
parentView.snackbar(getString(R.string.chat_is_unpinned), anchorView = parentFab)
} else {
viewModel.pinChat()

requireParentFragment()
.requireView()
.snackbar(
getString(R.string.chat_is_pinned),
anchorView = parentFragmentFab,
)
parentView.snackbar(getString(R.string.chat_is_pinned), anchorView = parentFab)
}
}

Expand All @@ -157,21 +131,10 @@ class ChatItemDialogFragment : BaseBottomSheetDialogFragment() {
dismiss()
if ((viewModel.chat.value as UiState.Success).value.isMuted) {
viewModel.unmuteChat()
requireParentFragment()
.requireView()
.snackbar(
getString(R.string.chat_is_unmuted),
anchorView = parentFragmentFab
)
parentView.snackbar(getString(R.string.chat_is_unmuted), anchorView = parentFab)
} else {
viewModel.muteChat()

requireParentFragment()
.requireView()
.snackbar(
getString(R.string.chat_is_muted),
anchorView = parentFragmentFab,
)
parentView.snackbar(getString(R.string.chat_is_muted), anchorView = parentFab)
}
}

Expand Down
7 changes: 3 additions & 4 deletions app/src/main/java/com/chatychaty/app/chat/ChatListFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,9 @@ class ChatListFragment : Fragment() {
}

private fun setupListeners() {
//
// binding.ivImage.setOnClickListener {
// findNavController().navigate(ChatListFragmentDirections.actionChatListFragmentToUserFragment())
// }
binding.ivUser.setOnClickListener {
findNavController().navigate(ChatListFragmentDirections.actionChatListFragmentToUserFragment())
}
binding.fab.setOnClickListener {
findNavController().navigate(ChatListFragmentDirections.actionChatListFragmentToSearchFragment())
}
Expand Down
30 changes: 17 additions & 13 deletions app/src/main/java/com/chatychaty/app/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ import androidx.appcompat.app.AppCompatDelegate
import androidx.core.os.bundleOf
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.lifecycleScope
import androidx.navigation.NavOptions
import androidx.navigation.findNavController
import androidx.work.Constraints
import androidx.work.NetworkType
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import com.chatychaty.app.R
import com.chatychaty.app.databinding.ActivityMainBinding
import com.chatychaty.app.notification.createNotificationChannel
import com.chatychaty.app.util.SyncWorker
import com.chatychaty.app.util.UiState
import com.chatychaty.app.notification.createNotificationChannel
import com.chatychaty.domain.model.Theme
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
Expand Down Expand Up @@ -51,16 +52,17 @@ class MainActivity : AppCompatActivity() {
.getStringExtra(Intent.EXTRA_TEXT)
?.let {
findNavController(R.id.nav_host_fragment_container)
.navigate(R.id.shareFragment, bundleOf("body" to it))
.navigate(
R.id.shareFragment,
bundleOf("body" to it),
NavOptions.Builder()
.setPopUpTo(R.id.chatListFragment, true)
.build()
)
}
}
}

override fun onStart() {
super.onStart()
workManager.cancelAllWork()
}

@Suppress("DEPRECATION")
private fun setupSystemBarsVisibility() {
when (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) {
Expand Down Expand Up @@ -94,11 +96,12 @@ class MainActivity : AppCompatActivity() {
.onEach {
if (it is UiState.Success) {
findNavController(R.id.nav_host_fragment_container).also { nav ->
nav.popBackStack()
nav.popBackStack(R.id.signGraph, false)
nav.navigate(R.id.chatListFragment)
}
viewModel.refreshData()
viewModel.connectHub()
viewModel.syncData()
}
}
.launchIn(lifecycleScope)
Expand All @@ -116,14 +119,15 @@ class MainActivity : AppCompatActivity() {
.launchIn(lifecycleScope)
}

override fun onStart() {
super.onStart()
workManager.cancelAllWork()
}

override fun onStop() {
super.onStop()
viewModel.disconnectHub()
setupSyncWorker()
}

override fun onDestroy() {
super.onDestroy()
// viewModel.disconnectHub()
}

}
10 changes: 7 additions & 3 deletions app/src/main/java/com/chatychaty/app/main/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@ class MainViewModel(
fun connectHub() {
viewModelScope.launch {
messageRepository.connectHub()
launch { messageRepository.syncMessages() }
launch { chatRepository.syncChats() }
}
}

fun syncData() {
viewModelScope.launch {
messageRepository.syncMessages()
chatRepository.syncChats()
}
}

Expand All @@ -61,5 +66,4 @@ class MainViewModel(
chatRepository.refreshChats()
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class MessageItemDialogFragment : BaseBottomSheetDialogFragment() {
)
}
}
val chooser = Intent.createChooser(intent, getString(R.string.share_to))
val chooser = Intent.createChooser(intent, getString(R.string.share_with))
startActivity(chooser)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class MessageListDialogFragment : BaseBottomSheetDialogFragment() {
viewModel.chat
.onEach {
when (it) {

is UiState.Failure -> {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import androidx.recyclerview.widget.LinearLayoutManager
import com.chatychaty.app.R
import com.chatychaty.app.databinding.FragmentListMessageBinding
import com.chatychaty.app.notification.cancelNotification
import com.chatychaty.app.util.UiState
Expand Down Expand Up @@ -78,7 +79,6 @@ class MessageListFragment : Fragment() {
viewModel.messageBody.value = args.body.toString()
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY)
binding.et.requestFocus()
// findNavController().popBackStack(R.id.chatListFragment, true)
}
}

Expand Down Expand Up @@ -114,7 +114,6 @@ class MessageListFragment : Fragment() {
viewModel.chat
.onEach { state ->
when (state) {

is UiState.Failure -> {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,4 @@ class MessageListViewModel(
chatRepository.unmuteChat((mutableChat.value as UiState.Success).value.chatId)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fun NotificationManager.createNotification(context: Context, chat: Chat, message
val replyAction = context.createReplyAction(chat.chatId, messages.first().id)

val notification = NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_app)
.setSmallIcon(R.mipmap.ic_app_round)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setStyle(style)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class ProfileFragment : Fragment() {
viewModel.chat
.onEach { state ->
when (state) {

is UiState.Failure -> {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,4 @@ class ProfileViewModel(private val chatRepository: ChatRepository, private val c
.onEach { mutableChat.value = UiState.Success(it) }
.launchIn(viewModelScope)
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class SearchFragment : Fragment() {
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {

binding = FragmentSearchBinding.inflate(inflater, container, false).also {
it.lifecycleOwner = this
it.viewModel = viewModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,21 @@ class PasswordDialogFragment : BaseBottomSheetDialogFragment() {
}

private fun collectState() {
val parentView = requireParentFragment()
.requireView()

viewModel.state
.onEach { state ->
when (state) {
is UiState.Failure -> {
findNavController().navigateUp()
requireParentFragment().requireView().snackbar("Failed changing the password!")
parentView.snackbar("Failed changing the password! ${state.exception.message}")
}
is UiState.Loading -> {
}
is UiState.Success -> {
findNavController().navigateUp()
requireParentFragment().requireView().snackbar("Password has changed successfully!")
parentView.snackbar("Password has changed successfully!")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class ThemeDialogFragment : BaseBottomSheetDialogFragment() {
viewModel.theme
.onEach { state ->
when (state) {

is UiState.Failure -> {

}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/chatychaty/app/user/UserFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class UserFragment : Fragment() {
}

binding.tvDeleteAccount.setOnClickListener {
signOut()
binding.root.snackbar("TODO")
}
binding.fab.setOnClickListener {
when {
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/java/com/chatychaty/app/util/ExtensionUtils.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.chatychaty.app.util

import android.annotation.SuppressLint
import android.os.IBinder
import android.view.View
import android.view.inputmethod.InputMethodManager
import com.chatychaty.app.R
import com.chatychaty.domain.model.Chat
import com.chatychaty.domain.model.Message
Expand Down Expand Up @@ -91,4 +93,7 @@ fun List<Pair<Chat, Message>>.sortedByIsPinnedAndSentDate() =
sortedWith(
compareByDescending<Pair<Chat, Message>> { it.first.isPinned }
.thenByDescending { it.second.sentDate }
)
)

fun InputMethodManager.showKeyboard(view: View) = showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
fun InputMethodManager.hideKeyboard(windowToken: IBinder) = hideSoftInputFromWindow(windowToken, InputMethodManager.HIDE_IMPLICIT_ONLY)
Loading

0 comments on commit 84cb465

Please sign in to comment.