Skip to content

Instantly share code, notes, and snippets.

@vvksh
Created December 18, 2015 05:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vvksh/e329afba93dd6a20e222 to your computer and use it in GitHub Desktop.
Save vvksh/e329afba93dd6a20e222 to your computer and use it in GitHub Desktop.
An activity class for OnTrack android application(location sharing app). Describes the process when a user enters a session with a passcode to see the location of people who are in that session.
package edu.colby.ontrack.ontrack;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseRelation;
import java.util.List;
public class FollowActivity extends ActionBarActivity {
private EditText mUniqueID;
private ImageButton mFollow;
private ImageButton mCancelFollow;
private boolean misIDUnique;
private String muserID;
private static ParseObject mLeader;
private ParseObject mcurrentSession;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_follow);
mUniqueID = (EditText) findViewById(R.id.follower_uniqueID_edittext);
mFollow = (ImageButton) findViewById(R.id.button_follow);
muserID = LaunchActivity.getUser().getObjectId().toString();
mCancelFollow = (ImageButton) findViewById(R.id.button_cancel_follow);
mCancelFollow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
mFollow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//check if the user has input anything
String id = mUniqueID.getText().toString().trim();
// Log.d("before_checking", ""+ id.equals("")) ;
if(id.equals("")) {
// Log.d("after_checking", ""+ id.equals("")) ;
Toast.makeText(FollowActivity.this, "Please enter a valid ID", Toast.LENGTH_SHORT).show();
}else{
try {
if (doesIDexist(id)){
enterSession(id);
Intent i = new Intent(FollowActivity.this, FollowerMapsActivity.class);
startActivity(i);
} else {
Toast.makeText(FollowActivity.this, "ID does not exist, try again!", Toast.LENGTH_SHORT).show();
}
} catch (ParseException e) {
e.printStackTrace();
}
//check if ID is unique
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_follow, menu);
return true;
}
public boolean doesIDexist(String id) throws ParseException {
ParseQuery<ParseObject> query = ParseQuery.getQuery("Session");
query.whereEqualTo("Title", id);
List<ParseObject> results = query.find( );
if (results.isEmpty()){
return false;
} else if (!((boolean)results.get(0).get("Active"))){
return false;
} else {
mcurrentSession = results.get(0);
mLeader = (ParseObject) mcurrentSession.get("Leader");
return true;
}
}
public void enterSession(String id){
ParseObject currentUser = LaunchActivity.getUser();
ParseRelation relation = mcurrentSession.getRelation("Followers");
relation.add(currentUser);
mcurrentSession.saveInBackground();
currentUser.put("session", mcurrentSession);
currentUser.saveInBackground();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static ParseObject getmLeader() {
return mLeader;
}
}
@vvksh
Copy link
Author

vvksh commented Dec 18, 2015

During HackDartmouth, I worked in a team to develop an android app which lets users share location easily across different platforms. HOW THIS APP WORKS: There's a leader who creates a session and shares a passcode with others users who want to participate in that session and share their location and learn others' location.
MY RESPONSIBiLITY: I was assigned to handle the case when the users clicks "Follow" and enters a passcode. First, I check if the code is valid and exists in the database; if it does, the transitions to a new page; if not, he/she gets a message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment