diff --git a/app/src/main/java/com/yacgroup/yacguide/CountryActivity.kt b/app/src/main/java/com/yacgroup/yacguide/CountryActivity.kt index 2f0d7dde..89a4a93b 100644 --- a/app/src/main/java/com/yacgroup/yacguide/CountryActivity.kt +++ b/app/src/main/java/com/yacgroup/yacguide/CountryActivity.kt @@ -21,16 +21,31 @@ import android.content.Intent import android.os.Bundle import android.view.View import android.widget.LinearLayout +import com.yacgroup.yacguide.activity_properties.* import com.yacgroup.yacguide.network.CountryParser import com.yacgroup.yacguide.utils.IntentConstants import com.yacgroup.yacguide.utils.WidgetUtils -class CountryActivity : UpdatableTableActivity() { +class CountryActivity : TableActivityWithOptionsMenu() { + + private lateinit var _updatable: Updatable override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - jsonParser = CountryParser(db, this) + val rockSearchable = RockSearchable( + this, + ) { db.getRocks() } + val ascentFilterable = AscentFilterable( + this, + { db.getProjectedRocks() }, + { db.getBotchedRocks() } + ) + _updatable = Updatable( + this, + CountryParser(db) + ) { db.deleteCountriesRecursively() } + properties = arrayListOf(rockSearchable, ascentFilterable, _updatable) WhatsNewInfo(this).let { if (it.checkForVersionUpdate()) { @@ -59,15 +74,7 @@ class CountryActivity : UpdatableTableActivity() { layout.addView(WidgetUtils.createHorizontalLine(this, 1)) } if (countries.isEmpty()) { - displayDownloadButton() + layout.addView(_updatable.getDownloadButton()) } } - - override fun deleteContent() = db.deleteCountriesRecursively() - - override fun searchRocks() = db.getRocks() - - override fun searchProjects() = db.getProjectedRocks() - - override fun searchBotches() = db.getBotchedRocks() } diff --git a/app/src/main/java/com/yacgroup/yacguide/RegionActivity.kt b/app/src/main/java/com/yacgroup/yacguide/RegionActivity.kt index 3c83430d..7e32b43c 100644 --- a/app/src/main/java/com/yacgroup/yacguide/RegionActivity.kt +++ b/app/src/main/java/com/yacgroup/yacguide/RegionActivity.kt @@ -21,20 +21,35 @@ import android.content.Intent import android.os.Bundle import android.view.View import android.widget.LinearLayout +import com.yacgroup.yacguide.activity_properties.* import com.yacgroup.yacguide.network.RegionParser import com.yacgroup.yacguide.utils.IntentConstants import com.yacgroup.yacguide.utils.WidgetUtils -class RegionActivity : UpdatableTableActivity() { +class RegionActivity : TableActivityWithOptionsMenu() { private lateinit var _countryName: String + private lateinit var _updatable: Updatable override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) _countryName = intent.getStringExtra(IntentConstants.COUNTRY_KEY).toString() - jsonParser = RegionParser(db, this, _countryName) + + val rockSearchable = RockSearchable( + this + ) { db.getRocksForCountry(_countryName) } + val ascentFilterable = AscentFilterable( + this, + { db.getProjectedRocksForCountry(_countryName) }, + { db.getBotchedRocksForCountry(_countryName) } + ) + _updatable = Updatable( + this, + RegionParser(db, _countryName) + ) { db.deleteRegionsRecursively(_countryName) } + properties = arrayListOf(rockSearchable, ascentFilterable, _updatable) } override fun getLayoutId() = R.layout.activity_region @@ -57,15 +72,7 @@ class RegionActivity : UpdatableTableActivity() { layout.addView(WidgetUtils.createHorizontalLine(this, 1)) } if (regions.isEmpty()) { - displayDownloadButton() + layout.addView(_updatable.getDownloadButton()) } } - - override fun deleteContent() = db.deleteRegionsRecursively(_countryName) - - override fun searchRocks() = db.getRocksForCountry(_countryName) - - override fun searchProjects() = db.getProjectedRocksForCountry(_countryName) - - override fun searchBotches() = db.getBotchedRocksForCountry(_countryName) } diff --git a/app/src/main/java/com/yacgroup/yacguide/RockActivity.kt b/app/src/main/java/com/yacgroup/yacguide/RockActivity.kt index 95f577b9..d795cc2d 100644 --- a/app/src/main/java/com/yacgroup/yacguide/RockActivity.kt +++ b/app/src/main/java/com/yacgroup/yacguide/RockActivity.kt @@ -23,6 +23,8 @@ import android.graphics.Typeface import android.os.Bundle import android.view.View import android.widget.* +import com.yacgroup.yacguide.activity_properties.AscentFilterable +import com.yacgroup.yacguide.activity_properties.RockSearchable import com.yacgroup.yacguide.database.comment.SectorComment import com.yacgroup.yacguide.database.DatabaseWrapper @@ -33,7 +35,7 @@ import com.yacgroup.yacguide.utils.ParserUtils import com.yacgroup.yacguide.utils.SearchBarHandler import com.yacgroup.yacguide.utils.WidgetUtils -class RockActivity : TableActivity() { +class RockActivity : TableActivityWithOptionsMenu() { private lateinit var _sector: Sector private lateinit var _searchBarHandler: SearchBarHandler @@ -44,13 +46,25 @@ class RockActivity : TableActivity() { super.onCreate(savedInstanceState) val sectorId = intent.getIntExtra(IntentConstants.SECTOR_KEY, DatabaseWrapper.INVALID_ID) - _sector = db.getSector(sectorId)!! + + val rockSearchable = RockSearchable( + this + ) { db.getRocksForSector(sectorId) } + val ascentFilterable = AscentFilterable( + this, + { db.getProjectedRocksForSector(sectorId) }, + { db.getBotchedRocksForSector(sectorId) } + ) + properties = arrayListOf(rockSearchable, ascentFilterable) + _searchBarHandler = SearchBarHandler(findViewById(R.id.searchBarLayout), R.string.rock_search, R.array.rockFilters) { rockNamePart, rockFilter -> _onSearchBarUpdate(rockNamePart, rockFilter) } } + override fun getLayoutId() = R.layout.activity_rock + override fun showComments(v: View) { val comments = db.getSectorComments(_sector.id) if (comments.isNotEmpty()) { @@ -142,8 +156,4 @@ class RockActivity : TableActivity() { && rock.status != Rock.statusProhibited && rock.status != Rock.statusCollapsed } - - override fun getLayoutId(): Int { - return R.layout.activity_rock - } } diff --git a/app/src/main/java/com/yacgroup/yacguide/RouteActivity.kt b/app/src/main/java/com/yacgroup/yacguide/RouteActivity.kt index 988bf3c7..99e44a18 100644 --- a/app/src/main/java/com/yacgroup/yacguide/RouteActivity.kt +++ b/app/src/main/java/com/yacgroup/yacguide/RouteActivity.kt @@ -68,6 +68,8 @@ class RouteActivity : TableActivity() { findViewById(R.id.infoTextView).text = rockStatus } + override fun getLayoutId() = R.layout.activity_route + @Suppress("UNUSED_PARAMETER") fun showMap(v: View) { try { @@ -170,9 +172,5 @@ class RouteActivity : TableActivity() { && route.statusId != Route.STATUS_UNACKNOWLEDGED && route.statusId != Route.STATUS_UNFINISHED } - - override fun getLayoutId(): Int { - return R.layout.activity_route - } } diff --git a/app/src/main/java/com/yacgroup/yacguide/SectorActivity.kt b/app/src/main/java/com/yacgroup/yacguide/SectorActivity.kt index 31bb838e..0532791e 100644 --- a/app/src/main/java/com/yacgroup/yacguide/SectorActivity.kt +++ b/app/src/main/java/com/yacgroup/yacguide/SectorActivity.kt @@ -22,6 +22,7 @@ import android.graphics.Typeface import android.os.Bundle import android.view.View import android.widget.* +import com.yacgroup.yacguide.activity_properties.* import com.yacgroup.yacguide.database.comment.RegionComment import com.yacgroup.yacguide.database.DatabaseWrapper @@ -33,17 +34,30 @@ import com.yacgroup.yacguide.utils.IntentConstants import com.yacgroup.yacguide.utils.ParserUtils import com.yacgroup.yacguide.utils.WidgetUtils -class SectorActivity : UpdatableTableActivity() { +class SectorActivity : TableActivityWithOptionsMenu() { private lateinit var _region: Region + private lateinit var _updatable: Updatable override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val regionId = intent.getIntExtra(IntentConstants.REGION_KEY, DatabaseWrapper.INVALID_ID) - - jsonParser = SectorParser(db, this, regionId) _region = db.getRegion(regionId)!! + + val rockSearchable = RockSearchable( + this + ) { db.getRocksForRegion(regionId) } + val ascentFilterable = AscentFilterable( + this, + { db.getProjectedRocksForRegion(regionId) }, + { db.getBotchedRocksForRegion(regionId) } + ) + _updatable = Updatable( + this, + SectorParser(db, regionId) + ) { db.deleteSectorsRecursively(_region.id) } + properties = arrayListOf(rockSearchable, ascentFilterable, _updatable) } override fun getLayoutId() = R.layout.activity_sector @@ -103,18 +117,10 @@ class SectorActivity : UpdatableTableActivity() { layout.addView(WidgetUtils.createHorizontalLine(this, 1)) } if (sectors.isEmpty()) { - displayDownloadButton() + layout.addView(_updatable.getDownloadButton()) } } - override fun deleteContent() = db.deleteSectorsRecursively(_region.id) - - override fun searchRocks() = db.getRocksForRegion(_region.id) - - override fun searchProjects() = db.getProjectedRocksForRegion(_region.id) - - override fun searchBotches() = db.getBotchedRocksForRegion(_region.id) - private fun _generateRockCountString(rocks: List): String { val countSummits = customSettings.getBoolean(getString(R.string.count_summits), resources.getBoolean(R.bool.count_summits)) val countMassifs = customSettings.getBoolean(getString(R.string.count_massifs), resources.getBoolean(R.bool.count_massifs)) diff --git a/app/src/main/java/com/yacgroup/yacguide/TableActivity.kt b/app/src/main/java/com/yacgroup/yacguide/TableActivity.kt index 48afeb71..3cddc691 100644 --- a/app/src/main/java/com/yacgroup/yacguide/TableActivity.kt +++ b/app/src/main/java/com/yacgroup/yacguide/TableActivity.kt @@ -78,5 +78,5 @@ abstract class TableActivity : BaseNavigationActivity() { return "$botchAdd$projectAdd$watchingAdd" } - protected abstract fun displayContent() + abstract fun displayContent() } diff --git a/app/src/main/java/com/yacgroup/yacguide/TableActivityWithOptionsMenu.kt b/app/src/main/java/com/yacgroup/yacguide/TableActivityWithOptionsMenu.kt new file mode 100644 index 00000000..a22e8940 --- /dev/null +++ b/app/src/main/java/com/yacgroup/yacguide/TableActivityWithOptionsMenu.kt @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2019 Axel Paetzold + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.yacgroup.yacguide + +import android.annotation.SuppressLint +import android.view.Menu +import android.view.MenuItem +import androidx.appcompat.view.menu.MenuBuilder +import androidx.core.view.MenuCompat +import com.yacgroup.yacguide.activity_properties.ActivityProperty + +abstract class TableActivityWithOptionsMenu : TableActivity() { + + protected lateinit var properties: ArrayList + + @SuppressLint("RestrictedApi") + override fun onCreateOptionsMenu(menu: Menu): Boolean { + menuInflater.inflate(R.menu.options_menu, menu) + properties.map { menu.setGroupVisible(it.getMenuGroupId(), true) } + MenuCompat.setGroupDividerEnabled(menu, true); + if (menu is MenuBuilder) { + menu.setOptionalIconsVisible(true) + } + return true + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + for (prop in properties) { + if (item.groupId == prop.getMenuGroupId()) { + prop.onMenuAction(item.itemId) + break + } + } + return true + } +} \ No newline at end of file diff --git a/app/src/main/java/com/yacgroup/yacguide/UpdatableTableActivity.kt b/app/src/main/java/com/yacgroup/yacguide/UpdatableTableActivity.kt deleted file mode 100644 index f3dbe536..00000000 --- a/app/src/main/java/com/yacgroup/yacguide/UpdatableTableActivity.kt +++ /dev/null @@ -1,167 +0,0 @@ -/* - * Copyright (C) 2019 Fabian Kantereit - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package com.yacgroup.yacguide - -import android.annotation.SuppressLint -import android.app.Dialog -import android.content.Intent -import android.view.Menu -import android.view.MenuItem -import android.view.View -import android.widget.* -import androidx.appcompat.app.AppCompatDialog -import androidx.appcompat.view.menu.MenuBuilder -import androidx.core.view.MenuCompat -import com.yacgroup.yacguide.database.Rock -import com.yacgroup.yacguide.network.JSONWebParser -import com.yacgroup.yacguide.utils.DialogWidgetBuilder -import com.yacgroup.yacguide.utils.IntentConstants -import com.yacgroup.yacguide.utils.NetworkUtils - -abstract class UpdatableTableActivity : TableActivity(), UpdateListener { - - private var _updateDialog: Dialog? = null - protected var jsonParser: JSONWebParser? = null - - @SuppressLint("RestrictedApi") - override fun onCreateOptionsMenu(menu: Menu): Boolean { - menuInflater.inflate(R.menu.sync_delete, menu) - MenuCompat.setGroupDividerEnabled(menu, true); - if (menu is MenuBuilder) { - menu.setOptionalIconsVisible(true) - } - return true - } - - override fun onOptionsItemSelected(item: MenuItem): Boolean { - when (item.itemId) { - R.id.action_download -> update() - R.id.action_filter_projects -> _searchRockGlobally(ClimbingObjectFilter.eProject) - R.id.action_filter_botches -> _searchRockGlobally(ClimbingObjectFilter.eBotch) - R.id.action_search -> _searchRockGlobally() - R.id.action_delete -> _delete() - else -> return super.onOptionsItemSelected(item) - } - return true - } - - // UpdateListener - override fun onUpdateFinished(success: Boolean) { - _updateDialog?.dismiss() - - if (success) { - Toast.makeText(this, R.string.objects_refreshed, Toast.LENGTH_SHORT).show() - } else { - Toast.makeText(this, R.string.error_on_refresh, Toast.LENGTH_SHORT).show() - } - displayContent() - } - - override fun onUpdateStatus(statusMessage: String) { - runOnUiThread { - _updateDialog?.findViewById(R.id.dialogText)?.text = statusMessage - } - } - - @Suppress("UNUSED_PARAMETER") - fun update(v: View = View(this)) { - if (!NetworkUtils.isNetworkAvailable(this)) { - Toast.makeText(this, R.string.no_internet_connection, Toast.LENGTH_LONG).show() - return - } - jsonParser?.fetchData() - showUpdateDialog() - } - - private fun _delete() { - DialogWidgetBuilder(this, R.string.dialog_question_delete).apply { - setIcon(android.R.drawable.ic_dialog_alert) - setNegativeButton() - setPositiveButton { _, _ -> - deleteContent() - Toast.makeText( - this@UpdatableTableActivity, - R.string.objects_deleted, - Toast.LENGTH_SHORT - ).show() - displayContent() - } - }.show() - } - - private fun showUpdateDialog() { - _updateDialog = Dialog(this) - _updateDialog?.setContentView(R.layout.info_dialog) - _updateDialog?.setCancelable(false) - _updateDialog?.setCanceledOnTouchOutside(false) - _updateDialog?.show() - } - - protected fun displayDownloadButton() { - val layout = findViewById(R.id.tableLayout) - val downloadButton = layoutInflater.inflate(R.layout.button_download, null) as ImageButton - layout.addView(downloadButton) - } - - protected abstract fun deleteContent() - - protected abstract fun searchRocks(): List - - protected abstract fun searchProjects(): List - - protected abstract fun searchBotches(): List - - private fun _searchRockGlobally(filter: ClimbingObjectFilter = ClimbingObjectFilter.eNone) { - when (filter) { - ClimbingObjectFilter.eProject -> _goToFilteredRocksView(searchProjects()) - ClimbingObjectFilter.eBotch -> _goToFilteredRocksView(searchBotches()) - else -> { - val searchDialog = AppCompatDialog(this) - searchDialog.setContentView(R.layout.search_dialog) - searchDialog.findViewById