-
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.
시작
- Loading branch information
Showing
14 changed files
with
325 additions
and
40 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
package com.example.myapplication | ||
|
||
interface goActivity { | ||
interface GoActivity { | ||
fun goActivity(id:Int?) | ||
} |
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("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
54 changes: 54 additions & 0 deletions
54
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 |
---|---|---|
@@ -1,2 +1,56 @@ | ||
package com.example.myapplication.adapter | ||
|
||
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 | ||
} | ||
} | ||
} |
56 changes: 54 additions & 2 deletions
56
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,71 @@ | ||
package com.example.myapplication.api | ||
|
||
import com.example.myapplication.util.Constants.Companion.BASE_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() | ||
} | ||
|
||
val api: SimpleApi by lazy { | ||
retrofit.create(SimpleApi::class.java) | ||
} | ||
|
||
val hangangApi by lazy{ | ||
hangangInstance.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 | ||
|
||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
app/src/main/java/com/example/myapplication/model/Lecture.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,2 +1,17 @@ | ||
package com.example.myapplication.model | ||
|
||
import com.google.gson.annotations.SerializedName | ||
|
||
data class LectureResult( | ||
@SerializedName("result") | ||
var resultList:List<Lecture>? = null | ||
) | ||
|
||
class Lecture{ | ||
@SerializedName("id") | ||
var id:Int? = null | ||
@SerializedName("name") | ||
var subjectName:String? = null | ||
@SerializedName("professor") | ||
var professor:String? = null | ||
} |
6 changes: 5 additions & 1 deletion
6
app/src/main/java/com/example/myapplication/repository/Repository.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,12 +1,16 @@ | ||
package com.example.myapplication.repository | ||
|
||
import com.example.myapplication.api.RetrofitInstance | ||
import com.example.myapplication.model.Body | ||
import com.example.myapplication.model.LectureResult | ||
import com.example.myapplication.model.StatusCode | ||
import retrofit2.Response | ||
|
||
class Repository { | ||
suspend fun getStatusCode(): Response<StatusCode> { | ||
return RetrofitInstance.api.getStatusCode() | ||
} | ||
|
||
suspend fun getHangangList(limit:Int,page:Int): Response<LectureResult>{ | ||
return RetrofitInstance.hangangApi.getHangangList(limit,page) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,9 +1,28 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
<layout | ||
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" | ||
tools:context=".LectureDetailsActivity"> | ||
xmlns:tools="http://schemas.android.com/tools"> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> | ||
<androidx.constraintlayout.widget.ConstraintLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".LectureDetailsActivity"> | ||
|
||
<TextView | ||
android:id="@+id/lecture_details_name" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="제목" | ||
android:textSize="20sp" | ||
android:textColor="@color/black" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintHorizontal_bias="0.498" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" | ||
app:layout_constraintVertical_bias="0.123" /> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> | ||
|
||
</layout> |
Oops, something went wrong.