Add files via upload

This commit is contained in:
bearacuda13
2018-12-26 12:14:05 -06:00
committed by GitHub
parent eeeec86be6
commit 08b336cf15
35 changed files with 1172 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
package com.example.ian.titanscout
class Match (var ind: Int, var redTeams: Array<Team>, var blueTeams: Array<Team>) {
// class body
fun getScouts(): Int {
var x = 0
for (red in redTeams) {
if (red.hasScouts()) {
x++
}
}
for (blue in blueTeams) {
if (blue.hasScouts()) {
x++
}
}
return x
}
}
class Team (var num: String, var color:String, var scouts:Array<String>) {
fun hasScouts() : Boolean {
if (scouts.size > 0 && !scouts[0].equals("")) {
return true
}
return false
}
}

View File

@@ -0,0 +1,58 @@
package com.example.ian.titanscout
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ProgressBar
import android.widget.TextView
class MatchAdapter(private val context: Context,
private val dataSource: Array<Match>) : BaseAdapter() {
private val inflater: LayoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
//1
override fun getCount(): Int {
return dataSource.size
}
//2
override fun getItem(position: Int): Any {
return dataSource[position]
}
//3
override fun getItemId(position: Int): Long {
return position.toLong()
}
//4
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
// Get view for row item
val rowView = inflater.inflate(R.layout.list_item, parent, false)
// Get title element
val titleTextView = rowView.findViewById(R.id.titleTV) as TextView
// Get subtitle element
val subtitleTextView = rowView.findViewById(R.id.subtitle) as TextView
// Get progressBar element
val progressBar = rowView.findViewById(R.id.progressBar) as ProgressBar
val match = getItem(position) as Match
val str = "Match " + match.ind
titleTextView.text = str
val str2 = match.getScouts().toString() + " of 6"
subtitleTextView.text = str2
progressBar.progress = (1.0 * progressBar.max * match.getScouts() / (6.0)).toInt()
return rowView
}
}

View File

@@ -0,0 +1,188 @@
package com.example.ian.titanscout
import android.graphics.Color
import android.os.Bundle
import android.support.design.widget.FloatingActionButton
import android.support.v7.app.AlertDialog
import android.support.v7.app.AppCompatActivity;
import com.google.firebase.firestore.FirebaseFirestore
import org.json.JSONObject
import com.google.zxing.WriterException
import android.graphics.Bitmap
import android.widget.*
import net.glxn.qrgen.android.QRCode
class MatchesTableView : AppCompatActivity() {
var shouldShow = true
// Reference the database to be used in the rest of the class.
val db = FirebaseFirestore.getInstance()
var alias = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_matches_table_view)
// setSupportActionBar(toolbar)
var matches = arrayOf<Match>()
val TAG = "MainActivity"
val docRef = db.collection("appBuilding").document("schedule")
docRef.get().addOnSuccessListener { documentSnapshot ->
val stringData = documentSnapshot.data.toString()
// Get data from the database and process it into an array of Matches.
val schedule = Response(stringData).getJSONArray("matches")
for (i in 0..(schedule.length() - 1)) {
val item = schedule.getJSONObject(i)
val reds = Response(item["RED"].toString())
val blues = Response(item["BLUE"].toString())
val redTeams = getTeamArrayFromJSON(reds, "RED")
val blueTeams = getTeamArrayFromJSON(blues, "BLUE")
matches += Match(i+1, redTeams, blueTeams)
}
// update the user's alias
alias = intent.getStringExtra("alias")
updateAlias(alias)
val listView = findViewById<ListView>(R.id.match_list_view)
val listItems = arrayOfNulls<String>(matches.size)
for (i in 0 until matches.size) {
val match = matches[i]
listItems[i] = "Match " + match.ind
}
// val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, listItems)
// listView.adapter = adapter
val adapter = MatchAdapter(this, matches)
listView.adapter = adapter
}
val fab = findViewById<FloatingActionButton>(R.id.fab)
fab.setImageResource(R.drawable.qrcodeicon)
fab.setColorFilter(Color.parseColor("#FFFFFF"))
fab.setOnClickListener { view ->
// QR Button pressed
if (shouldShow) {
try {
val bitmap = TextToImageEncode(intent.getStringExtra("auth"))
findViewById<ImageView>(R.id.imageView).setImageBitmap(bitmap)
} catch (e: WriterException) {
e.printStackTrace()
}
} else {
findViewById<ImageView>(R.id.imageView).setImageResource(android.R.color.transparent)
}
shouldShow = !shouldShow
}
findViewById<Button>(R.id.changeAliasButton).setOnClickListener {
showCreateCategoryDialog()
}
}
fun TextToImageEncode(text: String): Bitmap {
// generate a QR code from the given text.
return QRCode.from(text).withSize(1000, 1000).bitmap()
}
// From https://code.luasoftware.com/tutorials/android/android-text-input-dialog-with-inflated-view-kotlin/
fun showCreateCategoryDialog() {
val context = this
val builder = AlertDialog.Builder(context)
builder.setTitle("Change Alias")
// https://stackoverflow.com/questions/10695103/creating-custom-alertdialog-what-is-the-root-view
// Seems ok to inflate view with null rootView
val view = layoutInflater.inflate(R.layout.dialog_new_category, null)
val categoryEditText = view.findViewById(R.id.categoryEditText) as EditText
builder.setView(view)
// set up the ok button
builder.setPositiveButton(android.R.string.ok) { dialog, p1 ->
val newCategory = categoryEditText.text
var isValid = true
if (newCategory.isBlank()) {
categoryEditText.error = "Some String"
isValid = false
}
if (isValid) {
// do something
updateAlias(categoryEditText.text.toString())
}
if (isValid) {
dialog.dismiss()
}
}
builder.setNegativeButton(android.R.string.cancel) { dialog, p1 ->
dialog.cancel()
}
builder.show();
}
fun updateAlias(withString: String) {
alias = withString
val str = "Hello! I'm " + withString
findViewById<TextView>(R.id.aliasTextView).text = str
}
// When given a JSON containing teams and scouts like this : {"team-4096":[],"team-101":["scoutName"],"team-4292":["ScoutName", "ScoutName32"]}},
// The function will convert it to a Team Array
fun getTeamArrayFromJSON(json: JSONObject, forColor:String) : Array<Team> {
var teams = arrayOf<Team>()
val keys = json.keys()
while (keys.hasNext()) {
// get the key
val key = keys.next()
// Manually parse the string into an array of strings.
var vs = json.get(key).toString()
var scouts = arrayOf<String>()
vs = vs.substring(1,vs.length-1)
if (!vs.equals("") && !vs.contains(",")) {
scouts += vs.substring(1,vs.length-1)
}
for (str in vs.split(",")) {
if (str.length>2) {
scouts += str.substring(1,str.length-1)
}
}
teams += Team(key.substring(5), forColor, scouts)
}
return teams
}
}

View File

@@ -0,0 +1,56 @@
package com.example.ian.titanscout
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import org.json.JSONObject
// Classes taken from https://stackoverflow.com/questions/41928803/how-to-parse-json-in-kotlin.
// Inputs a string and outputs a JSONObject. This was a quick alternative to importing another library.
class Response(json: String) : JSONObject(json) {
val type: String? = this.optString("type")
val data = this.optJSONArray("data")
?.let { 0.until(it.length()).map { i -> it.optJSONObject(i) } } // returns an array of JSONObject
?.map { Foo(it.toString()) } // transforms each JSONObject of the array into Foo
}
// Helper for the above class
class Foo(json: String) : JSONObject(json) {
val id = this.optInt("id")
val title: String? = this.optString("title")
}
class PrematchesActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_alias)
findViewById<Button>(R.id.updateProfileButton).setOnClickListener {
// Get alias from the edittext and clear the edittext
val alias = findViewById<EditText>(R.id.aliasField).text.toString()
findViewById<EditText>(R.id.aliasField).text.clear()
//
val intent2 = Intent(this, MatchesTableView::class.java)
intent2.putExtra("alias", alias)
intent2.putExtra("auth", intent.getStringExtra("auth"))
startActivity(intent2)
}
}
}

View File

@@ -0,0 +1,164 @@
package com.example.ian.titanscout;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.*;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import static android.widget.Toast.makeText;
public class qrActivity extends AppCompatActivity {
ImageView imageView;
Button button;
Button btnScan;
EditText editText;
String EditTextValue ;
Thread thread;
public final static int QRcodeWidth = 350;
Bitmap bitmap;
TextView tv_qr_readTxt;
private Object MainActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activityqr);
// // WARNING: TAKE OUT OF FINAL CODE, ONLY FOR TESTING
// this.move("auth-team-2022");
IntentIntegrator integrator = new IntentIntegrator(qrActivity.this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("Scan a teammate's QR!");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.initiateScan();
imageView = (ImageView)findViewById(R.id.imageView);
editText = (EditText)findViewById(R.id.editText);
button = (Button)findViewById(R.id.button);
btnScan = (Button)findViewById(R.id.btnScan);
tv_qr_readTxt = (TextView) findViewById(R.id.tv_qr_readTxt);
}
Bitmap TextToImageEncode(String Value) throws WriterException {
BitMatrix bitMatrix;
try {
bitMatrix = new MultiFormatWriter().encode(
Value,
BarcodeFormat.DATA_MATRIX.QR_CODE,
QRcodeWidth, QRcodeWidth, null
);
} catch (IllegalArgumentException Illegalargumentexception) {
return null;
}
int bitMatrixWidth = bitMatrix.getWidth();
int bitMatrixHeight = bitMatrix.getHeight();
int[] pixels = new int[bitMatrixWidth * bitMatrixHeight];
for (int y = 0; y < bitMatrixHeight; y++) {
int offset = y * bitMatrixWidth;
for (int x = 0; x < bitMatrixWidth; x++) {
pixels[offset + x] = bitMatrix.get(x, y) ?
getResources().getColor(R.color.QRCodeBlackColor):getResources().getColor(R.color.QRCodeWhiteColor);
}
}
Bitmap bitmap = Bitmap.createBitmap(bitMatrixWidth, bitMatrixHeight, Bitmap.Config.ARGB_4444);
bitmap.setPixels(pixels, 0, 350, 0, 0, bitMatrixWidth, bitMatrixHeight);
return bitmap;
}
void move(String withCode) {
Log.e("START", "START");
Intent intent = new Intent(this, PrematchesActivity.class);
intent.putExtra("auth", withCode);
startActivity(intent);
Log.e("END", "END");
}
@Override
protected void onActivityResult(int requestCode, final int resultCode, Intent data) {
final IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
if(result.getContents() == null) {
Log.e("Scan", "Cancelled scan");
} else {
Log.e("Scan", "Scanned");
tv_qr_readTxt.setText(result.getContents());
// Access a Cloud Firestore instance from your Activity
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("data")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
private static final String TAG = "Firestore";
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
if (document.getId().equals(result.getContents())) {
makeText(qrActivity.this, "Scan Successful!", Toast.LENGTH_LONG).show();
move(result.getContents());
} else {
makeText(qrActivity.this, "Team not registered. Please try again.", Toast.LENGTH_LONG).show();
}
Log.d(TAG, document.getId() + " => " + document.getData());
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
}
} else {
// This is important, otherwise the result will not be passed to the fragment
super.onActivityResult(requestCode, resultCode, data);
}
}
}