Skip to content

Commit

Permalink
Merge pull request #14 from kokoichi206/issue/3
Browse files Browse the repository at this point in the history
バグを修正
  • Loading branch information
kokoichi206 authored Dec 2, 2022
2 parents f755bd8 + d20d3cb commit 1894cd3
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,19 @@ class DetailFragment : Fragment(R.layout.fragment_detail) {

private val args: DetailFragmentArgs by navArgs()

private var binding: FragmentDetailBinding? = null

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

lastSearchDate?.let { date ->
Log.d("検索した日時", date.toString())
}

binding = FragmentDetailBinding.bind(view)

val item = args.item
val binding = FragmentDetailBinding.bind(view)

val context = requireContext()
binding.also {
val item = args.item
val context = requireContext()

binding?.let {
it.ownerIconView.load(item.ownerIconUrl)
it.nameView.text = item.name
it.languageView.text = item.language
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MainFragment : Fragment(R.layout.fragment_main) {

val binding = FragmentMainBinding.bind(view)

val viewModel = MainViewModel(requireContext())
val viewModel = MainViewModel(application = requireActivity().application)

val layoutManager = LinearLayoutManager(requireContext())
val dividerItemDecoration = DividerItemDecoration(requireContext(), layoutManager.orientation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
*/
package jp.co.yumemi.android.code_check

import android.app.Application
import android.content.Context
import android.os.Parcelable
import androidx.lifecycle.ViewModel
import android.util.Log
import androidx.lifecycle.AndroidViewModel
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.engine.android.*
Expand All @@ -23,58 +25,76 @@ import java.util.*
* MainFragment で使用。
*/
class MainViewModel(
val context: Context
) : ViewModel() {
application: Application
) : AndroidViewModel(application) {

companion object {
private val TAG = MainViewModel::class.java.simpleName
}

// 検索結果
fun searchResults(inputText: String): List<Repository> = runBlocking {
val client = HttpClient(Android)

return@runBlocking GlobalScope.async {
val response: HttpResponse = client.get("https://api.github.com/search/repositories") {
header("Accept", "application/vnd.github.v3+json")
parameter("q", inputText)
}
// API 呼び出し前のバリデーション
// query パラメータが空の場合 422 が返る
if (inputText.isBlank()) {
return@runBlocking emptyList<Repository>()
}

val jsonBody = JSONObject(response.receive<String>())
val client = HttpClient(Android)
return@runBlocking GlobalScope.async {

val jsonItems = jsonBody.optJSONArray("items")
val result = mutableListOf<Repository>()

val items = mutableListOf<Repository>()
try {
val response: HttpResponse = client.get("https://api.github.com/search/repositories") {
header("Accept", "application/vnd.github.v3+json")
parameter("q", inputText)
}
val jsonBody = JSONObject(response.receive<String>())
val jsonItems = jsonBody.optJSONArray("items")

jsonItems?.let {
for (i in 0 until it.length()) {
val jsonItem = it.optJSONObject(i)
jsonItems?.let {
for (i in 0 until it.length()) {
val jsonItem = it.optJSONObject(i)

val name = jsonItem.optString("full_name")
val ownerIconUrl = jsonItem.optJSONObject("owner")?.optString("avatar_url") ?: "empty_url"
val language = jsonItem.optString("language")
val stargazersCount = jsonItem.optLong("stargazers_count")
val watchersCount = jsonItem.optLong("watchers_count")
val forksCount = jsonItem.optLong("forks_count")
val openIssuesCount = jsonItem.optLong("open_issues_count")
val name = jsonItem.optString("full_name")
val ownerIconUrl = jsonItem.optJSONObject("owner")?.optString("avatar_url") ?: "empty_url"
val language = jsonItem.optString("language")
val stargazersCount = jsonItem.optLong("stargazers_count")
val watchersCount = jsonItem.optLong("watchers_count")
val forksCount = jsonItem.optLong("forks_count")
val openIssuesCount = jsonItem.optLong("open_issues_count")

items.add(
Repository(
name = name,
ownerIconUrl = ownerIconUrl,
language = context.getString(R.string.written_language, language),
stargazersCount = stargazersCount,
watchersCount = watchersCount,
forksCount = forksCount,
openIssuesCount = openIssuesCount
result.add(
Repository(
name = name,
ownerIconUrl = ownerIconUrl,
language = context.getString(R.string.written_language, language),
stargazersCount = stargazersCount,
watchersCount = watchersCount,
forksCount = forksCount,
openIssuesCount = openIssuesCount
)
)
)
}
}
}
lastSearchDate = Date()

lastSearchDate = Date()
} catch (e: Exception) {
e.localizedMessage?.let {
Log.e(TAG, it)
}
}

return@async items.toList()
return@async result.toList()
}.await()
}
}

val AndroidViewModel.context: Context
get() = getApplication()

@Parcelize
data class Repository(
val name: String,
Expand Down
193 changes: 99 additions & 94 deletions app/src/main/res/layout/fragment_detail.xml
Original file line number Diff line number Diff line change
@@ -1,108 +1,113 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/ownerIconView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="16dp"
android:contentDescription="@null"
android:src="@drawable/jetbrains"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_max="240dp" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/nameView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="JetBrains/kotlin"
android:textColor="@color/black"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/ownerIconView" />
<ImageView
android:id="@+id/ownerIconView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="16dp"
android:contentDescription="@null"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_max="240dp"
tools:src="@drawable/jetbrains" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/centerGuid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
<TextView
android:id="@+id/nameView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:textColor="@color/black"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/ownerIconView"
tools:text="JetBrains/kotlin" />

<TextView
android:id="@+id/languageView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="8dp"
android:text="Written in Kotlin"
android:textColor="@color/black"
android:textSize="14sp"
app:layout_constraintTop_toBottomOf="@id/nameView"
tools:ignore="MissingConstraints" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/centerGuid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />

<TextView
android:id="@+id/starsView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="8dp"
android:text="38530 stars"
android:textAlignment="textEnd"
android:textColor="@color/black"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@id/centerGuid"
app:layout_constraintTop_toBottomOf="@id/nameView" />
<TextView
android:id="@+id/languageView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="8dp"
android:textColor="@color/black"
android:textSize="14sp"
app:layout_constraintTop_toBottomOf="@id/nameView"
tools:ignore="MissingConstraints"
tools:text="Written in Kotlin" />

<TextView
android:id="@+id/watchersView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="8dp"
android:text="38530 watchers"
android:textAlignment="textEnd"
android:textColor="@color/black"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@id/centerGuid"
app:layout_constraintTop_toBottomOf="@id/starsView" />
<TextView
android:id="@+id/starsView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="8dp"
android:textAlignment="textEnd"
android:textColor="@color/black"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@id/centerGuid"
app:layout_constraintTop_toBottomOf="@id/nameView"
tools:text="38530 stars" />

<TextView
android:id="@+id/forksView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="8dp"
android:text="4675 forks"
android:textAlignment="textEnd"
android:textColor="@color/black"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="@id/centerGuid"
app:layout_constraintTop_toBottomOf="@id/watchersView" />
<TextView
android:id="@+id/watchersView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="8dp"
android:textAlignment="textEnd"
android:textColor="@color/black"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@id/centerGuid"
app:layout_constraintTop_toBottomOf="@id/starsView"
tools:text="38530 watchers" />

<TextView
android:id="@+id/openIssuesView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="8dp"
android:text="131 open issues"
android:textAlignment="textEnd"
android:textColor="@color/black"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@id/centerGuid"
app:layout_constraintTop_toBottomOf="@id/forksView" />
<TextView
android:id="@+id/forksView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="8dp"
android:textAlignment="textEnd"
android:textColor="@color/black"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="@id/centerGuid"
app:layout_constraintTop_toBottomOf="@id/watchersView"
tools:text="4675 forks" />

</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="@+id/openIssuesView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="8dp"
android:textAlignment="textEnd"
android:textColor="@color/black"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@id/centerGuid"
app:layout_constraintTop_toBottomOf="@id/forksView"
tools:text="131 open issues" />

</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

0 comments on commit 1894cd3

Please sign in to comment.