-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
retrofit
- Loading branch information
Showing
12 changed files
with
175 additions
and
41 deletions.
There are no files selected for viewing
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
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,7 +1,7 @@ | ||
package com.trolle.trolleapp.data | ||
|
||
data class Item( | ||
var name: String, | ||
var count: Int, | ||
var price: Int | ||
val login: String, | ||
val type: String, | ||
val id: Int | ||
) |
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,7 @@ | ||
package com.trolle.trolleapp.data | ||
|
||
import java.util.* | ||
|
||
data class ItemResponse ( | ||
val items : ArrayList<Item> | ||
) |
43 changes: 43 additions & 0 deletions
43
app/src/main/java/com/trolle/trolleapp/data/adapter/ItemAdapter.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,43 @@ | ||
package com.trolle.trolleapp.data.adapter | ||
|
||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import android.widget.Toast | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.trolle.trolleapp.data.Item | ||
import com.trolle.trolleapp.databinding.ItemRowListBinding | ||
import java.util.* | ||
|
||
class ItemAdapter: RecyclerView.Adapter<ItemAdapter.ItemViewHolder>() { | ||
private val list = ArrayList<Item>() | ||
|
||
fun setList(items: ArrayList<Item>){ | ||
list.clear() | ||
list.addAll(items) | ||
notifyDataSetChanged() | ||
} | ||
|
||
inner class ItemViewHolder(val binding: ItemRowListBinding) : RecyclerView.ViewHolder(binding.root){ | ||
fun bind(item: Item){ | ||
binding.tvItemName.text = item.login | ||
binding.tvItemCount.text = item.type | ||
binding.tvItemPrice.text = item.id.toString() | ||
|
||
} | ||
|
||
} | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder { | ||
val view = ItemRowListBinding.inflate(LayoutInflater.from(parent.context), parent, false) | ||
return ItemViewHolder(view) | ||
|
||
} | ||
|
||
override fun onBindViewHolder(holder: ItemViewHolder, position: Int) { | ||
holder.bind(list[position]) | ||
} | ||
|
||
override fun getItemCount(): Int = list.size | ||
|
||
|
||
} |
15 changes: 15 additions & 0 deletions
15
app/src/main/java/com/trolle/trolleapp/data/network/api/Api.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,15 @@ | ||
package com.trolle.trolleapp.data.network.api | ||
|
||
import com.trolle.trolleapp.data.ItemResponse | ||
import retrofit2.Call | ||
import retrofit2.http.GET | ||
import retrofit2.http.Headers | ||
import retrofit2.http.Query | ||
|
||
interface Api { | ||
@GET("search/users") | ||
@Headers("Authorization: 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", "User-Agent: request") | ||
fun getSearchItems( | ||
@Query("q") query: String? | ||
): Call<ItemResponse> | ||
} |
19 changes: 19 additions & 0 deletions
19
app/src/main/java/com/trolle/trolleapp/data/network/api/RetrofitClient.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,19 @@ | ||
package com.trolle.trolleapp.data.network.api | ||
|
||
import okhttp3.OkHttpClient | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.gson.GsonConverterFactory | ||
import retrofit2.create | ||
import java.util.concurrent.TimeUnit | ||
|
||
object RetrofitClient { | ||
private const val BASE_URL = "https://api.github.com/" | ||
|
||
val retrofit = Retrofit.Builder() | ||
.baseUrl(BASE_URL) | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.build() | ||
|
||
val apiInstance: Api = retrofit.create(Api::class.java) | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
app/src/main/java/com/trolle/trolleapp/data/viewmodel/MainViewModel.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,39 @@ | ||
package com.trolle.trolleapp.data.viewmodel | ||
|
||
import android.util.Log | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import com.trolle.trolleapp.data.Item | ||
import com.trolle.trolleapp.data.ItemResponse | ||
import com.trolle.trolleapp.data.network.api.RetrofitClient | ||
import retrofit2.Call | ||
import retrofit2.Callback | ||
import retrofit2.Response | ||
import java.util.* | ||
|
||
class MainViewModel: ViewModel() { | ||
|
||
val listItems = MutableLiveData<ArrayList<Item>>() | ||
|
||
fun setSearchItems(query: String){ | ||
RetrofitClient.apiInstance | ||
.getSearchItems(query) | ||
.enqueue(object: Callback<ItemResponse>{ | ||
override fun onResponse(call: Call<ItemResponse>, response: Response<ItemResponse>) { | ||
if (response.isSuccessful){ | ||
listItems.postValue(response.body()?.items) | ||
} | ||
} | ||
|
||
override fun onFailure(call: Call<ItemResponse>, t: Throwable) { | ||
Log.d("Failure", "ada error mainviewmodel") | ||
} | ||
|
||
}) | ||
} | ||
|
||
fun getSearchItems(): LiveData<ArrayList<Item>>{ | ||
return listItems | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...src/main/res/drawable/roundedshapebtn.xml → app/src/main/res/drawable/rounded_items.xml
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
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