Skip to content

Commit

Permalink
Merge branch 'dev' into story_7_task_3
Browse files Browse the repository at this point in the history
  • Loading branch information
Ai-Albert committed Mar 17, 2023
2 parents a0c293f + b3f6333 commit ad52ad4
Show file tree
Hide file tree
Showing 20 changed files with 530 additions and 74 deletions.
Expand Up @@ -2,6 +2,7 @@

import androidx.annotation.VisibleForTesting;
import androidx.appcompat.app.AppCompatActivity;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet;
import androidx.lifecycle.LiveData;
Expand All @@ -24,6 +25,7 @@
import java.util.Map;

import database.Database;
import database.UserDao;
import database.UserRepository;
import model.User;
import utilities.Calculation;
Expand All @@ -38,10 +40,13 @@ public class CompassActivity extends AppCompatActivity {
private OrientationService orientationService;
private LocationService locationService;
SharedPreferences preferences;
private LocationViewModel viewModel;

private String main_public_uid;
private String main_private_uid;
private LiveData<List<User>> users;
private List<User> usersList;
private List<LiveData<User>> userDatas;
@VisibleForTesting
public Map<String, LocationView> locationViews;
private List<ImageView> circleViews;
Expand All @@ -51,7 +56,7 @@ public class CompassActivity extends AppCompatActivity {

@VisibleForTesting
public int radius; // Miles
public final int[] radii = {1, 10, 500, Integer.MAX_VALUE};
public final int[] radii = {0, 1, 10, 500, Integer.MAX_VALUE};
public int radiusIndex;

private final double COMPASS_EDGE = (getScreenWidth() - 32) / 2.0;
Expand All @@ -64,17 +69,12 @@ public static int getScreenWidth() {
return Resources.getSystem().getDisplayMetrics().widthPixels;
}

public static int getScreenHeight() {
return Resources.getSystem().getDisplayMetrics().heightPixels;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_compass);

// Setting up services
userRepo = new UserRepository(Database.getInstance(this).getUserDao());
orientationService = OrientationService.getInstance(this);
locationService = LocationService.getInstance(this);

Expand All @@ -83,42 +83,77 @@ protected void onCreate(Bundle savedInstanceState) {
main_public_uid = preferences.getString("Public", "");
main_private_uid = preferences.getString("Private", "");

// Checking when the list of users is being updated
LocationViewModel viewModel = setupViewModel();
users = viewModel.getUsers();
users.observe(this, this::updateFriendLocations);
locationViews = new HashMap<>();
// API mocking
UserDao dao = Database.getInstance(this).getUserDao();
String link = preferences.getString("API_Link", "");
if (link.equals("")) {
userRepo = new UserRepository(dao);
} else {
userRepo = new UserRepository(dao, link);
}

// Getting the current user's public uid
TextView public_uid_text = this.findViewById(R.id.public_uid);
public_uid_text.setText(String.format("%s%s", getString(R.string.publicUIDString), preferences.getString("Public", "")));

// Setting up location/orientation for user
compass = findViewById(R.id.compass);
compassLayout = findViewById(R.id.compassLayout);
circleViews = new ArrayList<>();
radius = 20;

// Checking when the list of users is being updated
viewModel = setupViewModel();
users = viewModel.getUsers();
users.observe(this, this::updateFriendLocations);
usersList = viewModel.getUsersNotLive();
userDatas = new ArrayList<>();
locationViews = new HashMap<>();

// Setting up compass
compass = findViewById(R.id.compass);
compassLayout = findViewById(R.id.compassLayout);
circleViews = new ArrayList<>();
radius = preferences.getInt("Radius", 10);
radiusIndex = preferences.getInt("Index", 1);
radiusIndex = preferences.getInt("Index", 2);
if (radiusIndex == 0) {
setNotClickable(findViewById(R.id.zoomInButton));
}
else if (radiusIndex == 2) {
else if (radiusIndex == 4) {
setNotClickable(findViewById(R.id.zoomOutButton));
}

// Setting up location/orientation for user
lastMainLat = locationService.getLocation().getValue() != null ? locationService.getLocation().getValue().first : 0;
lastMainLong = locationService.getLocation().getValue() != null ? locationService.getLocation().getValue().second : 0;

setupObservers();
updateCircles();
observeLocation();
observeOrientation();
}

@Override
protected void onResume() {
super.onResume();
setupObservers();
}

private LocationViewModel setupViewModel() {
return new ViewModelProvider(this).get(LocationViewModel.class);
}

private void setupObservers() {
for (User user : usersList) {
if (user.public_code.equals(main_public_uid)) {
continue;
}
LiveData<User> liveUser = userRepo.getSynced(user.public_code);
userDatas.add(liveUser);
liveUser.observe(this, this::updateCompassLocation);
}
}

@Override
protected void onDestroy() {
super.onDestroy();
Expand All @@ -143,36 +178,55 @@ private void updateFriendLocations(List<User> users) {
LocationView newLocation = addLocationView(cl, user);
locationViews.put(user.public_code, newLocation);
}
LocationView userView = locationViews.get(user.public_code);
updateCompassLocation(user, userView);
locationViews.get(user.public_code).update(user);
updateCompassLocation(user);
}
}

/**
* Displays a user's location marker on the compass.
*
* @param user The user whose location we are displaying
* @param userView The LocationView object containing the location marker for user
*/
public void updateCompassLocation(User user, LocationView userView) {
public void updateCompassLocation(User user) {
LocationView userView = locationViews.get(user.public_code);
assert userView != null;

// Getting angle
float azimuth = compass.getRotation() + Calculation.getAngle(lastMainLat, lastMainLong, user.latitude, user.longitude);

// Getting radius
float distance = Calculation.getDistance(lastMainLat, lastMainLong, user.latitude, user.longitude);
int compassRadius = (int) (distance / radius * COMPASS_EDGE);
int lowerIndex = 0;
for (int i = 1; i < 5; i++) {
if (distance >= radii[i]) {
lowerIndex = i;
}
}

int compassRadius = 0;
userView.nameView.setText(user.name);
if (compassRadius > COMPASS_EDGE) {
if (lowerIndex >= radiusIndex) {
compassRadius = (int) COMPASS_EDGE;
userView.nameView.setText("");
}
else {
int lowerRadius = lowerIndex == 0 ? 0 : ((ConstraintLayout.LayoutParams) circleViews.get(lowerIndex - 1).getLayoutParams()).width / 2;
int upperRadius = ((ConstraintLayout.LayoutParams) circleViews.get(lowerIndex).getLayoutParams()).width / 2;
compassRadius = (lowerRadius - upperRadius) / 2 + upperRadius;
}

// Checking for main user
if (user.public_code.equals(main_public_uid)) {
compassRadius = 0;
compassRadius = -30;
azimuth = 0;
}

// Setting new constraint based on angle and radius
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone((ConstraintLayout) findViewById(R.id.mainLayout));
constraintSet.constrainCircle(userView.itemView.getId(), compassLayout.getId(), compassRadius, azimuth);
constraintSet.applyTo(findViewById(R.id.mainLayout));

userView.itemView.bringToFront();
}

Expand All @@ -187,9 +241,9 @@ public void updateCircles() {
}
circleViews.clear();

int currIndex = 0;
int currIndex = 1;
while (currIndex <= radiusIndex) {
drawCircle(currIndex + 1, radiusIndex + 1);
drawCircle(currIndex, radiusIndex);
currIndex++;
}
}
Expand Down Expand Up @@ -245,7 +299,7 @@ public void observeLocation() {
lastMainLat = location.first;
lastMainLong = location.second;

User mainUser = userRepo.getLocal(main_public_uid);
User mainUser = viewModel.getUserNotLive(main_public_uid);
mainUser.setLatitude(lastMainLat);
mainUser.setLongitude(lastMainLong);

Expand All @@ -261,12 +315,12 @@ public void onAdd(View view) {
}

/**
* @require radius > 5
* @ensure radius = radius@pre - 5
* @ensure friend locations on compass are updated according to new radius
*/
public void onZoomIn(View view) {
radiusIndex--;
if (radiusIndex > 1) {
radiusIndex--;
}
radius = radii[radiusIndex];
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("Radius", radius);
Expand All @@ -277,17 +331,18 @@ public void onZoomIn(View view) {
updateFriendLocations(users.getValue());

setClickable(findViewById(R.id.zoomOutButton));
if (radiusIndex == 0) {
if (radiusIndex == 1) {
setNotClickable(findViewById(R.id.zoomInButton));
}
}

/**
* @ensure radius = radius@pre + 5
* @ensure friend locations on compass are updated according to new radius
*/
public void onZoomOut(View view) {
radiusIndex++;
if (radiusIndex < 4) {
radiusIndex++;
}
radius = radii[radiusIndex];
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("Radius", radius);
Expand All @@ -298,7 +353,7 @@ public void onZoomOut(View view) {
updateFriendLocations(users.getValue());

setClickable(findViewById(R.id.zoomInButton));
if (radiusIndex == 3) {
if (radiusIndex == 4) {
setNotClickable(findViewById(R.id.zoomOutButton));
}
}
Expand Down Expand Up @@ -329,6 +384,7 @@ public LocationView addLocationView(ConstraintLayout cl, User user) {
ConstraintSet set = new ConstraintSet();
set.clone(inflater);
set.connect(userView.statusView.getId(), ConstraintSet.TOP, userView.nameView.getId(), ConstraintSet.BOTTOM);
set.connect(userView.timeView.getId(), ConstraintSet.TOP, userView.statusView.getId(), ConstraintSet.BOTTOM);
set.applyTo(inflater);

if (user.public_code.equals(main_public_uid)) {
Expand Down
Expand Up @@ -25,13 +25,19 @@ protected void onCreate(Bundle savedInstanceState) {

dao = Database.getInstance(this).getUserDao();
// TODO: Remove this during demo
Database.getInstance(this).clearAllTables();
// Database.getInstance(this).clearAllTables();

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 200);
}
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
SharedPreferences preferences = getSharedPreferences("preferences", MODE_PRIVATE);

SharedPreferences.Editor editor = preferences.edit();
// TODO: Remove this during demo
// editor.clear();
// editor.apply();

if (!preferences.contains("Private")) {
Intent intent = new Intent(this, NewUserActivity.class);
startActivity(intent);
Expand Down
Expand Up @@ -4,6 +4,7 @@
import androidx.lifecycle.LiveData;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
Expand All @@ -17,6 +18,7 @@
public class NewFriendActivity extends AppCompatActivity {

private EditText public_code;
private EditText api_link;
private TextView last_added;
private UserRepository repo;

Expand All @@ -27,8 +29,15 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_new_friend);
public_code = findViewById(R.id.public_code);
UserDao dao = Database.getInstance(this).getUserDao();
repo = new UserRepository(dao);
SharedPreferences preferences = getSharedPreferences("preferences", MODE_PRIVATE);
String link = preferences.getString("API_Link", "");
if (link.equals("")) {
repo = new UserRepository(dao);
} else {
repo = new UserRepository(dao, link);
}
last_added = findViewById(R.id.last_added);
api_link = findViewById(R.id.api_link);
}

/**
Expand All @@ -49,7 +58,14 @@ public void onSubmit(View view) {
* Goes back to compass view without adding a friend
**/
public void onBack(View view) {
Intent intent = new Intent(this, CompassActivity.class);
startActivity(intent);
finish();
}

public void onSubmitNewLink(View view) {
SharedPreferences preferences = getSharedPreferences("preferences", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("API_Link", api_link.getText().toString());
editor.apply();
finish();
}
}

0 comments on commit ad52ad4

Please sign in to comment.