moxy-community / Moxy

Moxy is MVP library for Android with incremental annotation processor and ktx features
MIT License
324 stars 33 forks source link

Add internal logging capability #113

Open alaershov opened 4 years ago

alaershov commented 4 years ago

For debugging purposes, it would be cool to log all "internal" operations of Moxy, such as presenter attachment and detachment, and ViewState commands execution. Maybe we should allow the user to implement some Logger interface and pass it somewhere to Moxy configuration.

antonio-pedro99 commented 1 year ago

Hey @alaershov, I would like to work on this, can you assign?

alaershov commented 1 year ago

@antonio-pedro99 sure, here you go!

antonio-pedro99 commented 1 year ago

@alaershov I do not think it is practical the user passing Logger interface in the Moxy configuration. There should be default behavior, just like what happens with fragments and activities. The thing is, can user's override these internals operations? If so, maybe the user can use their own logger. Something like this

class MyFragment : Fragment() {

    companion object {
        private const val TAG = "MyFragment"
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        Log.d(TAG, "onAttach")
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d(TAG, "onCreate")
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        Log.d(TAG, "onCreateView")
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_my, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        Log.d(TAG, "onViewCreated")
    }

    override fun onStart() {
        super.onStart()
        Log.d(TAG, "onStart")
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "onResume")
    }

    override fun onPause() {
        super.onPause()
        Log.d(TAG, "onPause")
    }

    override fun onStop() {
        super.onStop()
        Log.d(TAG, "onStop")
    }

    override fun onDestroyView() {
        super.onDestroyView()
        Log.d(TAG, "onDestroyView")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG, "onDestroy")
    }

    override fun onDetach() {
        super.onDetach()
        Log.d(TAG, "onDetach")
    }
}
alaershov commented 1 year ago

@antonio-pedro99 I think overriding the internals of the library is not always a good idea. Some of them are too critical to allow change, some are kapt-generated code. Do you have an example in mind of how would it look in Moxy? Like a sample of code, if this feature was already available.

Passing an event listener interface to the library is quite common practice, here is an example from OkHttp: https://square.github.io/okhttp/features/events/#eventlistener

I was thinking of a similar approach for this issue, but I'm open to suggestions. Also, before implementing the feature let's maybe discuss some requirements? A list of events to log would be useful for thinking how to log them :)