focus-android icon indicating copy to clipboard operation
focus-android copied to clipboard

Reduce complexity of SettingsFragment by not overloading responsibility

Open pocmo opened this issue 8 years ago • 8 comments

SettingsFragment used to be responsible for showing the main settings screen only.

Now it's also responsible for showing a bunch of sub-screens. Those screens share almost no functionality so having a lot of switch cases in newInstance, onCreateOptionsMenu and so on make this class super complex and I fear we end up with monster like in Fennec.

For the autocomplete screens I added separate fragments and this seemed to work nicely. The only thing I borrowed was a little bit of code to send a telemetry event in case a setting was changed.

I think it would be cleaner to move the code into separate fragments for separate screens. If there's more code they need to share then it might make sense to introduce a base class for them?

pocmo avatar Dec 07 '17 11:12 pocmo

Hi @pocmo

https://github.com/Jacquester/focus-android/commits/Jacquester (See commits from 21Dec and on) Is this basically what you mean ? I'm essentially moving the sub-views to their own fragments (Later removing unnecessary classes and code from SettingsFragment)

neonpegasu5 avatar Dec 21 '17 05:12 neonpegasu5

Hey @Jacquester – it looks like you're in the right direction. To summarize, what this issue is about, it's primarily intended to simplify SettingsFragment by assigning only one layout to one fragment, whereas SettingsFragment now inflates multiple (4?) separate layouts.

Note that PR #1992 is currently open for this issue, but it's not complete. If you keep working on this issue, you may conflict with that user and your changes may not get in. That being said, if that PR is inactive for long enough, we'll assume it won't get worked and close it, giving you space to work on this.

By the way, I'd recommend working in git branches – they allow you to separate different, unrelated bits of work so they can all land independently on the main line of code (master).

mcomella avatar Dec 21 '17 21:12 mcomella

Note that PR #1992 is currently open for this issue

Yeah I'll leave this to @michaelsam94 to figure out lol I only played with it to see if I understand the issue correctly and to figure out if this could possibly solve #1974

neonpegasu5 avatar Dec 21 '17 22:12 neonpegasu5

I don't really want to make a new issue since this is mostly based off of this issue:

Is there a possibility of merging ManualAddSearchEngineSettingsFragment and ManualAddSearchEnginePreference into one class ?

Reasons why I'm asking:

  • They both represent the same view and I can't really see a reason for ManualAddSearchEnginePreference existing.
  • Manual...Preference has access to it's TextInputEditText fields and yet it's methods validateEngineNameAndShowError() and validateSearchQueryAndShowError() requires a parameter leading to (in my opinion) some very dodgy code to access them. See validateSearchQueryAndShowError() and ManualAdd...Fragment line 111
  • I belive children of the Preference class does not persist user-entered information (see #1974), if it's still an issue it could easily be fixed by saving information using SavedInstanceState

neonpegasu5 avatar Dec 21 '17 23:12 neonpegasu5

@Jacquester I will try to figure your comments out and work on it and thanks for helping me in this issue I'm newbie in contribution on github opensource projects

michaelsam94 avatar Dec 23 '17 09:12 michaelsam94

Is there a possibility of merging ManualAddSearchEngineSettingsFragment and ManualAddSearchEnginePreference into one class ?

I haven't looked into this too heavily but my intuition says this isn't a good idea (feel free to do more research and prove me wrong! :) ).

They both represent the same view and I can't really see a reason for ManualAddSearchEnginePreference existing.

Fragments and preferences have more responsibilities than just being a view (managing lifecycle/back stack and saving/loading preferences respectively). Ideally, in OO code, each class has one responsibility (see the SOLID principles) so a red flag is thrown for me about merging these. Maybe Preference doesn't need to exist at all though...

Manual...Preference has access to it's TextInputEditText fields and yet it's methods validateEngineNameAndShowError() and validateSearchQueryAndShowError() requires a parameter leading to (in my opinion) some very dodgy code to access them. See validateSearchQueryAndShowError() and ManualAdd...Fragment line 111

What's dodgy about it? In general, it's easier to reason about code with parameters to functions than global/member state.

I belive children of the Preference class does not persist user-entered information (see #1974), if it's still an issue it could easily be fixed by saving information using SavedInstanceState

You likely can use SavedInstanceState even through the two layers of abstraction.

But again, I didn't spend much time looking into this – please let me know if you think I'm wrong about merging these!

mcomella avatar Jan 05 '18 15:01 mcomella

Hi @mcomella, I've been quite busy at work lately I don't think I'll be able to look into this but you are probably right. I just thought the code would be fairly more readable if SavedInstanceState was not used through two levels of abstraction.

All the best and apologies for the late reply

neonpegasu5 avatar Jan 18 '18 11:01 neonpegasu5

I just thought the code would be fairly more readable if SavedInstanceState was not used through two levels of abstraction.

Generally, yes – the less indirection there is, the more readable the code is at a low level. However, indirection can make the code easier to read at a high level through encapsulation. I think it's important to always look critically at layers of abstraction to understand why they're there and if they're worth the added complexity (e.g. I think it was good for you to question and I'm now wondering if we need Preference at all!). Hope it helps!

mcomella avatar Jan 18 '18 19:01 mcomella