added ability to use application offline

This commit is contained in:
leca 2024-11-12 18:17:01 +03:00
parent d08a79e981
commit 8c3845a07e
6 changed files with 57 additions and 14 deletions

View File

@ -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() { private fun fillUpLanguagesSpinner() {

View File

@ -6,6 +6,7 @@ import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.ActionBarDrawerToggle import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
@ -108,8 +109,8 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
} }
val net = Net() val net = Net()
net.server = sharedPreferences.getString("server", "")!! net.server = sharedPreferences.getString("server", "")!!
val currentGroup = sharedPreferences.getString("currentGroup", "offline")!!
if (!net.serverIsAvailable(this)) { if ( currentGroup != "offline" && !net.serverIsAvailable(this)) {
noInternetConnectionAvailableNotification(this) noInternetConnectionAvailableNotification(this)
} else { } else {
synchronize() synchronize()
@ -393,16 +394,35 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
return true 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 { override fun onNavigationItemSelected(item: MenuItem): Boolean {
lateinit var intent: Intent lateinit var intent: Intent
when (item.itemId) { when (item.itemId) {
R.id.nav_account -> { 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) intent = Intent(this, AccountSettingsActivity::class.java)
settingsActivityLauncher.launch(intent) settingsActivityLauncher.launch(intent)
return true return true
} }
R.id.nav_groups -> { 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) intent = Intent(this, MyGroupsActivity::class.java)
} }

View File

@ -24,11 +24,11 @@ class NavigatorActivity : Activity() {
var intent = Intent(this, MainActivity::class.java) var intent = Intent(this, MainActivity::class.java)
if (!isInGroup()) { if (!isInGroup() && !isOffline()) {
intent = Intent(this, GroupActivity::class.java) intent = Intent(this, GroupActivity::class.java)
} }
if (!isAuthenticated()) { if (!isAuthenticated() && !isOffline()) {
intent = Intent(this, LoginActivity::class.java); intent = Intent(this, LoginActivity::class.java);
} }
@ -36,6 +36,10 @@ class NavigatorActivity : Activity() {
finish(); finish();
} }
private fun isOffline(): Boolean {
return sharedPreferences.getString("currentGroup", "")!! == "offline"
}
private fun isAuthenticated(): Boolean { private fun isAuthenticated(): Boolean {
return sharedPreferences.getString("token", "") != "" return sharedPreferences.getString("token", "") != ""
} }

View File

@ -37,17 +37,23 @@ class SettingsActivity : AppCompatActivity() {
net.language = sharedPreferences.getString("language", "")!! net.language = sharedPreferences.getString("language", "")!!
net.server = sharedPreferences.getString("server", "")!! net.server = sharedPreferences.getString("server", "")!!
binding = ActivitySettingsBinding.inflate(layoutInflater)
setContentView(binding.root)
setUpImageCompressionSeekBar()
if (isOffline()) {
binding.currentGroupSetting.visibility = View.GONE
} else {
groupsNames = mutableListOf() groupsNames = mutableListOf()
for (myGroup in myGroups) { for (myGroup in myGroups) {
groupsNames.add(net.getGroupName(myGroup.toInt())) groupsNames.add(net.getGroupName(myGroup.toInt()))
} }
binding = ActivitySettingsBinding.inflate(layoutInflater)
setContentView(binding.root)
setUpImageCompressionSeekBar()
fillUpCurrentGroupSpinner() fillUpCurrentGroupSpinner()
}
binding.saveButton.setOnClickListener { binding.saveButton.setOnClickListener {
sharedPreferences.edit().putString("currentGroup", net.getGroupId(binding.currentGroupSpinner.selectedItem.toString())).apply() 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() { private fun setUpImageCompressionSeekBar() {
val compressionFactor = sharedPreferences.getInt("imageCompression", 1) val compressionFactor = sharedPreferences.getInt("imageCompression", 1)
binding.imageCompressionFactorSeekBar.progress = compressionFactor - 1 binding.imageCompressionFactorSeekBar.progress = compressionFactor - 1
} }
private fun fillUpCurrentGroupSpinner() { private fun fillUpCurrentGroupSpinner() {
if (currentGroup == "offline") { if (currentGroup == "offline") {
binding.currentGroupSetting.visibility = View.GONE binding.currentGroupSetting.visibility = View.GONE
} }

View File

@ -117,6 +117,7 @@
<string name="no_internet_connection">No internet connection available. Please, connect to a network.</string> <string name="no_internet_connection">No internet connection available. Please, connect to a network.</string>
<string name="ok">Ok</string> <string name="ok">Ok</string>
<string name="logout">Log out</string> <string name="logout">Log out</string>
<string name="online_only_feature">This feature is online only</string>
<string-array name="languages"> <string-array name="languages">
<item>en-US</item> <item>en-US</item>
<item>ru-RU</item> <item>ru-RU</item>

View File

@ -115,6 +115,7 @@
<string name="no_internet_connection">No internet connection available. Please, connect to a network.</string> <string name="no_internet_connection">No internet connection available. Please, connect to a network.</string>
<string name="ok">Ok</string> <string name="ok">Ok</string>
<string name="logout">Log out</string> <string name="logout">Log out</string>
<string name="online_only_feature">This feature is online only</string>
<string-array name="languages"> <string-array name="languages">
<item>en-US</item> <item>en-US</item>
<item>ru-RU</item> <item>ru-RU</item>