-
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
28 changed files
with
422 additions
and
50 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
27 changes: 0 additions & 27 deletions
27
app/src/main/java/com/muhammedesadcomert/shopping/HomeFragment.kt
This file was deleted.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
app/src/main/java/com/muhammedesadcomert/shopping/ShoppingApp.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,7 @@ | ||
package com.muhammedesadcomert.shopping | ||
|
||
import android.app.Application | ||
import dagger.hilt.android.HiltAndroidApp | ||
|
||
@HiltAndroidApp | ||
class ShoppingApp : Application() |
29 changes: 29 additions & 0 deletions
29
app/src/main/java/com/muhammedesadcomert/shopping/common/base/BaseRepository.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,29 @@ | ||
package com.muhammedesadcomert.shopping.common.base | ||
|
||
import com.muhammedesadcomert.shopping.common.util.Resource | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import retrofit2.HttpException | ||
import retrofit2.Response | ||
import java.io.IOException | ||
|
||
abstract class BaseRepository { | ||
suspend fun <T> safeApiCall(apiCall: suspend () -> Response<T>): Resource<T> { | ||
return withContext(Dispatchers.IO) { | ||
try { | ||
val response: Response<T> = apiCall() | ||
if (response.isSuccessful) { | ||
Resource.Success(response.body()) | ||
} else { | ||
Resource.Failure(response.message().orEmpty()) | ||
} | ||
} catch (e: HttpException) { | ||
Resource.Failure(e.message ?: "Something went wrong!") | ||
} catch (e: IOException) { | ||
Resource.Failure("Please check your internet connection") | ||
} catch (e: Exception) { | ||
Resource.Failure("Something went wrong!") | ||
} | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
app/src/main/java/com/muhammedesadcomert/shopping/common/util/Constant.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,7 @@ | ||
package com.muhammedesadcomert.shopping.common.util | ||
|
||
object Constant { | ||
const val BASE_URL = "https://api.shopiroller.com/v2.0/" | ||
const val API_KEY = "xXspnfUxPzOGKNu90bFAjlOTnMLpN8veiixvEFXUw9I=" | ||
const val ALIAS_KEY = "AtS1aPFxlIdVLth6ee2SEETlRxk=" | ||
} |
8 changes: 8 additions & 0 deletions
8
app/src/main/java/com/muhammedesadcomert/shopping/common/util/Resource.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,8 @@ | ||
package com.muhammedesadcomert.shopping.common.util | ||
|
||
sealed class Resource<out T> { | ||
data class Success<out T>(val data: T?) : Resource<T>() | ||
data class Failure(val errorMessage: String) : Resource<Nothing>() | ||
object Loading : Resource<Nothing>() | ||
object NONE : Resource<Nothing>() | ||
} |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/com/muhammedesadcomert/shopping/data/remote/ApiService.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,10 @@ | ||
package com.muhammedesadcomert.shopping.data.remote | ||
|
||
import com.muhammedesadcomert.shopping.ui.home.model.CategoryApiModel | ||
import retrofit2.Response | ||
import retrofit2.http.GET | ||
|
||
interface ApiService { | ||
@GET("categories") | ||
suspend fun getCategories(): Response<CategoryApiModel> | ||
} |
17 changes: 17 additions & 0 deletions
17
app/src/main/java/com/muhammedesadcomert/shopping/data/remote/AuthInterceptor.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,17 @@ | ||
package com.muhammedesadcomert.shopping.data.remote | ||
|
||
import com.muhammedesadcomert.shopping.common.util.Constant.ALIAS_KEY | ||
import com.muhammedesadcomert.shopping.common.util.Constant.API_KEY | ||
import okhttp3.Interceptor | ||
import okhttp3.Response | ||
|
||
class AuthInterceptor : Interceptor { | ||
override fun intercept(chain: Interceptor.Chain): Response { | ||
val requestBuilder = with(chain.request().newBuilder()) { | ||
addHeader("Api-Key", API_KEY) | ||
addHeader("Alias-Key", ALIAS_KEY) | ||
build() | ||
} | ||
return chain.proceed(requestBuilder).newBuilder().build() | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
app/src/main/java/com/muhammedesadcomert/shopping/data/repository/ProductRepository.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,12 @@ | ||
package com.muhammedesadcomert.shopping.data.repository | ||
|
||
import com.muhammedesadcomert.shopping.common.base.BaseRepository | ||
import com.muhammedesadcomert.shopping.common.util.Resource | ||
import com.muhammedesadcomert.shopping.data.remote.ApiService | ||
import com.muhammedesadcomert.shopping.ui.home.model.CategoryApiModel | ||
import javax.inject.Inject | ||
|
||
class ProductRepository @Inject constructor(private val apiService: ApiService) : BaseRepository() { | ||
suspend fun getCategories(): Resource<CategoryApiModel> = | ||
safeApiCall { apiService.getCategories() } | ||
} |
42 changes: 42 additions & 0 deletions
42
app/src/main/java/com/muhammedesadcomert/shopping/di/AppModule.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,42 @@ | ||
package com.muhammedesadcomert.shopping.di | ||
|
||
import com.muhammedesadcomert.shopping.common.util.Constant.BASE_URL | ||
import com.muhammedesadcomert.shopping.data.remote.ApiService | ||
import com.muhammedesadcomert.shopping.data.remote.AuthInterceptor | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import okhttp3.OkHttpClient | ||
import okhttp3.logging.HttpLoggingInterceptor | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.gson.GsonConverterFactory | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object AppModule { | ||
@Provides | ||
@Singleton | ||
fun provideOkHttpClient(authInterceptor: AuthInterceptor) = OkHttpClient.Builder() | ||
.addInterceptor(authInterceptor) | ||
.addInterceptor( | ||
HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.HEADERS) | ||
.setLevel(HttpLoggingInterceptor.Level.BODY) | ||
).build() | ||
|
||
@Provides | ||
@Singleton | ||
fun provideRetrofitClient(okHttpClient: OkHttpClient): Retrofit = Retrofit.Builder() | ||
.client(okHttpClient) | ||
.baseUrl(BASE_URL) | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.build() | ||
|
||
@Provides | ||
@Singleton | ||
fun provideApiService(retrofit: Retrofit): ApiService = retrofit.create(ApiService::class.java) | ||
|
||
@Provides | ||
fun provideAuthInterceptor() = AuthInterceptor() | ||
} |
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
41 changes: 41 additions & 0 deletions
41
app/src/main/java/com/muhammedesadcomert/shopping/ui/home/CategoryAdapter.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,41 @@ | ||
package com.muhammedesadcomert.shopping.ui.home | ||
|
||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.bumptech.glide.Glide | ||
import com.muhammedesadcomert.shopping.databinding.CategoryItemBinding | ||
import com.muhammedesadcomert.shopping.ui.home.CategoryAdapter.CategoryViewHolder | ||
import com.muhammedesadcomert.shopping.ui.home.model.Category | ||
|
||
class CategoryAdapter( | ||
private val categories: List<Category>, | ||
private val onItemClicked: (Category) -> Unit, | ||
) : RecyclerView.Adapter<CategoryViewHolder>() { | ||
|
||
inner class CategoryViewHolder(private val binding: CategoryItemBinding) : | ||
RecyclerView.ViewHolder(binding.root) { | ||
fun bind(category: Category) { | ||
with(binding) { | ||
Glide.with(itemView).load(category.icon).into(categoryIcon) | ||
categoryName.text = category.name | ||
} | ||
} | ||
} | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryViewHolder { | ||
return CategoryViewHolder( | ||
CategoryItemBinding.inflate(LayoutInflater.from(parent.context), parent, false) | ||
) | ||
} | ||
|
||
override fun onBindViewHolder(holder: CategoryViewHolder, position: Int) { | ||
val category = categories[position] | ||
holder.bind(category) | ||
holder.itemView.setOnClickListener { | ||
onItemClicked(category) | ||
} | ||
} | ||
|
||
override fun getItemCount() = categories.size | ||
} |
57 changes: 57 additions & 0 deletions
57
app/src/main/java/com/muhammedesadcomert/shopping/ui/home/HomeFragment.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.muhammedesadcomert.shopping.ui.home | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.viewModels | ||
import com.muhammedesadcomert.shopping.databinding.FragmentHomeBinding | ||
import com.muhammedesadcomert.shopping.ui.home.model.Category | ||
import dagger.hilt.android.AndroidEntryPoint | ||
|
||
@AndroidEntryPoint | ||
class HomeFragment : Fragment() { | ||
|
||
private var _binding: FragmentHomeBinding? = null | ||
private val binding get() = _binding!! | ||
|
||
private val viewModel: HomeViewModel by viewModels() | ||
private lateinit var categories: List<Category> | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle?, | ||
): View { | ||
_binding = FragmentHomeBinding.inflate(inflater, container, false) | ||
return binding.root | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
getCategories() | ||
} | ||
|
||
private fun getCategories() { | ||
viewModel.categories.observe(viewLifecycleOwner) { | ||
categories = it | ||
initCategoryAdapter() | ||
} | ||
} | ||
|
||
private fun initCategoryAdapter() { | ||
val categoryAdapter = CategoryAdapter(categories) { category -> | ||
} | ||
|
||
binding.recyclerViewCategories.apply { | ||
adapter = categoryAdapter | ||
setHasFixedSize(true) | ||
} | ||
} | ||
|
||
override fun onDestroyView() { | ||
super.onDestroyView() | ||
_binding = null | ||
} | ||
} |
Oops, something went wrong.