Skip to content

Commit

Permalink
Update dependencies and reimplement deprecated methods
Browse files Browse the repository at this point in the history
- Reimplemented deprecated methods:
    - Replaced calls to startActivityForResult by registerForActivityResult (every call in application);
    - Removed unused method (setTargetFragment) from SettingsActivity;
- Updated gradle dependencies.
  • Loading branch information
mauromascarenhas committed Jun 10, 2021
1 parent 6ee6052 commit f6f7e6e
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 109 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion BibliotecaUFABC/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ dependencies {
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.vectordrawable:vectordrawable:1.1.0'
implementation 'androidx.activity:activity-ktx:1.3.0-beta01'
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.preference:preference-ktx:1.1.1'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.fragment:fragment-ktx:1.4.0-alpha02'
implementation "androidx.concurrent:concurrent-futures-ktx:1.1.0"
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
Expand All @@ -71,7 +73,7 @@ dependencies {
}

dependencies {
def work_version = '2.6.0-alpha02'
def work_version = '2.6.0-beta01'
implementation "androidx.work:work-runtime-ktx:$work_version"
androidTestImplementation "androidx.work:work-testing:$work_version"
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.os.Bundle
import android.os.Handler
import android.view.MenuItem
import android.webkit.WebView
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
Expand Down Expand Up @@ -36,6 +37,25 @@ class BookViewerActivity : AppCompatActivity(), BookViewerFragment.ReservationEv
private lateinit var messageViewModel: MessageViewModel
private lateinit var bookViewerViewModel: BookViewerViewModel

val openLoginActivity = registerForActivityResult(ActivityResultContracts
.StartActivityForResult()){ result ->
if (result.resultCode == Activity.RESULT_OK && result.data != null){
detailsWebClient?.resetCounters()

bookViewerViewModel.setLoginRequest(true)
bookViewerViewModel.setReservationRequest(true)

dataSource?.loadUrl(bookViewerViewModel.bookURL.value!!)
Snackbar.make(bookViewerParent,
getString(R.string.snack_message_connected,
result.data!!.getStringExtra(Constants.CONNECTED_STATUS_USER_NAME)),
Snackbar.LENGTH_LONG).show()

loggedInAs.value = result.data!!.getStringExtra(Constants.CONNECTED_STATUS_USER_NAME)
}
else bookViewerViewModel.setLoginCancelled(true)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_book_viewer)
Expand Down Expand Up @@ -63,28 +83,6 @@ class BookViewerActivity : AppCompatActivity(), BookViewerFragment.ReservationEv
}
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)

if (requestCode == Constants.ACTIVITY_LOGIN_REQUEST_CODE){
if (resultCode == Activity.RESULT_OK && data != null){
detailsWebClient?.resetCounters()

bookViewerViewModel.setLoginRequest(true)
bookViewerViewModel.setReservationRequest(true)

dataSource?.loadUrl(bookViewerViewModel.bookURL.value!!)
Snackbar.make(bookViewerParent,
getString(R.string.snack_message_connected,
data.getStringExtra(Constants.CONNECTED_STATUS_USER_NAME)),
Snackbar.LENGTH_LONG).show()

loggedInAs.value = data.getStringExtra(Constants.CONNECTED_STATUS_USER_NAME)
}
else bookViewerViewModel.setLoginCancelled(true)
}
}

private fun initializeBookData(){
var hasData = true
var bookData = intent.getStringExtra(Constants.BOOK_CODE)
Expand Down Expand Up @@ -112,8 +110,7 @@ class BookViewerActivity : AppCompatActivity(), BookViewerFragment.ReservationEv
commitAllowingStateLoss()
}
if (bookViewerViewModel.loginCancelled.value!!){
startActivityForResult(Intent(this, LoginActivity::class.java),
Constants.ACTIVITY_LOGIN_REQUEST_CODE)
openLoginActivity.launch(Intent(this, LoginActivity::class.java))
bookViewerViewModel.setLoginCancelled(false)
}
else dataSource?.reload()
Expand All @@ -129,8 +126,7 @@ class BookViewerActivity : AppCompatActivity(), BookViewerFragment.ReservationEv
commitAllowingStateLoss()
}
if (bookViewerViewModel.loginCancelled.value!!){
startActivityForResult(Intent(this, LoginActivity::class.java),
Constants.ACTIVITY_LOGIN_REQUEST_CODE)
openLoginActivity.launch(Intent(this, LoginActivity::class.java))
bookViewerViewModel.setLoginCancelled(false)
}
else dataSource?.reload()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.os.Bundle
import android.provider.Settings
import android.webkit.CookieManager
import android.webkit.WebView
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
import com.google.android.material.bottomnavigation.BottomNavigationView
import androidx.appcompat.app.AppCompatActivity
Expand Down Expand Up @@ -61,6 +62,32 @@ class MainActivity : AppCompatActivity(), ReservationsRecyclerFragment.Reservati
if(key == getString(R.string.key_general_share_app_enabled)) invalidateOptionsMenu()
}

val openLoginActivity = registerForActivityResult(ActivityResultContracts
.StartActivityForResult()){ result ->
if (result.resultCode == Activity.RESULT_OK){
val user = result.data?.getStringExtra(Constants.CONNECTED_STATUS_USER_NAME)
if (!user.isNullOrEmpty()){
homeViewModel.defineLoginStatus(true)
homeViewModel.defineConnectedUserName(user)
}
}
}
private val openSyncPermissionRequest = registerForActivityResult(ActivityResultContracts
.StartActivityForResult()){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if (!Settings.canDrawOverlays(this))
AlertDialog.Builder(this).apply {
setTitle(R.string.notification_sync_denied_title)
setMessage(R.string.notification_sync_denied_message)
setPositiveButton(R.string.dialog_button_ok, null)
}.create().show()
else {
startPowerSavingSettings()
setSyncSchedule()
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Expand Down Expand Up @@ -112,35 +139,6 @@ class MainActivity : AppCompatActivity(), ReservationsRecyclerFragment.Reservati
super.onDestroy()
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode){
Constants.ACTIVITY_LOGIN_REQUEST_CODE -> {
if (resultCode == Activity.RESULT_OK){
val user = data?.getStringExtra(Constants.CONNECTED_STATUS_USER_NAME)
if (!user.isNullOrEmpty()){
homeViewModel.defineLoginStatus(true)
homeViewModel.defineConnectedUserName(user)
}
}
}
Constants.SYNC_PERMISSION_REQUEST_ID -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if (!Settings.canDrawOverlays(this))
AlertDialog.Builder(this).apply {
setTitle(R.string.notification_sync_denied_title)
setMessage(R.string.notification_sync_denied_message)
setPositiveButton(R.string.dialog_button_ok, null)
}.create().show()
else {
startPowerSavingSettings()
setSyncSchedule()
}
}
}
}
}

override fun onRequestRefresh() {
dataSource?.loadUrl(Constants.URL_LIBRARY_NEWEST)
MessageSnackbar.make(container, R.string.snack_message_refreshing_newest,
Expand Down Expand Up @@ -197,10 +195,7 @@ class MainActivity : AppCompatActivity(), ReservationsRecyclerFragment.Reservati
if (loadingAlert.isShowing) loadingAlert.dismiss()
if (prefs.getBoolean(getString(R.string.key_privacy_auto_login), true) &&
!homeViewModel.hasRequestedLogin.value!!) {
startActivityForResult(
Intent(this, LoginActivity::class.java),
Constants.ACTIVITY_LOGIN_REQUEST_CODE
)
openLoginActivity.launch(Intent(this, LoginActivity::class.java))
homeViewModel.hasRequestedLogin.value = true
}
}
Expand Down Expand Up @@ -256,7 +251,7 @@ class MainActivity : AppCompatActivity(), ReservationsRecyclerFragment.Reservati
Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:$packageName")).also {
if (packageManager.resolveActivity(it, PackageManager.MATCH_DEFAULT_ONLY) != null)
startActivityForResult(it, Constants.SYNC_PERMISSION_REQUEST_ID)
openSyncPermissionRequest.launch(it)
}
}))
}.create().show()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.nintersoft.bibliotecaufabc.activities

import android.app.Activity
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
Expand All @@ -13,6 +12,7 @@ import android.view.inputmethod.EditorInfo
import android.webkit.WebView
import android.widget.EditText
import android.widget.LinearLayout
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
Expand All @@ -39,6 +39,30 @@ class SearchActivity : AppCompatActivity() {
private lateinit var searchViewModel : SearchViewModel
private lateinit var messageViewModel : MessageViewModel

@Suppress("UNCHECKED_CAST")
private val openSearchFilterActivity = registerForActivityResult(ActivityResultContracts
.StartActivityForResult()){ result ->
if (result.resultCode == RESULT_OK && result.data != null){
with(result.data!!){
searchViewModel.setSearchField(getIntExtra(Constants.SEARCH_FILTER_FIELD, 0))
searchViewModel.setSearchFilter(getSerializableExtra(Constants.SEARCH_FILTER_TYPE)
as ArrayList<Boolean>)
searchViewModel.setLibraryFilters(getSerializableExtra(Constants.SEARCH_FILTER_LIBRARY)
as ArrayList<Boolean>)
}

AlertDialog.Builder(this).apply {
setTitle(R.string.dialog_filter_changed_title)
setMessage(R.string.dialog_filter_changed_message)
setNegativeButton(R.string.dialog_button_no, null)
setPositiveButton(R.string.dialog_button_yes, ({ _, _ ->
performNewSearch(searchViewModel.query.value!!)
}))
}.create().show()
}
}


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_search)
Expand All @@ -65,31 +89,6 @@ class SearchActivity : AppCompatActivity() {
return super.onOptionsItemSelected(item)
}

@Suppress("UNCHECKED_CAST")
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)

if (requestCode == Constants.ACTIVITY_SEARCH_FILTER_REQUEST_CODE
&& resultCode == Activity.RESULT_OK && data != null){
with(data){
searchViewModel.setSearchField(getIntExtra(Constants.SEARCH_FILTER_FIELD, 0))
searchViewModel.setSearchFilter(getSerializableExtra(Constants.SEARCH_FILTER_TYPE)
as ArrayList<Boolean>)
searchViewModel.setLibraryFilters(getSerializableExtra(Constants.SEARCH_FILTER_LIBRARY)
as ArrayList<Boolean>)
}

AlertDialog.Builder(this).apply {
setTitle(R.string.dialog_filter_changed_title)
setMessage(R.string.dialog_filter_changed_message)
setNegativeButton(R.string.dialog_button_no, null)
setPositiveButton(R.string.dialog_button_yes, ({ _, _ ->
performNewSearch(searchViewModel.query.value!!)
}))
}.create().show()
}
}

private fun configureWebView(){
dataSource = WebView(this)
Functions.configureWebView(dataSource!!, SearchWebClient(searchViewModel))
Expand All @@ -111,7 +110,7 @@ class SearchActivity : AppCompatActivity() {
filters.putExtra(Constants.SEARCH_FILTER_TYPE, searchViewModel.searchFilter.value)
filters.putExtra(Constants.SEARCH_FILTER_FIELD, searchViewModel.searchField.value!!)
filters.putExtra(Constants.SEARCH_FILTER_LIBRARY, searchViewModel.searchLibrary.value)
startActivityForResult(filters, Constants.ACTIVITY_SEARCH_FILTER_REQUEST_CODE)
openSearchFilterActivity.launch(filters)
}

btn_more.setOnClickListener{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ class SettingsActivity : AppCompatActivity(),
pref.fragment
).apply {
arguments = args
setTargetFragment(caller, 0)
}
// Replace the existing Fragment with the new Fragment
supportFragmentManager.beginTransaction()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,13 @@ object Constants {
const val SYNC_EXECUTIONER_FIRST_DELAY_MIN : Long = 15
const val SYNC_REMINDER_NOTIFICATION_INTERVAL: Long = 5

const val ACTIVITY_LOGIN_REQUEST_CODE = 11
const val ACTIVITY_RENEWAL_REQUEST_CODE = 13
const val ACTIVITY_SEARCH_FILTER_REQUEST_CODE = 15

const val SYNC_NOTIFICATION_ID = 9000
const val SYNC_NOTIFICATION_UPDATE_ID = 9001
const val SYNC_NOTIFICATION_REVOKED_ID = 9002
const val SYNC_NOTIFICATION_REMINDER_ID = 9003

const val SYNC_PERMISSION_REQUEST_ID = 10001

val BOOK_COVER_PLACEHOLDERS = listOf(R.drawable.ic_book_cover_fill_01,
R.drawable.ic_book_cover_fill_02, R.drawable.ic_book_cover_fill_03)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.nintersoft.bibliotecaufabc.R
import com.nintersoft.bibliotecaufabc.activities.BookViewerActivity
import com.nintersoft.bibliotecaufabc.activities.LoginActivity
import com.nintersoft.bibliotecaufabc.global.Constants
import kotlinx.android.synthetic.main.fragment_book_viewer.*
import org.json.JSONException
import org.json.JSONObject
Expand Down Expand Up @@ -67,8 +67,8 @@ class BookViewerFragment : Fragment() {
listener = null
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
buildLate()
setListeners()
}
Expand Down Expand Up @@ -257,9 +257,8 @@ class BookViewerFragment : Fragment() {
setMessage(R.string.dialog_warning_message_user_disconnected)
setPositiveButton(R.string.dialog_button_yes, ({_ ,_ ->
showLoadingAlert()
activity?.startActivityForResult(Intent(activity,
LoginActivity::class.java),
Constants.ACTIVITY_LOGIN_REQUEST_CODE)
(activity as BookViewerActivity?)?.openLoginActivity
?.launch(Intent(activity, LoginActivity::class.java))
}))
setNegativeButton(R.string.dialog_button_cancel, null)
}.create().show()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ class HomeFragment : Fragment() {
@SuppressLint("QueryPermissionsNeeded")
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId){
R.id.login -> activity?.startActivityForResult(Intent(context,
LoginActivity::class.java), Constants.ACTIVITY_LOGIN_REQUEST_CODE)
R.id.login -> (activity as MainActivity?)?.openLoginActivity
?.launch(Intent(context, LoginActivity::class.java))
R.id.refresh -> listener?.onRequestRefresh()
R.id.sign_out -> {
item.isVisible = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ class HomeViewAdapter : RecyclerView.Adapter<HomeViewAdapter.HomeBookViewHolder>
BOOK_COVER_PLACEHOLDERS[position % Constants.BOOK_COVER_PLACEHOLDERS.size])).
into(holder.bookCover!!)

holder.card?.setOnClickListener { _clickedBook.value = book }
holder.card?.setOnClickListener { _clickedBook.value = book!! }
holder.card?.setOnLongClickListener {
_selectedBook.value = book
_selectedBook.value = book!!
true
}
}
Expand Down
Loading

0 comments on commit f6f7e6e

Please sign in to comment.