Skip to content
This repository has been archived by the owner on Oct 20, 2023. It is now read-only.

Commit

Permalink
Fix various bugs for 2020.1.0-beta (#40)
Browse files Browse the repository at this point in the history
* loc don't look at this

* loc don't look at this

* loc don't look at this

* loc don't look at this

* loc don't look at this
  • Loading branch information
yuliu2016 authored Feb 5, 2020
1 parent c6bb119 commit 1f3dde0
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 57 deletions.
6 changes: 3 additions & 3 deletions app/src/main/assets/Boardfile.json
Original file line number Diff line number Diff line change
Expand Up @@ -390,17 +390,17 @@
{
"name": "A1_lifted",
"type": "Switch",
"options": []
"options": ["lite"]
},
{
"name": "A2_lifted",
"type": "Switch",
"options": []
"options": ["lite"]
},
{
"name": "A3_lifted",
"type": "Switch",
"options": []
"options": ["lite"]
}
],
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import ca.warp7.android.scouting.tba.EventSimple
import ca.warp7.android.scouting.tba.createCachedTBAInstance
import ca.warp7.android.scouting.tba.getEventMatchesSimple
import ca.warp7.android.scouting.tba.getTeamEventsByYearSimple
import ca.warp7.android.scouting.ui.EventListAdapter
Expand All @@ -33,7 +34,7 @@ class EventSelectionActivity : AppCompatActivity() {
val teamSearch = findViewById<EditText>(R.id.team_search)
val yearEdit = findViewById<EditText>(R.id.year)
teamSearch.setOnEditorActionListener { textView, _, _ ->
onSearch(textView, yearEdit)
onSearch(textView, yearEdit, cacheFirst = false)
true
}

Expand All @@ -56,7 +57,7 @@ class EventSelectionActivity : AppCompatActivity() {

if (teamNumber.isNotEmpty()) {
// show previous results
onSearch(teamSearch, yearEdit)
onSearch(teamSearch, yearEdit, cacheFirst = true)
}
}

Expand All @@ -81,8 +82,12 @@ class EventSelectionActivity : AppCompatActivity() {
.apply()
thread {
// pre-fetch the event matches so it's cached
// we don't process anything here
createCachedTBAInstance(this).getEventMatchesSimple(event.key)
// we don't process anything here. Make sure to catch errors
try {
createCachedTBAInstance(this, cacheFirst = false)
.getEventMatchesSimple(event.key)
} catch (ignored: Exception) {
}
}
dialog.dismiss()

Expand All @@ -96,7 +101,7 @@ class EventSelectionActivity : AppCompatActivity() {
.create().show()
}

private fun onSearch(textView: TextView, yearEdit: EditText) {
private fun onSearch(textView: TextView, yearEdit: EditText, cacheFirst: Boolean) {
val teamNumber = textView.text.toString()

// https://stackoverflow.com/questions/1109022/close-hide-android-soft-keyboard
Expand All @@ -114,7 +119,7 @@ class EventSelectionActivity : AppCompatActivity() {

thread {
try {
val result = createCachedTBAInstance(this)
val result = createCachedTBAInstance(this, cacheFirst)
.getTeamEventsByYearSimple("frc$teamNumber", year)
.sortedBy { it.start_date }
// update the events on the UI thread
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/ca/warp7/android/scouting/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import ca.warp7.android.scouting.entry.Board.*
import ca.warp7.android.scouting.entry.toBoard
import ca.warp7.android.scouting.event.EventInfo
import ca.warp7.android.scouting.event.MatchSchedule
import ca.warp7.android.scouting.tba.createCachedTBAInstance
import ca.warp7.android.scouting.tba.getEventMatchesSimple
import ca.warp7.android.scouting.ui.EntryInMatch
import ca.warp7.android.scouting.ui.EntryListAdapter
Expand Down Expand Up @@ -174,7 +175,8 @@ class MainActivity : AppCompatActivity() {
private fun updateMatchScheduleInThread(event: String, key: String) {
try {
// we get the tba matches, then sort it
val matches = createCachedTBAInstance(this).getEventMatchesSimple(key)
val matches = createCachedTBAInstance(this, cacheFirst = true)
.getEventMatchesSimple(key)
.filter { it.comp_level == "qm" }
.sortedBy { it.match_number }

Expand Down
13 changes: 0 additions & 13 deletions app/src/main/java/ca/warp7/android/scouting/TBAUtil.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ class MatchSchedule(
) {
val size = matches.size / 6

operator fun get(match: Int) = if (match < 0 || match > size) null else
matches.subList(match * 6, (match + 1) * 6)
fun getMatch(match: Int): List<Int> {
return matches.subList(match * 6, (match + 1) * 6)
}

fun forEach(block: (matchNumber: Int, teams: List<Int>) -> Unit) =
(0 until size).forEach { this[it]?.apply { block(it + 1, this) } }
inline fun forEach(block: (matchNumber: Int, teams: List<Int>) -> Unit) {
for (matchNumber in 0 until size) {
block(matchNumber + 1, getMatch(matchNumber))
}
}
}
34 changes: 22 additions & 12 deletions app/src/main/java/ca/warp7/android/scouting/tba/TBA.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import javax.net.ssl.HttpsURLConnection
*/
class TBA(
private val authKey: String,
private val cacheRoot: File
private val cacheRoot: File,
private val cacheFirst: Boolean
) {

private fun getTBAStringNoCache(requestURL: String): String {
Expand All @@ -25,21 +26,30 @@ class TBA(
}

private fun getTBAString(requestURL: String): String {
try {
// Get rid of the slash
val strippedURL = requestURL.substring(1) + ".json"
// Get rid of the slash
val strippedURL = requestURL.substring(1) + ".json"

val cacheFile = File(cacheRoot, strippedURL)
if (cacheFile.exists()) {
return String(cacheFile.inputStream().use { stream -> stream.readBytes() })
}
val cacheFile = File(cacheRoot, strippedURL)

if (cacheFirst && cacheFile.exists()) {
return cacheFile.readText()
}

return try {
val result = getTBAStringNoCache(requestURL)

// make sure we have the parent directories made
cacheFile.parentFile?.mkdirs()
cacheFile.outputStream().use { stream -> stream.write(result.toByteArray()) }
return result
} catch (e: Throwable) {
throw e
cacheFile.writeText(result)

result
} catch (e: Exception) {

if (!cacheFirst && cacheFile.exists()) {
cacheFile.readText()
} else {
throw e
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/ca/warp7/android/scouting/tba/TBAUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ca.warp7.android.scouting.tba

import android.content.Context
import ca.warp7.android.scouting.BuildConfig
import java.io.File

/**
* Create an instance of the blue alliance
*/
fun createCachedTBAInstance(context: Context, cacheFirst: Boolean): TBA {
val cacheFile = File(context.cacheDir, "tba-cache")
return TBA(BuildConfig.TBA_KEY, cacheFile, cacheFirst)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import android.os.VibrationEffect
import android.os.VibrationEffect.DEFAULT_AMPLITUDE
import android.os.Vibrator

/**
* @since v0.4.1
*/

class ActionVibrator(
context: Context,
private val mVibrationOn: Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class QRCodeFragment : Fragment(), ScoutingEntryTab {
}
}

private var prevSize = 0
private var prevSize = -1
private var prevComment = ""
private var message = " "

Expand All @@ -86,6 +86,7 @@ class QRCodeFragment : Fragment(), ScoutingEntryTab {
if (newSize != prevSize || prevComment != comment) {
message = entry.getEncoded()
sendButton?.text = message
prevSize = newSize
view?.let { setQRImage(it) }
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package ca.warp7.android.scouting.ui.field

/**
* Base interface for all custom controls
* @since v0.2.0
*/

interface BaseFieldWidget {
fun updateControlState()
val fieldData: FieldData?
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import ca.warp7.android.scouting.entry.DataPoint

class ButtonField : FrameLayout, BaseFieldWidget {

override val fieldData: FieldData?
private val fieldData: FieldData?
private val counter: TextView?
private val button: Button?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import ca.warp7.android.scouting.entry.DataPoint

class CheckboxField : LinearLayout, BaseFieldWidget {

override val fieldData: FieldData?
private val fieldData: FieldData?
private val checkBox: CheckBox?

private val accent = ContextCompat.getColor(context, R.color.colorAccent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import ca.warp7.android.scouting.entry.DataPoint

class SwitchField : FrameLayout, BaseFieldWidget {

override val fieldData: FieldData?
private val fieldData: FieldData?

private val white = ContextCompat.getColor(context, R.color.colorWhite)
private val gray = ContextCompat.getColor(context, R.color.colorGray)
Expand Down Expand Up @@ -52,7 +52,7 @@ class SwitchField : FrameLayout, BaseFieldWidget {

private fun onClick(data: FieldData) {
val activity = data.scoutingActivity
if (activity.isTimeEnabled()) {
if (activity.isTimeEnabled() || isLite) {
activity.vibrateAction()
val entry = activity.entry
if (entry != null) {
Expand All @@ -67,11 +67,11 @@ class SwitchField : FrameLayout, BaseFieldWidget {
val fieldData = fieldData ?: return
val button = button ?: return

if (fieldData.scoutingActivity.isTimeEnabled()) {
if (fieldData.scoutingActivity.isTimeEnabled() || isLite) {
button.isEnabled = true
val entry = fieldData.scoutingActivity.entry
if (entry != null) {
val lastDP = entry.lastValue(fieldData.typeIndex)
val lastDP = entry.lastValue(fieldData.typeIndex)
isChecked = if (lastDP != null) lastDP.value == 1 else false

if (isChecked) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ https://github.com/llollox/Android-Toggle-Switch
*/
class ToggleField : LinearLayout, BaseFieldWidget {

override val fieldData: FieldData?
private val fieldData: FieldData?

private val almostWhite = ContextCompat.getColor(context, R.color.colorAlmostWhite)
private val almostBlack = ContextCompat.getColor(context, R.color.colorAlmostBlack)
Expand Down

0 comments on commit 1f3dde0

Please sign in to comment.