From 8c3845a07ea5871e24192c71df7ce8f3ee9021ba Mon Sep 17 00:00:00 2001 From: leca Date: Tue, 12 Nov 2024 18:17:01 +0300 Subject: [PATCH] added ability to use application offline --- .../activities/LoginActivity.kt | 9 +++++++ .../activities/MainActivity.kt | 24 +++++++++++++++-- .../activities/NavigatorActivity.kt | 10 ++++--- .../activities/SettingsActivity.kt | 26 ++++++++++++------- app/src/main/res/values-ru/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 6 files changed, 57 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/LoginActivity.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/LoginActivity.kt index aec68d3..c2395f6 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/LoginActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/LoginActivity.kt @@ -104,6 +104,15 @@ class LoginActivity : AppCompatActivity() { } } } + + binding.offlineButton.setOnClickListener { + sharedPreferences.edit().putString("currentGroup", "offline").apply() + sharedPreferences.edit().putStringSet("groups", setOf("offline")).apply() + + val intent = Intent(this, MainActivity::class.java) + startActivity(intent) + finish() + } } private fun fillUpLanguagesSpinner() { diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MainActivity.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MainActivity.kt index 3f88bd1..e267b4d 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MainActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MainActivity.kt @@ -6,6 +6,7 @@ import android.content.Intent import android.os.Bundle import android.view.Menu import android.view.MenuItem +import android.widget.Toast import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.ActionBarDrawerToggle import androidx.appcompat.app.AlertDialog @@ -108,8 +109,8 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte } val net = Net() net.server = sharedPreferences.getString("server", "")!! - - if (!net.serverIsAvailable(this)) { + val currentGroup = sharedPreferences.getString("currentGroup", "offline")!! + if ( currentGroup != "offline" && !net.serverIsAvailable(this)) { noInternetConnectionAvailableNotification(this) } else { synchronize() @@ -393,16 +394,35 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte return true } + private fun isOffline(): Boolean { + val sharedPreferences = EncryptedSharedPreferences.create( + "sensitive", + MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC), + applicationContext, + EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, + EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM + ) + return sharedPreferences.getString("currentGroup", "") == "offline" + } + override fun onNavigationItemSelected(item: MenuItem): Boolean { lateinit var intent: Intent when (item.itemId) { R.id.nav_account -> { + if (isOffline()) { + Toast.makeText(this, getString(R.string.online_only_feature), Toast.LENGTH_SHORT).show() + return false + } intent = Intent(this, AccountSettingsActivity::class.java) settingsActivityLauncher.launch(intent) return true } R.id.nav_groups -> { + if (isOffline()) { + Toast.makeText(this, getString(R.string.online_only_feature), Toast.LENGTH_SHORT).show() + return false + } intent = Intent(this, MyGroupsActivity::class.java) } diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/NavigatorActivity.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/NavigatorActivity.kt index 37324a0..01917de 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/NavigatorActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/NavigatorActivity.kt @@ -24,18 +24,22 @@ class NavigatorActivity : Activity() { var intent = Intent(this, MainActivity::class.java) - if (!isInGroup()) { + if (!isInGroup() && !isOffline()) { intent = Intent(this, GroupActivity::class.java) } - if (!isAuthenticated()) { + if (!isAuthenticated() && !isOffline()) { intent = Intent(this, LoginActivity::class.java); } - + startActivity(intent); finish(); } + private fun isOffline(): Boolean { + return sharedPreferences.getString("currentGroup", "")!! == "offline" + } + private fun isAuthenticated(): Boolean { return sharedPreferences.getString("token", "") != "" } diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/SettingsActivity.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/SettingsActivity.kt index ae5d172..5195133 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/SettingsActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/SettingsActivity.kt @@ -37,17 +37,23 @@ class SettingsActivity : AppCompatActivity() { net.language = sharedPreferences.getString("language", "")!! net.server = sharedPreferences.getString("server", "")!! - groupsNames = mutableListOf() - - for (myGroup in myGroups) { - groupsNames.add(net.getGroupName(myGroup.toInt())) - } - binding = ActivitySettingsBinding.inflate(layoutInflater) setContentView(binding.root) setUpImageCompressionSeekBar() - fillUpCurrentGroupSpinner() + + + if (isOffline()) { + binding.currentGroupSetting.visibility = View.GONE + } else { + groupsNames = mutableListOf() + + for (myGroup in myGroups) { + groupsNames.add(net.getGroupName(myGroup.toInt())) + } + + fillUpCurrentGroupSpinner() + } binding.saveButton.setOnClickListener { sharedPreferences.edit().putString("currentGroup", net.getGroupId(binding.currentGroupSpinner.selectedItem.toString())).apply() @@ -62,14 +68,16 @@ class SettingsActivity : AppCompatActivity() { } } + private fun isOffline(): Boolean { + return sharedPreferences.getString("currentGroup", "offline") == "offline" + } + private fun setUpImageCompressionSeekBar() { val compressionFactor = sharedPreferences.getInt("imageCompression", 1) binding.imageCompressionFactorSeekBar.progress = compressionFactor - 1 } private fun fillUpCurrentGroupSpinner() { - - if (currentGroup == "offline") { binding.currentGroupSetting.visibility = View.GONE } diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index 37c53f9..6e40e50 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -117,6 +117,7 @@ No internet connection available. Please, connect to a network. Ok Log out + This feature is online only en-US ru-RU diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 37c9170..73864d8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -115,6 +115,7 @@ No internet connection available. Please, connect to a network. Ok Log out + This feature is online only en-US ru-RU