Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: [ANDROAPP-6333] update retrofix call to ktor #3734

Merged
merged 6 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ dependencies {
implementation(libs.github.fancyshowcase)
implementation(libs.lottie)
implementation(libs.dagger.hilt.android)
implementation(libs.network.gsonconverter)
implementation(libs.network.okhttp)
implementation(libs.dates.jodatime)
implementation(libs.analytics.matomo)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package org.dhis2.usescases.searchte

import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.text.capitalize
import androidx.compose.ui.text.intl.Locale
Expand Down Expand Up @@ -131,6 +129,7 @@ class SearchTETest : BaseTest() {
}
}

@Ignore("Test needs to be fixed in ANDROAPP-6340")
@Test
fun shouldCheckDisplayInList() {
val displayInListData = createDisplayListFields()
Expand Down
142 changes: 67 additions & 75 deletions app/src/main/java/org/dhis2/usescases/jira/JiraFragment.kt
Original file line number Diff line number Diff line change
@@ -1,95 +1,87 @@
package org.dhis2.usescases.jira

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.provider.Browser
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DividerItemDecoration
import org.dhis2.data.jira.ClickedIssueData
import org.dhis2.data.jira.JiraIssuesResult
import org.dhis2.databinding.FragmentJiraBinding
import org.dhis2.usescases.general.FragmentGlobalAbstract
import org.dhis2.usescases.main.MainActivity
import org.dhis2.utils.NetworkUtils
import javax.inject.Inject

class JiraFragment : FragmentGlobalAbstract() {
@Inject
lateinit var jiraViewModelFactory: JiraViewModelFactory
private val jiraModel: JiraViewModel by viewModels {
jiraViewModelFactory
}
private val jiraIssueAdapter by lazy {
JiraIssueAdapter { jiraModel.onJiraIssueClick(it) }
}
// @Inject
// lateinit var jiraViewModelFactory: JiraViewModelFactory
// private val jiraModel: JiraViewModel by viewModels {
// jiraViewModelFactory
// }
// private val resourceManager: ResourceManager = ResourceManager(requireContext(), ColorUtils())
// private val preferenceProvider: PreferenceProvider = PreferenceProviderImpl(requireContext())
// private val jiraRepository: JiraRepository = JiraRepository(preferenceProvider)
// private val jiraModel: JiraViewModel = JiraViewModel(jiraRepository, resourceManager)
// private val jiraIssueAdapter by lazy {
// JiraIssueAdapter { jiraModel.onJiraIssueClick(it) }
// }

override fun onAttach(context: Context) {
super.onAttach(context)
if (context is MainActivity) {
context.mainComponent.plus(JiraModule()).inject(this)
}
}
// override fun onAttach(context: Context) {
// super.onAttach(context)
// if (context is MainActivity) {
// context.mainComponent.plus(JiraModule()).inject(this)
// }
// }

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View? {
return FragmentJiraBinding.inflate(inflater, container, false).apply {
jiraViewModel = jiraModel
rememberCheck.setOnCheckedChangeListener { _, isChecked ->
jiraModel.onCheckedChanged(isChecked)
}
sendReportButton.isEnabled = NetworkUtils.isOnline(context)
issueRecycler.apply {
adapter = jiraIssueAdapter
addItemDecoration(
DividerItemDecoration(
context,
DividerItemDecoration.VERTICAL,
),
)
}
jiraModel.apply {
init()
issueListResponse.observe(viewLifecycleOwner, Observer { handleListResponse(it) })
issueMessage.observe(viewLifecycleOwner, Observer { handleMessage(it) })
clickedIssueData.observe(
viewLifecycleOwner,
Observer { openJiraTicketInBrowser(it) },
)
}
}.root
// return FragmentJiraBinding.inflate(inflater, container, false).apply {
// jiraViewModel = jiraModel
// rememberCheck.setOnCheckedChangeListener { _, isChecked ->
// jiraModel.onCheckedChanged(isChecked)
// }
// sendReportButton.isEnabled = NetworkUtils.isOnline(context)
// issueRecycler.apply {
// adapter = jiraIssueAdapter
// addItemDecoration(
// DividerItemDecoration(
// context,
// DividerItemDecoration.VERTICAL,
// ),
// )
// }
// jiraModel.apply {
// init()
// issueListResponse.observe(viewLifecycleOwner, Observer { handleListResponse(it) })
// issueMessage.observe(viewLifecycleOwner, Observer { handleMessage(it) })
// clickedIssueData.observe(
// viewLifecycleOwner,
// Observer { openJiraTicketInBrowser(it) },
// )
// }
// }.root
return null
}

private fun handleListResponse(result: JiraIssuesResult) {
if (result.isSuccess()) {
jiraIssueAdapter.submitList(result.issues)
} else {
displayMessage(result.errorMessage)
}
}
// private fun handleListResponse(result: JiraIssuesResult) {
// if (result.isSuccess()) {
// jiraIssueAdapter.submitList(result.issues)
// } else {
// displayMessage(result.errorMessage)
// }
// }

private fun handleMessage(message: String) {
displayMessage(message)
}

private fun openJiraTicketInBrowser(clickedIssueData: ClickedIssueData) {
val browserIntent = Intent(
Intent.ACTION_VIEW,
Uri.parse(clickedIssueData.uriString),
).apply {
val bundle = Bundle().apply {
putString(clickedIssueData.authHeader(), clickedIssueData.basicAuth())
}
putExtra(Browser.EXTRA_HEADERS, bundle)
}
startActivity(browserIntent)
}
// private fun handleMessage(message: String) {
// displayMessage(message)
// }
//
// private fun openJiraTicketInBrowser(clickedIssueData: ClickedIssueData) {
// val browserIntent = Intent(
// Intent.ACTION_VIEW,
// Uri.parse(clickedIssueData.uriString),
// ).apply {
// val bundle = Bundle().apply {
// putString(clickedIssueData.authHeader(), clickedIssueData.basicAuth())
// }
// putExtra(Browser.EXTRA_HEADERS, bundle)
// }
// startActivity(browserIntent)
// }
}
50 changes: 25 additions & 25 deletions app/src/main/java/org/dhis2/usescases/jira/JiraIssueService.kt
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package org.dhis2.usescases.jira

import io.reactivex.Single
import okhttp3.RequestBody
import okhttp3.ResponseBody
import org.dhis2.data.jira.JiraIssueListResponse
import retrofit2.http.Body
import retrofit2.http.Header
import retrofit2.http.POST

interface JiraIssueService {
@POST("rest/api/2/issue")
fun createIssue(
@Header("Authorization")
auth: String?,
@Body issueRequest: RequestBody,
): Single<ResponseBody>

@POST("rest/api/2/search")
fun getJiraIssues(
@Header("Authorization")
auth: String?,
@Body issueRequest: RequestBody,
): Single<JiraIssueListResponse>
}
// package org.dhis2.usescases.jira
//
// import io.reactivex.Single
// import okhttp3.RequestBody
// import okhttp3.ResponseBody
// import org.dhis2.data.jira.JiraIssueListResponse
// import retrofit2.http.Body
// import retrofit2.http.Header
// import retrofit2.http.POST
//
// interface JiraIssueService {
// @POST("rest/api/2/issue")
// fun createIssue(
// @Header("Authorization")
// auth: String?,
// @Body issueRequest: RequestBody,
// ): Single<ResponseBody>
//
// @POST("rest/api/2/search")
// fun getJiraIssues(
// @Header("Authorization")
// auth: String?,
// @Body issueRequest: RequestBody,
// ): Single<JiraIssueListResponse>
// }
10 changes: 7 additions & 3 deletions app/src/main/java/org/dhis2/usescases/jira/JiraRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.reactivex.Single
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.ResponseBody
import okhttp3.ResponseBody.Companion.toResponseBody
import org.dhis2.commons.Constants
import org.dhis2.commons.prefs.PreferenceProvider
import org.dhis2.data.jira.IssueRequest
Expand All @@ -15,7 +16,7 @@ import org.dhis2.data.jira.toBasicAuth
import org.dhis2.data.jira.toJiraJql

class JiraRepository(
private val jiraApi: JiraIssueService,
// private val jiraApi: JiraIssueService,
private val prefs: PreferenceProvider,
) {
private var session: String? = prefs.getString(Constants.JIRA_AUTH, null)
Expand All @@ -35,15 +36,18 @@ class JiraRepository(

val requestBody = Gson().toJson(request)
.toRequestBody(MEDIA_TYPE_APPLICATION_JSON.toMediaTypeOrNull())
return jiraApi.getJiraIssues(basic, requestBody)
return Single.just(JiraIssueListResponse(0, 0, emptyList()))
// return jiraApi.getJiraIssues(basic, requestBody)
}

//
fun sendJiraIssue(summary: String, description: String): Single<ResponseBody> {
val basic = session?.toBasicAuth()
val issueRequest = IssueRequest(summary, description)
val requestBody = Gson().toJson(issueRequest)
.toRequestBody(MEDIA_TYPE_APPLICATION_JSON.toMediaTypeOrNull())
return jiraApi.createIssue(basic, requestBody)
// return jiraApi.createIssue(basic, requestBody)
return Single.just("".toResponseBody())
}

fun saveCredentials() {
Expand Down
43 changes: 20 additions & 23 deletions app/src/main/java/org/dhis2/usescases/jira/JiraViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import io.reactivex.disposables.CompositeDisposable
import org.dhis2.commons.resources.ResourceManager
import org.dhis2.commons.schedulers.SchedulerProvider
import org.dhis2.data.jira.ClickedIssueData
import org.dhis2.data.jira.JiraIssuesResult
import org.dhis2.data.jira.toJiraIssueUri
import timber.log.Timber

const val MEDIA_TYPE_APPLICATION_JSON = "application/json"
const val BASIC_AUTH_CODE = "%s:%s"
Expand All @@ -18,7 +16,6 @@ const val MAX_RESULTS = 20
class JiraViewModel(
private val jiraRepository: JiraRepository,
private val resources: ResourceManager,
private val schedulerProvider: SchedulerProvider,
) : ViewModel() {

private var disposable = CompositeDisposable()
Expand Down Expand Up @@ -57,29 +54,29 @@ class JiraViewModel(
}

fun sendIssue() {
disposable.add(
jiraRepository.sendJiraIssue(summary, description)
.subscribeOn(schedulerProvider.io())
.observeOn(schedulerProvider.ui())
.subscribe(
{ handleSendResponse(true) },
{ handleThrowableResponse(it) },
),
)
// disposable.add(
// jiraRepository.sendJiraIssue(summary, description)
// .subscribeOn(schedulerProvider.io())
// .observeOn(schedulerProvider.ui())
// .subscribe(
// { handleSendResponse(true) },
// { handleThrowableResponse(it) },
// ),
// )
}

fun getJiraTickets() {
disposable.add(
jiraRepository.getJiraIssues(userName)
.map { JiraIssuesResult(it.issues) }
.onErrorReturn { JiraIssuesResult(errorMessage = it.message) }
.subscribeOn(schedulerProvider.io())
.observeOn(schedulerProvider.ui())
.subscribe(
{ handleResponse(it) },
{ t -> Timber.e(t) },
),
)
// disposable.add(
// jiraRepository.getJiraIssues(userName)
// .map { JiraIssuesResult(it.issues) }
// .onErrorReturn { JiraIssuesResult(errorMessage = it.message) }
// .subscribeOn(schedulerProvider.io())
// .observeOn(schedulerProvider.ui())
// .subscribe(
// { handleResponse(it) },
// { t -> Timber.e(t) },
// ),
// )
}

fun onSummaryChanged(s: CharSequence, start: Int, before: Int, count: Int) {
Expand Down
Loading
Loading