Skip to content

Commit

Permalink
Change activities to fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
epicadk committed Oct 30, 2020
1 parent ff42e9d commit 59d9951
Show file tree
Hide file tree
Showing 13 changed files with 174 additions and 116 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'com.android.application'
apply plugin: "androidx.navigation.safeargs"

android {
compileSdkVersion 30
Expand Down
7 changes: 0 additions & 7 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,10 @@
android:name=".view.activity.MainActivity"
android:windowSoftInputMode="adjustPan" />

<activity
android:name=".view.activity.RestaurantDetails"
android:theme="@style/AppTheme" />

<activity
android:name=".view.activity.MenuActivity"
android:theme="@style/AppTheme" />

<activity
android:name=".view.activity.CollectionDetails" />

</application>

</manifest>

This file was deleted.

14 changes: 14 additions & 0 deletions app/src/main/java/com/tip/lunchbox/view/activity/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.tip.lunchbox.view.activity;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.NavDestination;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;

Expand All @@ -24,6 +28,16 @@ protected void onCreate(Bundle savedInstanceState) {
//Setting navController
NavController navController = Navigation.findNavController(this,
R.id.navigation_host_fragment);
navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
@Override
public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
if (destination.getId() == R.id.restaurantDetails || destination.getId() == R.id.collectionDetails){
mainBinding.mainActivityBn.setVisibility(View.GONE);
} else {
mainBinding.mainActivityBn.setVisibility(View.VISIBLE);
}
}
});
NavigationUI.setupWithNavController(mainBinding.mainActivityBn, navController);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.tip.lunchbox.view.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.fragment.NavHostFragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.tip.lunchbox.databinding.FragmentCollectionDetailsBinding;
import com.tip.lunchbox.view.adapter.RestaurantAdapter;
import com.tip.lunchbox.view.listeners.RecyclerTouchListener;
import com.tip.lunchbox.viewmodel.CollectionDetailsViewModel;

import org.jetbrains.annotations.NotNull;

public class CollectionDetails extends Fragment {
private FragmentCollectionDetailsBinding binding;
private CollectionDetailsViewModel viewModel;
private RestaurantAdapter adapter;

@Override
public View onCreateView(@NotNull LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = FragmentCollectionDetailsBinding.inflate(inflater, container, false);
setupRecyclerView();

viewModel = new ViewModelProvider(this).get(CollectionDetailsViewModel.class);
assert getArguments() != null;
int collectionId = CollectionDetailsArgs.fromBundle(getArguments()).getCollectionID();
String collectionName = CollectionDetailsArgs.fromBundle(getArguments()).getCollectionName();
binding.appBarTvLocation.setText(collectionName);
viewModel.getSearchResponseLiveData(collectionId).observe(getViewLifecycleOwner(), searchResponse ->
adapter.setData(searchResponse.getRestaurantContainers()));
return binding.getRoot();
}

private void setupRecyclerView() {
adapter = new RestaurantAdapter(requireContext());
binding.rvCollectionsDetails.setAdapter(adapter);
binding.rvCollectionsDetails.setLayoutManager(new LinearLayoutManager(requireContext()));

new RecyclerTouchListener(requireContext(), binding.rvCollectionsDetails, (view, position) -> {
CollectionDetailsDirections.ActionCollectionDetailsToRestaurantDetails action =
CollectionDetailsDirections.actionCollectionDetailsToRestaurantDetails(
Integer.parseInt(adapter.getData().get(position)
.getRestaurant().getId()));

NavHostFragment.findNavController(this).navigate(action);
});
}
}
18 changes: 8 additions & 10 deletions app/src/main/java/com/tip/lunchbox/view/fragment/HomeFragment.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.tip.lunchbox.view.fragment;

import android.animation.Animator;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -28,8 +27,6 @@
import com.tip.lunchbox.model.CategoryContainer;
import com.tip.lunchbox.model.Restaurant;
import com.tip.lunchbox.model.RestaurantContainer;
import com.tip.lunchbox.utilities.Constants;
import com.tip.lunchbox.view.activity.RestaurantDetails;
import com.tip.lunchbox.view.listeners.CategoryChangeListener;
import com.tip.lunchbox.view.adapter.RestaurantAdapter;
import com.tip.lunchbox.view.listeners.RecyclerTouchListener;
Expand All @@ -39,7 +36,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
Expand Down Expand Up @@ -74,10 +70,12 @@ public View onCreateView(@NotNull LayoutInflater inflater,
adapter = new RestaurantAdapter(getActivity());

new RecyclerTouchListener(getActivity(), homeBinding.rvRestaurant, (view, position) -> {
Intent intent = new Intent(getContext(), RestaurantDetails.class);
intent.putExtra(Constants.INTENT_RES_ID, adapter.getData().get(position)
.getRestaurant().getId());
requireActivity().startActivity(intent);
HomeFragmentDirections.ActionHomeFragmentToRestaurantDetails action =
HomeFragmentDirections.actionHomeFragmentToRestaurantDetails(
Integer.parseInt(adapter.getData().get(position)
.getRestaurant().getId()));

NavHostFragment.findNavController(this).navigate(action);
});
onError();
BottomSheetBehavior.from(homeBinding.nsvRestaurantList);
Expand Down Expand Up @@ -181,8 +179,8 @@ public void onAnimationRepeat(Animator animation) {
showData();

homeBinding.appBarTvLocation.setText(geoCodeResponse.getLocality().getTitle()
+ ", "
+ geoCodeResponse.getLocality().getCityName());
+ ", "
+ geoCodeResponse.getLocality().getCityName());
setMapMarkers(geoCodeResponse.getNearbyRestaurantContainers());
} else {
showErrorView();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.tip.lunchbox.view.activity;
package com.tip.lunchbox.view.fragment;

import android.Manifest;
import android.content.Intent;
Expand All @@ -7,89 +7,97 @@
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.bumptech.glide.Glide;
import com.tip.lunchbox.R;
import com.tip.lunchbox.databinding.ActivityRestaurantDetailsBinding;
import com.tip.lunchbox.databinding.FragmentRestaurantDetailsBinding;
import com.tip.lunchbox.model.Restaurant;
import com.tip.lunchbox.utilities.Constants;
import com.tip.lunchbox.view.activity.MenuActivity;
import com.tip.lunchbox.view.adapter.HighlightsAdapter;
import com.tip.lunchbox.view.adapter.PhoneNumberAdapter;
import com.tip.lunchbox.view.listeners.RecyclerTouchListener;
import com.tip.lunchbox.viewmodel.RestaurantDetailsViewModel;

import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.List;

public class RestaurantDetails extends AppCompatActivity {
public class RestaurantDetails extends Fragment {

private final int callerPermissionCode = 29;
private RestaurantDetailsViewModel viewModel;
private ActivityRestaurantDetailsBinding binding;
private FragmentRestaurantDetailsBinding binding;
private PhoneNumberAdapter phoneNumberAdapter;
private HighlightsAdapter highlightsAdapter;
private static final String TAG = "Restaurant Details";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityRestaurantDetailsBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
public View onCreateView(@NotNull LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {

binding = FragmentRestaurantDetailsBinding.inflate(inflater, container, false);
viewModel = new ViewModelProvider(this).get(RestaurantDetailsViewModel.class);
String resId = getIntent().getStringExtra(Constants.INTENT_RES_ID);
assert resId != null;
int resId = RestaurantDetailsArgs.fromBundle(getArguments()).getResId();

// creating adapter instances
phoneNumberAdapter = new PhoneNumberAdapter(this);
highlightsAdapter = new HighlightsAdapter(this);
phoneNumberAdapter = new PhoneNumberAdapter(requireContext());
highlightsAdapter = new HighlightsAdapter(requireContext());
setupRecyclerViews();

binding.appBar.addOnOffsetChangedListener((appBarLayout, verticalOffset) -> {
if (Math.abs(verticalOffset) - appBarLayout.getTotalScrollRange() == 0) {
// Collapsed State
binding.fabMenu.show();
binding.toolbar.setBackgroundColor(getColor(R.color.white));
binding.toolbar.setTitleTextColor(getColor(R.color.colorPrimary));
binding.toolbar.setBackgroundColor(requireActivity().getColor(R.color.white));
binding.toolbar.setTitleTextColor(requireActivity()
.getColor(R.color.colorPrimary));

} else {
// Expanded State
binding.fabMenu.hide();
binding.toolbar.setBackgroundColor(getColor(R.color.colorTransparent));
binding.toolbar.setBackgroundColor(requireActivity()
.getColor(R.color.colorTransparent));
}
});
viewModel.getRestaurantLiveData(Integer.parseInt(resId)).observe(this, this::setData);
viewModel.getRestaurantLiveData(resId).observe(getViewLifecycleOwner(), this::setData);
return binding.getRoot();
}

/**
* This method is used to setup {@link androidx.recyclerview.widget.RecyclerView}
* used in this activity.
*/
private void setupRecyclerViews() {
binding.rvPhoneNumber.setLayoutManager(new LinearLayoutManager(this));
binding.rvPhoneNumber.setLayoutManager(new LinearLayoutManager(requireContext()));
binding.rvPhoneNumber.setAdapter(phoneNumberAdapter);

binding.rvHighlights.setLayoutManager(new LinearLayoutManager(
this,
requireContext(),
LinearLayoutManager.HORIZONTAL,
true));
binding.rvHighlights.setAdapter(highlightsAdapter);

// Item click listener for phone number's recyclerview
new RecyclerTouchListener(this, binding.rvPhoneNumber, (view, position) -> {
new RecyclerTouchListener(requireContext(), binding.rvPhoneNumber, (view, position) -> {
if (checkPermission()) {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:" + phoneNumberAdapter.getData().get(position)));
if (callIntent.resolveActivity(getPackageManager()) != null) {
if (callIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivity(callIntent);
} else {
Toast.makeText(
this,
requireContext(),
getString(R.string.dialer_app_not_found),
Toast.LENGTH_LONG).show();
}
Expand All @@ -101,13 +109,13 @@ private boolean checkPermission() {
String callPermission = Manifest.permission.CALL_PHONE;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(
this, callPermission) == PackageManager.PERMISSION_DENIED) {
requireContext(), callPermission) == PackageManager.PERMISSION_DENIED) {
boolean showRationale = shouldShowRequestPermissionRationale(callPermission);
if (showRationale) {
requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
callerPermissionCode);
} else {
Toast.makeText(this, R.string.permission_rationale_call,
Toast.makeText(requireContext(), R.string.permission_rationale_call,
Toast.LENGTH_SHORT).show();
}
} else {
Expand Down Expand Up @@ -137,7 +145,7 @@ private void setData(Restaurant restaurant) {
// onClickListener for opening up the MenuActivity
binding.fabMenu.setOnClickListener(
view -> {
Intent menuIntent = new Intent(this, MenuActivity.class);
Intent menuIntent = new Intent(requireContext(), MenuActivity.class);
menuIntent.putExtra(Constants.INTENT_RESTAURANT_NAME, restaurant.getName());
menuIntent.putExtra(Constants.INTENT_MENU_URL, restaurant.getMenuUrl());
startActivity(menuIntent);
Expand All @@ -163,15 +171,16 @@ private void getDirections(Restaurant restaurant) {
restaurant.getName()));
Intent mapIntent = new Intent(Intent.ACTION_VIEW);
mapIntent.setData(url);
if (mapIntent.resolveActivity(getPackageManager()) != null) {
if (mapIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivity(mapIntent);
} else {
Toast.makeText(this, getString(R.string.maps_not_found), Toast.LENGTH_LONG).show();
Toast.makeText(requireContext(), getString(R.string.maps_not_found), Toast.LENGTH_LONG).show();
}
}

/**
* This method is used to set a hardcoded review text based on the ratings fetched from APIs.
*
* @param aggregateRating restaurant's rating fetched from the APIs.
*/
private void setOurReviewText(float aggregateRating) {
Expand Down
Loading

0 comments on commit 59d9951

Please sign in to comment.