-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from KWY0218/Retrofit/hangang
Retrofit/hangang
- Loading branch information
Showing
19 changed files
with
446 additions
and
29 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.example.myapplication | ||
|
||
interface GoActivity { | ||
fun goActivity(id:Int?) | ||
} |
38 changes: 38 additions & 0 deletions
38
app/src/main/java/com/example/myapplication/LectureDetailsActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
81
app/src/main/java/com/example/myapplication/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
app/src/main/java/com/example/myapplication/adapter/HanGangAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
69 changes: 67 additions & 2 deletions
69
app/src/main/java/com/example/myapplication/api/RetrofitInstance.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
app/src/main/java/com/example/myapplication/api/SimpleApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
Oops, something went wrong.