Skip to content

Commit

Permalink
Added local data save, updated icons, tiny UI changes
Browse files Browse the repository at this point in the history
  • Loading branch information
akhil99 committed Apr 7, 2016
1 parent d2351a0 commit c5bd009
Show file tree
Hide file tree
Showing 21 changed files with 368 additions and 63 deletions.
8 changes: 6 additions & 2 deletions pit/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
<activity
android:name=".PitScout"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LocalDataActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme">
</activity>
</application>

</manifest>
Binary file added pit/src/main/ic_launcher-web.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
237 changes: 237 additions & 0 deletions pit/src/main/java/com/mvrt/pit/LocalDataActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
package com.mvrt.pit;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.firebase.client.Firebase;
import com.mvrt.mvrtlib.util.Constants;
import com.mvrt.mvrtlib.util.JSONUtils;
import com.mvrt.mvrtlib.util.MatchInfo;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;

public class LocalDataActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener, ItemSelectListener {

RecyclerView fileListRecycler;
LocalDataAdapter localDataAdapter;
SwipeRefreshLayout swipeRefreshLayout;

Firebase firebase;

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
initFirebase();

setContentView(R.layout.activity_localdata);

setTitle("Local Data");
Toolbar t = (Toolbar)findViewById(R.id.pitscout_toolbar);
setSupportActionBar(t);

fileListRecycler = (RecyclerView)findViewById(R.id.localdata_listrecycler);
initRecycler();
swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.localdata_swiperefresh);
swipeRefreshLayout.setOnRefreshListener(this);
}

private void initFirebase(){
Firebase.setAndroidContext(getApplication());
firebase = new Firebase("http://teamdata.firebaseio.com/pit");
}

private void initRecycler(){
fileListRecycler.setLayoutManager(new LinearLayoutManager(this));
String[] filenames = getFilesDir().list(new JSONFilter());
localDataAdapter = new LocalDataAdapter(filenames);
localDataAdapter.setSelectListener(this);
fileListRecycler.setAdapter(localDataAdapter);
}

public void syncData(){
String[] filenames = localDataAdapter.getFilenames();
for(String file:filenames){
readFile(file);
}
this.onRefresh();
}

private void readFile(String filename){
try {
FileInputStream fis = openFileInput(filename);

int size = fis.available();
byte[] buffer = new byte[size];
fis.read(buffer);
fis.close();

JSONObject scoutData = new JSONObject(new String(buffer));

uploadData(scoutData, filename);

}catch(FileNotFoundException e){
Toast.makeText(this, "File " + filename + " not found", Toast.LENGTH_SHORT).show();
Log.e("MVRT", "File " + filename + " not found");
}catch(IOException e){
Toast.makeText(this, "File Read IOException", Toast.LENGTH_SHORT).show();
Log.e("MVRT", "File Read IOException");
}catch(JSONException e){
Toast.makeText(this, "File Read JSONException", Toast.LENGTH_SHORT).show();
Log.e("MVRT", "File Read JSONException");
}
}

private void uploadData(JSONObject scoutData, String filename){
try{
firebase.push().setValue(JSONUtils.jsonToMap(scoutData));
deleteFile(filename);
Log.d("MVRT", "Deleted file " + filename);
}catch(JSONException e){
Toast.makeText(this, "Upload JSONException", Toast.LENGTH_SHORT).show();
Log.e("MVRT", "Upload JSONException");
}
}

@Override
public void onRefresh() {
String[] filenames = getFilesDir().list(new JSONFilter());
localDataAdapter.setFilenames(filenames);
swipeRefreshLayout.setRefreshing(false);
}

@Override
public void itemSelected(String filename) {
Toast.makeText(this, filename + " selected", Toast.LENGTH_SHORT);
}

@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_localdata, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.action_syncdata:
syncData();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

static class JSONFilter implements FilenameFilter{

@Override
public boolean accept(File dir, String filename) {
Log.d("MVRT", "validating filename " + filename);
return filename.matches("pitscout-\\d+.json");
}
}

static class LocalDataAdapter extends RecyclerView.Adapter<LocalDataAdapter.ViewHolder>{

String[] filenames;
ItemSelectListener selectListener;

public LocalDataAdapter(String[] files){
filenames = files;
}

public String[] getFilenames(){
return filenames;
}

public void setSelectListener(ItemSelectListener listener){
selectListener = listener;
}

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

private TextView filenameView;
private ItemSelectListener selectListener;

public ViewHolder(View itemView, ItemSelectListener selectListener) {
super(itemView);
this.selectListener = selectListener;
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Log.d("MVRT", "");
return false;
}
});
filenameView = (TextView)itemView.findViewById(R.id.filelistitem_filename);
}

public String getFilename(){
return filenameView.getText().toString();
}

public void setFilename(String fn){
filenameView.setText(fn);
}

@Override
public void onClick(View v) {
if(selectListener != null)selectListener.itemSelected(getFilename());
}

}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_filelist, parent, false);
ViewHolder vh = new ViewHolder(v, selectListener);
return vh;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.setFilename(filenames[position]);
}

public void clear(){
setFilenames(new String[0]);
}

public void setFilenames(String[] filenames){
this.filenames = filenames;
notifyDataSetChanged();
}

@Override
public int getItemCount() {
return filenames.length;
}

}

}

interface ItemSelectListener {
void itemSelected(String filename);
}
46 changes: 34 additions & 12 deletions pit/src/main/java/com/mvrt/pit/PitScout.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.mvrt.pit;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
Expand All @@ -15,10 +18,14 @@
import com.firebase.client.Firebase;

import com.mvrt.mvrtlib.util.JSONUtils;
import com.mvrt.mvrtlib.util.MatchInfo;
import com.mvrt.mvrtlib.util.Snacker;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.FileOutputStream;

public class PitScout extends AppCompatActivity implements View.OnClickListener {

EditText team;
Expand Down Expand Up @@ -59,6 +66,9 @@ public class PitScout extends AppCompatActivity implements View.OnClickListener
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Firebase.setAndroidContext(getApplication());

setContentView(R.layout.activity_pit_scout);
initUI();
}
Expand Down Expand Up @@ -139,8 +149,9 @@ public boolean onCreateOptionsMenu(Menu menu) {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
if (id == R.id.action_localdata) {
Intent i = new Intent(this, LocalDataActivity.class);
startActivity(i);
}

return super.onOptionsItemSelected(item);
Expand All @@ -159,13 +170,11 @@ public void onClick (View v) {
}

private void pushData(){
Firebase.setAndroidContext(getApplication());
Firebase firebase = new Firebase("http://teamdata.firebaseio.com/pit");
Log.d("MVRT", "Pushing Data");
firebase.child("test").setValue("Hello!");
int teamNo = Integer.parseInt(team.getText().toString());

JSONObject obj = new JSONObject();
try{
obj.put("Team", team.getText().toString());
obj.put("Team",teamNo);

obj.put("Low Bar", lowBar.isChecked());
obj.put("Rock Wall", rockWall.isChecked());
Expand All @@ -184,24 +193,24 @@ private void pushData(){
obj.put("High Goal", highGoal.isChecked());
obj.put("Low Goal", lowGoal.isChecked());

obj.put("Number of Motors", numMotors.getText().toString());
obj.put("Number of Wheels", numWheels.getText().toString());
obj.put("Number of Motors", Integer.parseInt(numMotors.getText().toString()));
obj.put("Number of Wheels", Integer.parseInt(numWheels.getText().toString()));
obj.put("Type of Wheels", typeWheels.getText().toString());

obj.put("Auton Reach", autonReach.isChecked());
obj.put("Auton Break", autonBreak.isChecked());
obj.put("Auton Low", autonLowShot.isChecked());
obj.put("Auton High", autonHighShot.isChecked());

obj.put("Driver Regionals", driverExp.getText().toString());
obj.put("Weight", weight.getText().toString());
obj.put("Driver Regionals", Integer.parseInt(driverExp.getText().toString()));
obj.put("Weight", Integer.parseInt(weight.getText().toString()));
obj.put("Strategy", strategy.getText().toString());

} catch(Exception e){
Log.e("MVRT", "JSON Error");
}
try {
firebase.push().setValue(JSONUtils.jsonToMap(obj));
writeToFile(obj, teamNo);

} catch (JSONException e) {
Log.e("MVRT", "JSON Error");
Expand All @@ -221,4 +230,17 @@ private void clearData() {
sallyport.setChecked(false);

}

public String writeToFile(JSONObject data, int team) throws JSONException {
String filename = "pitscout-" + team + ".json";
try {
FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE);
fos.write(data.toString().getBytes());
Snacker.snack("Written to file: " + filename, this, Snackbar.LENGTH_SHORT);
} catch (Exception e) {
e.printStackTrace();
}
return filename;
}

}
Binary file added pit/src/main/res/drawable-hdpi/ic_menu_upload.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pit/src/main/res/drawable-mdpi/ic_menu_upload.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pit/src/main/res/drawable-xhdpi/ic_menu_upload.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pit/src/main/res/drawable-xxhdpi/ic_menu_upload.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit c5bd009

Please sign in to comment.