-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Refactor nearby classes mvp #2969
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6483d21
3b63d9c
24a85a1
c7533eb
b9d9548
7dec4af
48a550f
24aa7a2
a1e3b2e
3da8e30
11d1d00
6b6a697
bfc0d88
2aa5546
f8926aa
8070802
1c27c3c
a8190bb
f8e8d13
034a2e2
bcd7e64
4bb83f7
95dd97a
297eb67
cdd6229
fd515de
a77c3c4
d055641
f4321d0
771a9ec
7080c3a
b108049
786212f
a96bbb1
d4cee8b
4ec1346
4253965
5b69daa
97ab63e
ceab7f4
f57d2cd
69597b3
9a50f54
e8ae344
4ff63a2
3f847fd
1b0d614
05fba7d
a7d35ed
008e1f6
c2453cb
5a8c137
ab7f352
026a944
c43b494
627cdf7
2e1cb07
c84ecd1
c8b1ebb
edfa39a
8667917
22290af
98be517
3bb801b
b8b524f
e808b0a
f6c1d27
2e7365e
e0464fa
0ec470b
f3694e3
3890ea5
52e9ef9
b511112
1a9c20c
2c98db8
6d9c74f
e1806c2
c0075ca
341d6b7
8c4b8c0
0b51c93
54e3be9
6ab2f03
e6f3633
ea2d0cf
14f08e2
6d18ca0
7356044
dbd1ad2
5575d42
67af6d8
ed796b4
ef0472b
eeb03bf
65ca06c
356c767
8324e5d
be42568
53ee714
aa7fb84
d858813
a396821
b736087
c7261ca
7a06073
32db7b3
22662ac
bf73948
3dc0844
2094158
6011cc7
43b4ac1
13695fd
a3c6734
be1ca1a
8c9c37a
c1403c1
9007ae5
7feed0a
0725af2
62eba39
7659aec
9d1412b
1677a4b
230d62d
c326cce
dff9ded
6d41622
9f8becc
4304e3d
c5d4d55
f345174
148c891
cff4086
45d6f56
ee57f84
cc0cb53
e9ebd23
1b3ce38
6425d3c
1351541
275181c
8271504
222b438
3b26c00
23c61bc
838b492
d0310b3
2d64f4b
e435694
7e40d94
25a88f3
1a816fd
dbd6693
5006067
d4777e5
b580600
fa3a14e
4056811
f903f55
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,8 +31,9 @@ | |
import fr.free.nrw.commons.auth.AuthenticatedActivity; | ||
import fr.free.nrw.commons.auth.SessionManager; | ||
import fr.free.nrw.commons.location.LocationServiceManager; | ||
import fr.free.nrw.commons.nearby.NearbyFragment; | ||
import fr.free.nrw.commons.nearby.NearbyNotificationCardView; | ||
import fr.free.nrw.commons.nearby.fragments.NearbyParentFragment; | ||
import fr.free.nrw.commons.nearby.presenter.NearbyParentFragmentPresenter; | ||
import fr.free.nrw.commons.notification.Notification; | ||
import fr.free.nrw.commons.notification.NotificationActivity; | ||
import fr.free.nrw.commons.notification.NotificationController; | ||
|
@@ -47,13 +48,15 @@ | |
|
||
public class MainActivity extends AuthenticatedActivity implements FragmentManager.OnBackStackChangedListener { | ||
|
||
@Inject | ||
SessionManager sessionManager; | ||
@Inject ContributionController controller; | ||
@BindView(R.id.tab_layout) | ||
TabLayout tabLayout; | ||
@BindView(R.id.pager) | ||
public UnswipableViewPager viewPager; | ||
|
||
@Inject | ||
SessionManager sessionManager; | ||
@Inject | ||
ContributionController controller; | ||
@Inject | ||
public LocationServiceManager locationManager; | ||
@Inject | ||
|
@@ -72,29 +75,16 @@ public class MainActivity extends AuthenticatedActivity implements FragmentManag | |
public boolean isContributionsFragmentVisible = true; // False means nearby fragment is visible | ||
private Menu menu; | ||
|
||
private boolean onOrientationChanged = false; | ||
|
||
private MenuItem notificationsMenuItem; | ||
private TextView notificationCount; | ||
|
||
public void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_contributions); | ||
ButterKnife.bind(this); | ||
|
||
requestAuthToken(); | ||
initDrawer(); | ||
setTitle(getString(R.string.navigation_item_home)); // Should I create a new string variable with another name instead? | ||
|
||
|
||
if (savedInstanceState != null ) { | ||
onOrientationChanged = true; // Will be used in nearby fragment to determine significant update of map | ||
|
||
//If nearby map was visible, call on Tab Selected to call all nearby operations | ||
/*if (savedInstanceState.getInt("viewPagerCurrentItem") == 1) { | ||
((NearbyFragment)contributionsActivityPagerAdapter.getItem(1)).onTabSelected(onOrientationChanged); | ||
}*/ | ||
} | ||
} | ||
|
||
@Override | ||
|
@@ -161,9 +151,7 @@ public void setNumOfUploads(int uploadCount) { | |
* tab won't change and vice versa. So we have to notify each of them. | ||
*/ | ||
private void setTabAndViewPagerSynchronisation() { | ||
//viewPager.canScrollHorizontally(false); | ||
viewPager.setFocusableInTouchMode(true); | ||
|
||
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { | ||
@Override | ||
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { | ||
|
@@ -178,15 +166,14 @@ public void onPageSelected(int position) { | |
tabLayout.getTabAt(CONTRIBUTIONS_TAB_POSITION).select(); | ||
isContributionsFragmentVisible = true; | ||
updateMenuItem(); | ||
|
||
break; | ||
case NEARBY_TAB_POSITION: | ||
Timber.d("Nearby tab selected"); | ||
tabLayout.getTabAt(NEARBY_TAB_POSITION).select(); | ||
isContributionsFragmentVisible = false; | ||
updateMenuItem(); | ||
// Do all permission and GPS related tasks on tab selected, not on create | ||
((NearbyFragment)contributionsActivityPagerAdapter.getItem(1)).onTabSelected(onOrientationChanged); | ||
NearbyParentFragmentPresenter.getInstance().onTabSelected(); | ||
break; | ||
default: | ||
tabLayout.getTabAt(CONTRIBUTIONS_TAB_POSITION).select(); | ||
|
@@ -267,15 +254,7 @@ public void onBackPressed() { | |
} | ||
} else if (getSupportFragmentManager().findFragmentByTag(nearbyFragmentTag) != null && !isContributionsFragmentVisible) { | ||
// Means that nearby fragment is visible (not contributions fragment) | ||
NearbyFragment nearbyFragment = (NearbyFragment) contributionsActivityPagerAdapter.getItem(1); | ||
|
||
if(nearbyFragment.isBottomSheetExpanded()) { | ||
// Back should first hide the bottom sheet if it is expanded | ||
nearbyFragment.listOptionMenuItemClicked(); | ||
} else { | ||
// Otherwise go back to contributions fragment | ||
viewPager.setCurrentItem(0); | ||
} | ||
NearbyParentFragmentPresenter.getInstance().backButtonClicked(); | ||
} else { | ||
super.onBackPressed(); | ||
} | ||
|
@@ -332,12 +311,12 @@ private void updateMenuItem() { | |
// Display notifications menu item | ||
menu.findItem(R.id.notifications).setVisible(true); | ||
menu.findItem(R.id.list_sheet).setVisible(false); | ||
Timber.d("Contributions activity notifications menu item is visible"); | ||
Timber.d("Contributions fragment notifications menu item is visible"); | ||
} else { | ||
// Display bottom list menu item | ||
menu.findItem(R.id.notifications).setVisible(false); | ||
menu.findItem(R.id.list_sheet).setVisible(true); | ||
Timber.d("Contributions activity list sheet menu item is visible"); | ||
Timber.d("Nearby fragment list sheet menu item is visible"); | ||
} | ||
} | ||
} | ||
|
@@ -351,7 +330,7 @@ public boolean onOptionsItemSelected(MenuItem item) { | |
return true; | ||
case R.id.list_sheet: | ||
if (contributionsActivityPagerAdapter.getItem(1) != null) { | ||
((NearbyFragment)contributionsActivityPagerAdapter.getItem(1)).listOptionMenuItemClicked(); | ||
((NearbyParentFragment)contributionsActivityPagerAdapter.getItem(1)).listOptionMenuItemClicked(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As explained above, please use callback here as well. |
||
} | ||
return true; | ||
default: | ||
|
@@ -361,8 +340,6 @@ public boolean onOptionsItemSelected(MenuItem item) { | |
|
||
public class ContributionsActivityPagerAdapter extends FragmentPagerAdapter { | ||
FragmentManager fragmentManager; | ||
private boolean isContributionsListFragment = true; // to know what to put in first tab, Contributions of Media Details | ||
|
||
|
||
public ContributionsActivityPagerAdapter(FragmentManager fragmentManager) { | ||
super(fragmentManager); | ||
|
@@ -392,12 +369,12 @@ public Fragment getItem(int position) { | |
} | ||
|
||
case 1: | ||
NearbyFragment retainedNearbyFragment = getNearbyFragment(1); | ||
NearbyParentFragment retainedNearbyFragment = getNearbyFragment(1); | ||
if (retainedNearbyFragment != null) { | ||
return retainedNearbyFragment; | ||
} else { | ||
// If we reach here, retainedNearbyFragment is null | ||
return new NearbyFragment(); | ||
return new NearbyParentFragment(); | ||
} | ||
default: | ||
return null; | ||
|
@@ -419,9 +396,9 @@ private ContributionsFragment getContributionsFragment(int position) { | |
* @param position index of tabs, in our case 0 or 1 | ||
* @return | ||
*/ | ||
private NearbyFragment getNearbyFragment(int position) { | ||
private NearbyParentFragment getNearbyFragment(int position) { | ||
String tag = makeFragmentName(R.id.pager, position); | ||
return (NearbyFragment)fragmentManager.findFragmentByTag(tag); | ||
return (NearbyParentFragment)fragmentManager.findFragmentByTag(tag); | ||
} | ||
|
||
/** | ||
|
@@ -444,7 +421,6 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
controller.handleActivityResult(this, requestCode, resultCode, data); | ||
} | ||
|
||
@Override | ||
protected void onResume() { | ||
super.onResume(); | ||
setNotificationCount(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
package fr.free.nrw.commons.mwapi; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Date; | ||
|
||
|
@@ -36,7 +35,6 @@ public class UploadResult { | |
this.imageUrl = imageUrl; | ||
} | ||
|
||
@NotNull | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you briefly explain why this annotation was removed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The annotation import causes build error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you share the build error that was caused by this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The import is incorrect. You can use
|
||
@Override | ||
public String toString() { | ||
return "UploadResult{" + | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,8 +28,10 @@ | |
public class NearbyController { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that we have a presenter for Nearby, we should remove this class and shift the functionalities to the presenter There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nearby controller controls our map logic (ie. how wide area is queried) while NearbyPresenter controls fragments and views. So I think we should keep them separated, but renaming NearbyController can be a better approach to eliminate confusion. What do you say @ashishkumar468 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @neslihanturan any logic, be it controlling the map logic should go in the corresponding view presenter, we should not ideally have classes other than the presenter to have logics. If we talk in terms of unit testing, it would just be the presenter which would need to be tested. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, but I can say that these two classes has really different responsibilities. Merging them not only makes the presenter class too long but also causes a semantic confusion. NearbyController decides which places will be displayed and what their included information is while presenter decides how to display them. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The presenter is solely responsible for deciding how to display a view. If the view is complex then the presenter will become longer but the controller doesn't fit into the MVP semantics. If you are inclined towards having smaller presenters, you can try to break down the views into smaller logical views and have presenters for each of them. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please read what does NearbyController method do, it does not intended to control views, it does not decide how to display views either. It controls our search operation. It controls how we fetch data from Wikidata. Ie. storing our previously searched radius, our max number of results and accordingly giving us list of data in form of lets say Place type. It is not the same thing which is described in MVP documentation IMO. What do you say? |
||
private static final int MAX_RESULTS = 1000; | ||
private final NearbyPlaces nearbyPlaces; | ||
public static double searchedRadius = 10.0; //in kilometers | ||
public static LatLng currentLocation; | ||
public static double currentLocationSearchRadius = 10.0; //in kilometers | ||
public static LatLng currentLocation; // Users latest fetched location | ||
public static LatLng latestSearchLocation; // Can be current and camera target on search this area button is used | ||
public static double latestSearchRadius = 10.0; // Any last search radius except closest result search | ||
|
||
@Inject | ||
public NearbyController(NearbyPlaces nearbyPlaces) { | ||
|
@@ -41,21 +43,21 @@ public NearbyController(NearbyPlaces nearbyPlaces) { | |
* Prepares Place list to make their distance information update later. | ||
* | ||
* @param curLatLng current location for user | ||
* @param latLangToSearchAround the location user wants to search around | ||
* @param searchLatLng the location user wants to search around | ||
* @param returnClosestResult if this search is done to find closest result or all results | ||
* @return NearbyPlacesInfo a variable holds Place list without distance information | ||
* and boundary coordinates of current Place List | ||
*/ | ||
public NearbyPlacesInfo loadAttractionsFromLocation(LatLng curLatLng, LatLng latLangToSearchAround, boolean returnClosestResult, boolean checkingAroundCurrentLocation) throws IOException { | ||
public NearbyPlacesInfo loadAttractionsFromLocation(LatLng curLatLng, LatLng searchLatLng, boolean returnClosestResult, boolean checkingAroundCurrentLocation) throws IOException { | ||
|
||
Timber.d("Loading attractions near %s", latLangToSearchAround); | ||
Timber.d("Loading attractions near %s", searchLatLng); | ||
NearbyPlacesInfo nearbyPlacesInfo = new NearbyPlacesInfo(); | ||
|
||
if (latLangToSearchAround == null) { | ||
if (searchLatLng == null) { | ||
Timber.d("Loading attractions nearby, but curLatLng is null"); | ||
return null; | ||
} | ||
List<Place> places = nearbyPlaces.radiusExpander(latLangToSearchAround, Locale.getDefault().getLanguage(), returnClosestResult); | ||
List<Place> places = nearbyPlaces.radiusExpander(searchLatLng, Locale.getDefault().getLanguage(), returnClosestResult); | ||
|
||
if (null != places && places.size() > 0) { | ||
LatLng[] boundaryCoordinates = {places.get(0).location, // south | ||
|
@@ -91,13 +93,25 @@ public NearbyPlacesInfo loadAttractionsFromLocation(LatLng curLatLng, LatLng lat | |
} | ||
); | ||
} | ||
nearbyPlacesInfo.curLatLng = curLatLng; | ||
nearbyPlacesInfo.searchLatLng = searchLatLng; | ||
nearbyPlacesInfo.placeList = places; | ||
nearbyPlacesInfo.boundaryCoordinates = boundaryCoordinates; | ||
if (!returnClosestResult && checkingAroundCurrentLocation) { | ||
// Do not update searched radius, if controller is used for nearby card notification | ||
searchedRadius = nearbyPlaces.radius; | ||
currentLocation = curLatLng; | ||
|
||
// Returning closes result means we use the controller for nearby card. So no need to set search this area flags | ||
if (!returnClosestResult) { | ||
// To remember latest search either around user or any point on map | ||
latestSearchLocation = searchLatLng; | ||
latestSearchRadius = nearbyPlaces.radius*1000; // to meter | ||
|
||
// Our radius searched around us, will be used to understand when user search their own location, we will follow them | ||
if (checkingAroundCurrentLocation) { | ||
currentLocationSearchRadius = nearbyPlaces.radius*1000; // to meter | ||
currentLocation = curLatLng; | ||
} | ||
} | ||
|
||
|
||
return nearbyPlacesInfo; | ||
} | ||
else { | ||
|
@@ -112,7 +126,7 @@ public NearbyPlacesInfo loadAttractionsFromLocation(LatLng curLatLng, LatLng lat | |
* @param placeList list of nearby places in Place data type | ||
* @return Place list that holds nearby places | ||
*/ | ||
static List<Place> loadAttractionsFromLocationToPlaces( | ||
public static List<Place> loadAttractionsFromLocationToPlaces( | ||
LatLng curLatLng, | ||
List<Place> placeList) { | ||
placeList = placeList.subList(0, Math.min(placeList.size(), MAX_RESULTS)); | ||
|
@@ -212,5 +226,7 @@ public static List<NearbyBaseMarker> loadAttractionsFromLocationToBaseMarkerOpti | |
public class NearbyPlacesInfo { | ||
public List<Place> placeList; // List of nearby places | ||
public LatLng[] boundaryCoordinates; // Corners of nearby area | ||
public LatLng curLatLng; // Current location when this places are populated | ||
public LatLng searchLatLng; // Search location for finding this places | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall we use callbacks for this thing, that way code would be cleaner and by defining contracts even novice contributors would know the intended use case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I agree with @ashishkumar468. Let us use callbacks.