Skip to content

Commit

Permalink
fix design comments
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo <[email protected]>
  • Loading branch information
Balcan committed Mar 4, 2024
1 parent d613fab commit f306286
Show file tree
Hide file tree
Showing 16 changed files with 255 additions and 79 deletions.
2 changes: 1 addition & 1 deletion app/src/dhis/java/org/dhis2/bindings/ContextExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fun Context.buildInfo(): String {
return if (BuildConfig.BUILD_TYPE == "release") {
"v${BuildConfig.VERSION_NAME}"
} else {
"v${BuildConfig.VERSION_NAME} : ${BuildConfig.BUILD_DATE} : ${BuildConfig.GIT_SHA} "
"v${BuildConfig.VERSION_NAME} : ${BuildConfig.GIT_SHA} "
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fun Context.buildInfo(): String {
return if (BuildConfig.BUILD_TYPE == "release") {
"v${BuildConfig.VERSION_NAME}"
} else {
"v${BuildConfig.VERSION_NAME} : ${BuildConfig.BUILD_DATE} : ${BuildConfig.GIT_SHA} "
"v${BuildConfig.VERSION_NAME} : ${BuildConfig.GIT_SHA} "
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fun Context.buildInfo(): String {
return if (BuildConfig.BUILD_TYPE == "release") {
"v${BuildConfig.VERSION_NAME}"
} else {
"v${BuildConfig.VERSION_NAME} : ${BuildConfig.BUILD_DATE} : ${BuildConfig.GIT_SHA} "
"v${BuildConfig.VERSION_NAME} : ${BuildConfig.GIT_SHA} "
}
}

Expand Down
9 changes: 8 additions & 1 deletion app/src/main/java/org/dhis2/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import android.content.Context;
import android.os.Looper;
import android.os.StrictMode;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -108,6 +109,12 @@ public class App extends MultiDexApplication implements Components, LifecycleObs
public void onCreate() {
super.onCreate();

StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectAll()
.penaltyLog()
.penaltyDeath()
.build()
);

ProcessLifecycleOwner.get().getLifecycle().addObserver(this);

Expand All @@ -116,7 +123,7 @@ public void onCreate() {
MapController.Companion.init(this);

setUpAppComponent();
if(BuildConfig.DEBUG){
if (BuildConfig.DEBUG) {
Timber.plant(new DebugTree());
}

Expand Down
29 changes: 20 additions & 9 deletions app/src/main/java/org/dhis2/usescases/login/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import android.webkit.URLUtil
import android.widget.ArrayAdapter
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.databinding.DataBindingUtil
import com.google.android.material.composethemeadapter.MdcTheme
import com.google.gson.Gson
Expand Down Expand Up @@ -120,9 +122,11 @@ class LoginActivity : ActivityGlobalAbstract(), LoginContracts.View {
}
}

override fun onDbImportFinished() {
override fun onDbImportFinished(isSuccess: Boolean) {
showLoginProgress(false)
blockLoginInfo()
if (isSuccess) {
blockLoginInfo()
}
}

companion object {
Expand Down Expand Up @@ -187,14 +191,19 @@ class LoginActivity : ActivityGlobalAbstract(), LoginContracts.View {
binding = DataBindingUtil.setContentView(this, R.layout.activity_login)

binding.topbar.setContent {
val displayMoreActions by presenter.displayMoreActions().observeAsState(true)
MdcTheme {
LoginTopBar(version = buildInfo(), onImportDatabase = {
showLoginProgress(false, getString(R.string.importing_database))
val intent = Intent()
intent.type = "*/*"
intent.action = Intent.ACTION_GET_CONTENT
filePickerLauncher.launch(intent)
})
LoginTopBar(
version = buildInfo(),
displayMoreActions = displayMoreActions,
onImportDatabase = {
showLoginProgress(false, getString(R.string.importing_database))
val intent = Intent()
intent.type = "*/*"
intent.action = Intent.ACTION_GET_CONTENT
filePickerLauncher.launch(intent)
},
)
}
}

Expand Down Expand Up @@ -540,6 +549,7 @@ class LoginActivity : ActivityGlobalAbstract(), LoginContracts.View {
binding.userNameEdit.isEnabled = true
binding.clearUrl.visibility = View.VISIBLE
binding.clearUserNameButton.visibility = View.VISIBLE
presenter.setDisplayMoreActions(true)
}

private fun blockLoginInfo() {
Expand All @@ -549,6 +559,7 @@ class LoginActivity : ActivityGlobalAbstract(), LoginContracts.View {
binding.userNameEdit.isEnabled = false
binding.clearUrl.visibility = View.GONE
binding.clearUserNameButton.visibility = View.GONE
presenter.setDisplayMoreActions(false)
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ class LoginContracts {
fun openAccountsActivity()
fun showNoConnectionDialog()
fun initLogin(): UserManager?
fun onDbImportFinished()
fun onDbImportFinished(isSuccess: Boolean)
}
}
16 changes: 13 additions & 3 deletions app/src/main/java/org/dhis2/usescases/login/LoginViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ class LoginViewModel(
private val _hasAccounts = MutableLiveData<Boolean>()
val hasAccounts: LiveData<Boolean> = _hasAccounts

private val _displayMoreActions = MutableLiveData<Boolean>(true)
val displayMoreActions: LiveData<Boolean> = _displayMoreActions

init {
this.userManager?.let {
disposable.add(
Expand Down Expand Up @@ -563,7 +566,7 @@ class LoginViewModel(
fun onImportDataBase(file: File) {
userManager?.let {
viewModelScope.launch {
val result = async {
val resultJob = async {
try {
val importedMetadata =
it.d2.maintenanceModule().databaseImportExport().importDatabase(file)
Expand All @@ -573,7 +576,9 @@ class LoginViewModel(
}
}

result.await().fold(
val result = resultJob.await()

result.fold(
onSuccess = {
setAccountInfo(it.serverUrl, it.username)
view.setUrl(it.serverUrl)
Expand All @@ -585,8 +590,13 @@ class LoginViewModel(
},
)

view.onDbImportFinished()
view.onDbImportFinished(result.isSuccess)
}
}
}

fun displayMoreActions() = displayMoreActions
fun setDisplayMoreActions(shouldDisplayMoreActions: Boolean) {
_displayMoreActions.postValue(shouldDisplayMoreActions)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.dhis2.usescases.login.accounts

import org.hisp.dhis.android.core.D2
import org.hisp.dhis.android.core.arch.db.access.DatabaseExportMetadata
import java.io.File

class AccountRepository(val d2: D2) {

Expand All @@ -9,4 +11,14 @@ class AccountRepository(val d2: D2) {
AccountModel(it.username(), it.serverUrl())
}
}

fun importDatabase(file: File): Result<DatabaseExportMetadata> {
return try {
val importedMetadata =
d2.maintenanceModule().databaseImportExport().importDatabase(file)
Result.success(importedMetadata)
} catch (e: Exception) {
Result.failure(e)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
package org.dhis2.usescases.login.accounts

import android.content.Intent
import android.os.Bundle
import android.webkit.MimeTypeMap
import androidx.activity.compose.setContent
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.runtime.livedata.observeAsState
import com.google.android.material.composethemeadapter.MdcTheme
import org.dhis2.bindings.app
import org.dhis2.commons.resources.ColorUtils
import org.dhis2.commons.resources.ResourceManager
import org.dhis2.usescases.general.ActivityGlobalAbstract
import org.dhis2.usescases.login.LoginActivity
import org.dhis2.usescases.login.accounts.ui.AccountsScreen
import timber.log.Timber
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import javax.inject.Inject

class AccountsActivity : ActivityGlobalAbstract() {
Expand All @@ -17,19 +27,56 @@ class AccountsActivity : ActivityGlobalAbstract() {
lateinit var viewModelFactory: AccountsViewModelFactory
private val viewModel: AccountsViewModel by viewModels { viewModelFactory }

private val filePickerLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
result.data?.data?.let { uri ->
val fileType = with(contentResolver) {
MimeTypeMap.getSingleton().getExtensionFromMimeType(getType(uri))
}
val file = File.createTempFile("importedDb", fileType)
val inputStream = contentResolver.openInputStream(uri)!!
try {
FileOutputStream(file, false).use { outputStream ->
var read: Int
val bytes = ByteArray(DEFAULT_BUFFER_SIZE)
while (inputStream.read(bytes).also { read = it } != -1) {
outputStream.write(bytes, 0, read)
}
}
} catch (e: IOException) {
Timber.e("Failed to load file: ", e.message.toString())
}
if (file.exists()) {
viewModel.onImportDataBase(
file,
{ navigateToLogin(it) },
{ displayMessage(ResourceManager(this, ColorUtils()).parseD2Error(it)) },
)
}
}
}

@ExperimentalMaterialApi
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

app().serverComponent()?.plus(AccountsModule())?.inject(this)

setContent {
val accounts = viewModel.accounts.observeAsState(listOf())
AccountsScreen(
accounts = accounts.value,
onAccountClicked = { navigateToLogin(it) },
onAddAccountClicked = { navigateToLogin() },
)
MdcTheme {
val accounts = viewModel.accounts.observeAsState(listOf())
AccountsScreen(
accounts = accounts.value,
onAccountClicked = { navigateToLogin(it) },
onAddAccountClicked = { navigateToLogin() },
onImportDatabase = {
val intent = Intent()
intent.type = "*/*"
intent.action = Intent.ACTION_GET_CONTENT
filePickerLauncher.launch(intent)
},
)
}
}
viewModel.getAccounts()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package org.dhis2.usescases.login.accounts
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import java.io.File

class AccountsViewModel(
val repository: AccountRepository,
Expand All @@ -15,4 +19,27 @@ class AccountsViewModel(
fun getAccounts() {
_accounts.value = repository.getLoggedInAccounts()
}

fun onImportDataBase(
file: File,
onSuccess: (AccountModel) -> Unit,
onFailure: (Throwable) -> Unit,
) {
viewModelScope.launch {
val resultJob = async { repository.importDatabase(file) }

val result = resultJob.await()

result.fold(
onSuccess = {
onSuccess(
AccountModel(it.username, it.serverUrl),
)
},
onFailure = {
onFailure(it)
},
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,39 @@ import androidx.compose.ui.viewinterop.AndroidView
import org.dhis2.R
import org.dhis2.bindings.buildInfo
import org.dhis2.usescases.login.accounts.AccountModel
import org.dhis2.usescases.login.ui.LoginTopBar

@ExperimentalMaterialApi
@Composable
fun AccountsScreen(
accounts: List<AccountModel>,
onAccountClicked: (AccountModel) -> Unit,
onAddAccountClicked: () -> Unit,
onImportDatabase: () -> Unit,
) {
MaterialTheme {
Column(
Modifier
.fillMaxWidth()
.background(colorResource(id = R.color.colorPrimary)),
) {
LoginHeader()
LoginTopBar(
version = LocalContext.current.buildInfo(),
onImportDatabase = onImportDatabase,
)

Column(
verticalArrangement = Arrangement.SpaceBetween,
modifier = Modifier
.fillMaxHeight()
.clip(RoundedCornerShape(topStart = 16.dp, topEnd = 16.dp))
.background(Color.White),
) {
LazyColumn(Modifier.weight(1f).padding(top = 16.dp)) {
LazyColumn(
Modifier
.weight(1f)
.padding(top = 16.dp),
) {
items(accounts) {
AccountItem(
Modifier.padding(horizontal = 16.dp, vertical = 8.dp),
Expand Down Expand Up @@ -134,6 +144,7 @@ fun AccountsPreview() {
accounts,
{},
{},
{},
)
}

Expand All @@ -150,5 +161,6 @@ fun FewAccountsPreview() {
accounts,
{},
{},
{},
)
}
Loading

0 comments on commit f306286

Please sign in to comment.