Skip to content

Commit

Permalink
Merge pull request #3 from KWY0218/Retrofit/hangang
Browse files Browse the repository at this point in the history
Retrofit/hangang
  • Loading branch information
KWY0218 authored Nov 12, 2021
2 parents f24cd4c + bf5f248 commit 7a5ca94
Show file tree
Hide file tree
Showing 19 changed files with 446 additions and 29 deletions.
4 changes: 3 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses-permission android:name="android.permission.INTERNET"/>

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication">
<activity
android:name=".LectureDetailsActivity"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/example/myapplication/GoActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.myapplication

interface GoActivity {
fun goActivity(id:Int?)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.example.myapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.example.myapplication.databinding.ActivityLectureDetailsBinding
import com.example.myapplication.repository.Repository

class LectureDetailsActivity : AppCompatActivity() {
lateinit var binding: ActivityLectureDetailsBinding
private lateinit var viewModel: MainViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val id = intent.getIntExtra("lecture_id", 1)
binding = DataBindingUtil.setContentView(this, R.layout.activity_lecture_details)
viewModel = ViewModelProvider(this, MainViewModelFactory(Repository()))
.get(MainViewModel::class.java)

with(binding) {
with(viewModel) {
getDetail(id)
lectureDetails.observe(this@LectureDetailsActivity, Observer { response ->
if (response.isSuccessful) {
Log.d("Response1","details name: " + response.body()?.lectureDetailsName)
lectureDetailsName.text = response.body()?.lectureDetailsName
} else {
Log.d("Response1", "status: " + response.errorBody().toString())
Log.d("Response1", "status: " + response.code().toString())
}
})
}

}
}
}
81 changes: 63 additions & 18 deletions app/src/main/java/com/example/myapplication/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,34 +1,79 @@
package com.example.myapplication

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.myapplication.adapter.HanGangAdapter
import com.example.myapplication.databinding.ActivityMainBinding
import com.example.myapplication.repository.Repository

class MainActivity : AppCompatActivity() {

class MainActivity: AppCompatActivity(),GoActivity {
private lateinit var binding: ActivityMainBinding
private lateinit var viewModel: MainViewModel
private lateinit var hangangAdapter:HanGangAdapter

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val repository = Repository()
val viewModelFactory = MainViewModelFactory(repository)
viewModel = ViewModelProvider(this,viewModelFactory).get(MainViewModel::class.java)
viewModel.getStatusCode()

viewModel.myResponse.observe(this, Observer { response->
if(response.isSuccessful){
Log.d("Response1","status: "+response.body()?.statusCode.toString())
Log.d("Response1","status: "+response.body()?.body?.bodyInForce.toString())
Log.d("Response1","status: "+response.body()?.body?.bodyInVersion.toString())
}else{
Log.d("Response1","status: "+response.errorBody().toString())
Log.d("Response1","status: "+response.code().toString())
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
hangangAdapter = HanGangAdapter(this)
viewModel = ViewModelProvider(this, MainViewModelFactory(Repository()))
.get(MainViewModel::class.java)

with(binding) {

with(recyclerview) {
adapter = hangangAdapter
layoutManager = LinearLayoutManager(this@MainActivity)
}

with(viewModel) {
getHangangList(10, Integer.parseInt(page.text.toString()))
lectureList.observe(this@MainActivity, Observer { response ->
if (response.isSuccessful) {
response.body()?.resultList.let {
hangangAdapter.submitList(it)
}

response.body()?.resultList?.forEach {
Log.d("Response1", "id: " + it.id)
Log.d("Response1", "professor: " + it.professor)
Log.d("Response1", "subjectName: " + it.subjectName)
Log.d("Response1", "-----------------------------------")
}

} else {
Log.d("Response1", "status: " + response.errorBody().toString())
Log.d("Response1", "status: " + response.code().toString())
}

})
}
})


rightBtn.setOnClickListener {
if (Integer.parseInt(page.text.toString()) + 1 < 102) {
page.setText((Integer.parseInt(page.text.toString()) + 1).toString())
viewModel.getHangangList(10, Integer.parseInt(page.text.toString()))
}
}

leftBtn.setOnClickListener {
if (Integer.parseInt(page.text.toString()) - 1 > 0) {
page.setText((Integer.parseInt(page.text.toString()) - 1).toString())
viewModel.getHangangList(10, Integer.parseInt(page.text.toString()))
}
}
}
}

override fun goActivity(id: Int?) {
val intent = Intent(this,LectureDetailsActivity::class.java)
intent.putExtra("lecture_id",id)
startActivity(intent)
}
}
20 changes: 19 additions & 1 deletion app/src/main/java/com/example/myapplication/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,39 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import com.example.myapplication.model.Body
import com.example.myapplication.api.RetrofitInstance
import com.example.myapplication.model.LectureDetails
import com.example.myapplication.model.LectureResult
import com.example.myapplication.model.StatusCode
import com.example.myapplication.repository.Repository
import kotlinx.coroutines.launch
import retrofit2.Response

class MainViewModel (private val repository: Repository): ViewModel(){
val myResponse:MutableLiveData<Response<StatusCode>> = MutableLiveData()
val lectureList:MutableLiveData<Response<LectureResult>> = MutableLiveData()
val lectureDetails:MutableLiveData<Response<LectureDetails>> = MutableLiveData()

fun getStatusCode(){
viewModelScope.launch {
val response = repository.getStatusCode()
myResponse.value = response
}
}

fun getHangangList(limit:Int,page:Int){
viewModelScope.launch {
val response = repository.getHangangList(limit,page)
lectureList.value = response
}
}

fun getDetail(id:Int){
viewModelScope.launch {
val response = repository.getDetail(id)
lectureDetails.value = response
}
}
}

class MainViewModelFactory(private val repository: Repository):ViewModelProvider.Factory{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.example.myapplication.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.myapplication.GoActivity
import com.example.myapplication.R
import com.example.myapplication.model.Lecture

class HanGangAdapter(val goActivity: GoActivity) : ListAdapter<Lecture, HanGangAdapter.LectureHolder>(LectureComparator()) {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LectureHolder {
return LectureHolder.create(parent)
}

override fun onBindViewHolder(holder: LectureHolder, position: Int) {
val current = getItem(position)
holder.bind(current.subjectName.toString(),current.professor.toString())
holder.root.setOnClickListener{
goActivity.goActivity(current.id)
}
}

class LectureHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val lectureName: TextView = itemView.findViewById(R.id.lecture_item_name)
private val professorName: TextView = itemView.findViewById(R.id.professor_item)
val root:ConstraintLayout = itemView.findViewById(R.id.root)
fun bind(lecture: String?, professor:String?) {
lectureName.text = lecture
professorName.text = professor
}

companion object {
fun create(parent: ViewGroup): LectureHolder {
val view: View = LayoutInflater.from(parent.context)
.inflate(R.layout.lecture_item, parent, false)
return LectureHolder(view)
}
}
}

class LectureComparator : DiffUtil.ItemCallback<Lecture>() {
override fun areItemsTheSame(oldItem: Lecture, newItem: Lecture): Boolean {
return oldItem === newItem
}

override fun areContentsTheSame(oldItem: Lecture, newItem: Lecture): Boolean {
return oldItem.id == newItem.id
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,84 @@
package com.example.myapplication.api

import com.example.myapplication.util.Constants.Companion.BASE_URL
import com.example.myapplication.util.Constants.Companion.HANGANG_DETAILS_URL
import com.example.myapplication.util.Constants.Companion.HANGANG_URL
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.security.SecureRandom
import javax.net.ssl.SSLContext
import javax.net.ssl.TrustManager
import javax.net.ssl.X509TrustManager

object RetrofitInstance {

private val retrofit by lazy{
private val retrofit by lazy {
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
}

val api:SimpleApi by lazy {
private val hangangInstance by lazy {
Retrofit.Builder()
.baseUrl(HANGANG_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(unsafeOkHttpClient().build())
.build()
}

private val hangangDetailsInstance by lazy {
Retrofit.Builder()
.baseUrl(HANGANG_DETAILS_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(unsafeOkHttpClient().build())
.build()
}

val api: SimpleApi by lazy {
retrofit.create(SimpleApi::class.java)
}

val hangangApi by lazy{
hangangInstance.create(SimpleApi::class.java)
}

val hangangDetailsApi by lazy{
hangangDetailsInstance.create(SimpleApi::class.java)
}

private fun unsafeOkHttpClient(): OkHttpClient.Builder {
val trustAllCerts = arrayOf<TrustManager>(object : X509TrustManager {
override fun checkClientTrusted(
chain: Array<out java.security.cert.X509Certificate>?,
authType: String?
) {

}

override fun checkServerTrusted(
chain: Array<out java.security.cert.X509Certificate>?,
authType: String?
) {

}

override fun getAcceptedIssuers(): Array<out java.security.cert.X509Certificate>? {
return arrayOf()
}
})

val sslContext = SSLContext.getInstance("SSL")
sslContext.init(null, trustAllCerts, SecureRandom())

val sslSocketFactory = sslContext.socketFactory

val builder = OkHttpClient.Builder()
builder.sslSocketFactory(sslSocketFactory, trustAllCerts[0] as X509TrustManager)
builder.hostnameVerifier { hostname, session -> true }

return builder

}
}
14 changes: 13 additions & 1 deletion app/src/main/java/com/example/myapplication/api/SimpleApi.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
package com.example.myapplication.api

import com.example.myapplication.model.Body
import com.example.myapplication.model.LectureDetails
import com.example.myapplication.model.LectureResult
import com.example.myapplication.model.StatusCode
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Path
import retrofit2.http.Query

interface SimpleApi {

@GET("prod/version")
suspend fun getStatusCode(): Response<StatusCode>

@GET("lectures/")
suspend fun getHangangList(
@Query("limit")limit:Int,
@Query("page")page:Int
): Response<LectureResult>

@GET("lectures/{id}")
suspend fun getDetail(
@Path("id")id:Int
): Response<LectureDetails>
}
Loading

0 comments on commit 7a5ca94

Please sign in to comment.