1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
/**
* TrackerMap
* Copyright (C) 2021-2022 Iván Ávalos <avalos@disroot.org>, Henoch Ojeda <imhenoch@protonmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package mx.trackermap.TrackerMap.android.session
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceManager
import com.zhuinden.liveevent.observe
import kotlinx.coroutines.DelicateCoroutinesApi
import mx.trackermap.TrackerMap.android.R
import mx.trackermap.TrackerMap.android.databinding.UserInformationActivityBinding
import mx.trackermap.TrackerMap.controllers.SessionController
import org.koin.androidx.viewmodel.ext.android.viewModel
import kotlin.time.ExperimentalTime
@DelicateCoroutinesApi
@ExperimentalTime
class UserInformationActivity : AppCompatActivity() {
private var _binding: UserInformationActivityBinding? = null
private val binding get() = _binding!!
private val userInformationViewModel: UserInformationViewModel by viewModel()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
_binding = UserInformationActivityBinding.inflate(layoutInflater)
setContentView(binding.root)
initialize()
}
private fun initialize() {
setupObservers()
setupEvents()
userInformationViewModel.fetchUserInfo()
// Show version number
val pm = applicationContext.packageManager
val pkgName = applicationContext.packageName
try {
val pkgInfo = pm.getPackageInfo(pkgName, 0)
binding.appVersion.text = pkgInfo.versionName
} catch (e: PackageManager.NameNotFoundException) {
e.printStackTrace()
}
}
private fun setupObservers() {
userInformationViewModel.loginState.observe(this) { userInformation ->
when (userInformation) {
SessionController.LoginState.Loading -> setLoading(true)
SessionController.LoginState.Failure -> failure()
SessionController.LoginState.SignOut -> signOut()
else -> {}
}
}
userInformationViewModel.user.observe(this) { user ->
setLoading(false)
binding.apply {
usernameInfo.text = user.name ?: ""
emailInfo.text = user.email ?: ""
idInfo.text = "${user.id ?: "--"}"
adminInfo.text = "${user.administrator}"
serverInfo.text = "${
PreferenceManager
.getDefaultSharedPreferences(this@UserInformationActivity)
.getString(LoginFragment.PREFERENCE_SERVER_URL, getString(R.string.default_server_url))
}"
user.deviceLimit?.let {
deviceLimitInfo.text = "${user.deviceLimit ?: "--"}"
if (it > 0) {
deviceLimitInfo.visibility = View.VISIBLE
deviceLimitInfo.visibility = View.VISIBLE
}
}
}
}
}
private fun setupEvents() {
binding.closeButton.setOnClickListener { onBackPressed() }
binding.signoutButton.setOnClickListener {
userInformationViewModel.signOut(PreferenceManager
.getDefaultSharedPreferences(this)
.getString(LoginFragment.PREFERENCE_TOKEN, null))
}
binding.sourceCodeButton.setOnClickListener {
openURL(getString(R.string.app_source_code_url))
}
binding.websiteButton.setOnClickListener {
openURL(getString(R.string.app_website_url))
}
}
private fun setLoading(isLoading: Boolean) {
binding.infoLoading.root.visibility = if (isLoading) View.VISIBLE else View.GONE
binding.userInfoCard.visibility = if (isLoading) View.GONE else View.VISIBLE
}
private fun failure() {
setLoading(false)
Toast.makeText(this, "Something went wrong...", Toast.LENGTH_LONG).show()
}
private fun openURL(url: String) {
val uri = Uri.parse(url)
val intent = Intent(Intent.ACTION_VIEW, uri)
startActivity(intent)
}
private fun signOut() {
val intent = Intent(applicationContext, LoginActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(intent)
finish()
}
}
|