Skip to content

Commit

Permalink
Fixed 6-day forecast crash (#51)
Browse files Browse the repository at this point in the history
* Add local signing config

* Fix build error

* Fix warning

* Prevent crash on error

* Calculate number of rows dynamically

---------

Co-authored-by: Leonard <[email protected]>
  • Loading branch information
Lee245 and Leonard authored Mar 25, 2024
1 parent 31974f5 commit c5bf168
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ dependencies {
implementation 'org.jsoup:jsoup:1.17.2'
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"
android:exported="true">
<intent-filter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import foss.cnugteren.nlweer.databinding.FragmentKnmiSixdayforecastBinding
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.nodes.Element
import java.lang.IllegalArgumentException
import kotlin.math.ceil
import kotlin.math.floor
import kotlin.math.min
Expand Down Expand Up @@ -74,8 +75,7 @@ class KnmiSixDayForecastFragment : Fragment() {

private fun loadPage() {
val webView = binding.webView
val htmlBuilder = HtmlBuilder()
webView.loadData(htmlBuilder.buildHtmlPageWithLoadingMessage(), "text/html", "utf-8")
webView.loadData(HtmlBuilder().buildHtmlPageWithLoadingMessage(), "text/html", "utf-8")
RetrieveWebPage().execute(getURL())
}

Expand Down Expand Up @@ -112,15 +112,28 @@ class KnmiSixDayForecastFragment : Fragment() {
return
}

val tableData = getTableData(tableWrapperElement)
val htmlPageToShow = htmlBuilder.buildHtmlPageWithTables(tableData)
webView.loadData(htmlPageToShow, "text/html", "UTF-8")
try {
val tableData = getTableData(tableWrapperElement)
val htmlPageToShow = htmlBuilder.buildHtmlPageWithTables(tableData)
webView.loadData(htmlPageToShow, "text/html", "UTF-8")
}
catch (ex: Exception) {
webView.loadData(htmlBuilder.buildHtmPageWithLoadingError(), "text/html", "utf-8")
}
}

// Get the weather data per day of the week
private fun getTableData(tableWrapperElement: Element) : Array<Array<String>> {
val weatherPerDay = tableWrapperElement.select("li")
val tableData = Array<Array<String>>(weatherPerDay.size, { Array<String>(15, {""}) })
val numberOfTableCells = weatherPerDay.firstOrNull()?.select("span.weather-map__table-cell")?.size
if (numberOfTableCells == null) {
throw IllegalArgumentException("No rows with weather data found")
}
// Day of the week + date + weather icon + 2 rows for each property
val numberOfRows = 2 * (numberOfTableCells - 1) + 1
val tableData = Array<Array<String>>(weatherPerDay.size, { Array<String>(numberOfRows, {""}) })


weatherPerDay.forEachIndexed { colIndex, column ->
var rowIndex = 0

Expand Down Expand Up @@ -202,7 +215,8 @@ class KnmiSixDayForecastFragment : Fragment() {
var column = tableNumber * columnsPerTable
// Loop until either all columns for the current table have been processed,
// or all columns have already been processed
while (column < (tableNumber + 1 ) * columnsPerTable && column < totalNumberOfColumns) {
val endColumn = min((tableNumber + 1 ) * columnsPerTable, totalNumberOfColumns)
while (column < endColumn) {
var content = tableData[column][row]
if (URLUtil.isValidUrl(content)) {
content = """<img alt="" src="""" + content + """" width="""" + imageSize + """px"/>"""
Expand Down
4 changes: 4 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ android.enableJetifier=true
kotlin.code.style=official
android.nonTransitiveRClass=false
android.nonFinalResIds=false
RELEASE_STORE_FILE=~/.android/debug.keystore
RELEASE_STORE_PASSWORD=android
RELEASE_KEY_ALIAS=androiddebugkey
RELEASE_KEY_PASSWORD=android

0 comments on commit c5bf168

Please sign in to comment.