Skip to content

Commit

Permalink
REFACTOR : BaseActivity, BaseFragment 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
minseonglove committed Dec 29, 2022
1 parent 9e31066 commit dd8cbad
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 233 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
package com.fakedevelopers.bidderbidder.ui

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.fakedevelopers.bidderbidder.R
import com.fakedevelopers.bidderbidder.databinding.ActivityLoginBinding
import com.fakedevelopers.bidderbidder.ui.base.BaseActivity
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class LoginActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
}
}
class LoginActivity : BaseActivity<ActivityLoginBinding>(ActivityLoginBinding::inflate)
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,22 @@ import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import androidx.annotation.IdRes
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.NavController
import androidx.navigation.NavDirections
import androidx.navigation.fragment.NavHostFragment
import com.fakedevelopers.bidderbidder.R
import com.fakedevelopers.bidderbidder.databinding.ActivityMainBinding
import com.fakedevelopers.bidderbidder.ui.base.BaseActivity
import com.jakewharton.threetenabp.AndroidThreeTen
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
class MainActivity : BaseActivity<ActivityMainBinding>(ActivityMainBinding::inflate) {

private lateinit var navController: NavController
private var _binding: ActivityMainBinding? = null
private val binding get() = _binding!!

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
_binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
AndroidThreeTen.init(this)
navController = (supportFragmentManager.findFragmentById(R.id.navigation_main) as NavHostFragment).navController

navController.addOnDestinationChangedListener { _, destination, _ ->
private val destinationChangeListener by lazy {
NavController.OnDestinationChangedListener { _, destination, _ ->
when (destination.id) {
R.id.productListFragment -> {
binding.bottomNavigationMain.run {
Expand All @@ -39,6 +31,13 @@ class MainActivity : AppCompatActivity() {
else -> binding.bottomNavigationMain.visibility = View.GONE
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
AndroidThreeTen.init(this)
navController = (supportFragmentManager.findFragmentById(R.id.navigation_main) as NavHostFragment).navController
navController.addOnDestinationChangedListener(destinationChangeListener)
binding.bottomNavigationMain.setOnItemSelectedListener { item ->
when (item.itemId) {
R.id.menu_product_registration -> navController.safeNavigate(R.id.productRegistrationFragment)
Expand All @@ -60,7 +59,7 @@ class MainActivity : AppCompatActivity() {

override fun onDestroy() {
super.onDestroy()
_binding = null
navController.removeOnDestinationChangedListener(destinationChangeListener)
}

private fun NavController.safeNavigate(direction: NavDirections) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package com.fakedevelopers.bidderbidder.ui.chat.channel

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
Expand All @@ -15,6 +11,7 @@ import androidx.navigation.navGraphViewModels
import com.fakedevelopers.bidderbidder.R
import com.fakedevelopers.bidderbidder.api.data.Constants.Companion.STREAM_KEY
import com.fakedevelopers.bidderbidder.databinding.FragmentChannelListBinding
import com.fakedevelopers.bidderbidder.ui.base.BaseFragment
import com.fakedevelopers.bidderbidder.ui.util.ApiErrorHandler
import dagger.hilt.android.AndroidEntryPoint
import io.getstream.chat.android.client.ChatClient
Expand All @@ -29,30 +26,17 @@ import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

@AndroidEntryPoint
class ChannelListFragment : Fragment() {
class ChannelListFragment : BaseFragment<FragmentChannelListBinding>(
R.layout.fragment_channel_list
) {

private var _binding: FragmentChannelListBinding? = null
private val binding get() = _binding!!
private val viewModel: CLViewModel by navGraphViewModels(R.id.nav_graph) {
defaultViewModelProviderFactory
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
_binding = DataBindingUtil.inflate(
inflater,
R.layout.fragment_channel_list,
container,
false
)
return binding.run {
vm = viewModel
lifecycleOwner = viewLifecycleOwner
root
}
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.vm = viewModel
initCollector()
initListener()
}
Expand Down Expand Up @@ -144,9 +128,4 @@ class ChannelListFragment : Fragment() {
}
}
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package com.fakedevelopers.bidderbidder.ui.chat.chatting

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.OnBackPressedCallback
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import com.fakedevelopers.bidderbidder.R
import com.fakedevelopers.bidderbidder.databinding.FragmentChattingBinding
import com.fakedevelopers.bidderbidder.ui.base.BaseFragment
import com.getstream.sdk.chat.viewmodel.MessageInputViewModel
import com.getstream.sdk.chat.viewmodel.messages.MessageListViewModel
import io.getstream.chat.android.ui.message.input.viewmodel.bindView
Expand All @@ -20,10 +17,10 @@ import io.getstream.chat.android.ui.message.list.header.viewmodel.bindView
import io.getstream.chat.android.ui.message.list.viewmodel.bindView
import io.getstream.chat.android.ui.message.list.viewmodel.factory.MessageListViewModelFactory

class ChattingFragment : Fragment() {
class ChattingFragment : BaseFragment<FragmentChattingBinding>(
R.layout.fragment_chatting
) {

private var _binding: FragmentChattingBinding? = null
private val binding get() = _binding!!
private val args: ChattingFragmentArgs by navArgs()
private val backPressedCallback by lazy {
object : OnBackPressedCallback(true) {
Expand All @@ -33,16 +30,6 @@ class ChattingFragment : Fragment() {
}
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
_binding = DataBindingUtil.inflate(
inflater,
R.layout.fragment_chatting,
container,
false
)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
runCatching {
Expand Down Expand Up @@ -94,6 +81,5 @@ class ChattingFragment : Fragment() {
override fun onDestroyView() {
super.onDestroyView()
backPressedCallback.remove()
_binding = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ package com.fakedevelopers.bidderbidder.ui.login

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
Expand All @@ -16,40 +12,20 @@ import com.fakedevelopers.bidderbidder.R
import com.fakedevelopers.bidderbidder.api.data.Constants.Companion.LOGIN_SUCCESS
import com.fakedevelopers.bidderbidder.databinding.FragmentLoginBinding
import com.fakedevelopers.bidderbidder.ui.MainActivity
import com.orhanobut.logger.Logger
import com.fakedevelopers.bidderbidder.ui.base.BaseFragment
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
class LoginFragment : Fragment() {
class LoginFragment : BaseFragment<FragmentLoginBinding>(
R.layout.fragment_login
) {

private var _binding: FragmentLoginBinding? = null

private val binding get() = _binding!!
private val viewModel: LoginViewModel by viewModels()

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = DataBindingUtil.inflate(
inflater,
R.layout.fragment_login,
container,
false
)
return binding.run {
vm = viewModel
lifecycleOwner = viewLifecycleOwner
root
}
}

// 버튼 클릭 시 로그인 확인 API 호출
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

binding.vm = viewModel
initListener()
initCollector()
}
Expand All @@ -74,15 +50,10 @@ class LoginFragment : Fragment() {
startActivity(Intent(requireContext(), MainActivity::class.java))
requireActivity().finish()
} else {
Logger.e(it.errorBody().toString())
sendSnackBar(it.errorBody().toString())
}
}
}
}
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@ package com.fakedevelopers.bidderbidder.ui.loginType
import android.app.Activity.RESULT_OK
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
Expand All @@ -20,19 +15,22 @@ import com.fakedevelopers.bidderbidder.R
import com.fakedevelopers.bidderbidder.api.data.Constants.Companion.WEB_CLIENT_ID
import com.fakedevelopers.bidderbidder.databinding.FragmentLoginTypeBinding
import com.fakedevelopers.bidderbidder.ui.MainActivity
import com.fakedevelopers.bidderbidder.ui.base.BaseFragment
import com.google.android.gms.auth.api.Auth
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
class LoginTypeFragment : Fragment() {
class LoginTypeFragment : BaseFragment<FragmentLoginTypeBinding>(
R.layout.fragment_login_type
) {
private lateinit var googleSignInClient: GoogleSignInClient
private var _binding: FragmentLoginTypeBinding? = null

private val viewModel: LoginTypeViewModel by viewModels()
private val binding get() = _binding!!

private val requestActivity = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if (it.resultCode == RESULT_OK) {
val result = it.data?.let { data -> Auth.GoogleSignInApi.getSignInResultFromIntent(data) }
Expand All @@ -45,20 +43,6 @@ class LoginTypeFragment : Fragment() {
}
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = DataBindingUtil.inflate(
inflater,
R.layout.fragment_login_type,
container,
false
)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
(requireContext().applicationContext as HiltApplication).let {
Expand Down Expand Up @@ -96,22 +80,17 @@ class LoginTypeFragment : Fragment() {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.signinGoogleResponse.collect {
if (it.isSuccessful) {
Toast.makeText(requireActivity(), "success", Toast.LENGTH_LONG).show()
sendSnackBar("success")
startActivity(Intent(requireContext(), MainActivity::class.java))
requireActivity().finish()
} else {
Toast.makeText(requireActivity(), "failure", Toast.LENGTH_LONG).show()
sendSnackBar(it.errorBody().toString())
}
}
}
}
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

private fun googleLogin() {
val googleSignInOptions = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(WEB_CLIENT_ID)
Expand Down
Loading

0 comments on commit dd8cbad

Please sign in to comment.