Skip to content

Commit

Permalink
Formatting Code
Browse files Browse the repository at this point in the history
  • Loading branch information
Debanshu777 committed May 3, 2022
1 parent 505db5c commit a113732
Show file tree
Hide file tree
Showing 45 changed files with 300 additions and 329 deletions.
28 changes: 14 additions & 14 deletions app/src/main/java/com/debanshu777/compose_github/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ object AppModule {

@Provides
@Singleton
fun providesSqlDriver(@ApplicationContext context: Context):SqlDriver{
fun providesSqlDriver(@ApplicationContext context: Context): SqlDriver {
return AndroidSqliteDriver(
schema = GithubDatatbase.Schema,
context = context,
Expand All @@ -40,7 +40,7 @@ object AppModule {

@Provides
@Singleton
fun provideRepositoryDataSource(driver: SqlDriver):RepositoryDataSource{
fun provideRepositoryDataSource(driver: SqlDriver): RepositoryDataSource {
return RepositoryDataSourceImpl(GithubDatatbase(driver))
}

Expand All @@ -51,33 +51,33 @@ object AppModule {
}

@Provides
fun provideKtorAPIClient():HttpClient{
fun provideKtorAPIClient(): HttpClient {
val json = kotlinx.serialization.json.Json {
encodeDefaults = true
ignoreUnknownKeys = true
isLenient = true
}
return HttpClient(Android){
install(JsonFeature){
return HttpClient(Android) {
install(JsonFeature) {
serializer = KotlinxSerializer(json)
}
install(Logging){
logger= object : Logger {
install(Logging) {
logger = object : Logger {
override fun log(message: String) {
Log.d("Network Message","log: $message")
Log.d("Network Message", "log: $message")
}
}
level= LogLevel.ALL
level = LogLevel.ALL
}
install(HttpTimeout){
socketTimeoutMillis=30_000
requestTimeoutMillis=30_000
connectTimeoutMillis=30_000
install(HttpTimeout) {
socketTimeoutMillis = 30_000
requestTimeoutMillis = 30_000
connectTimeoutMillis = 30_000
}
defaultRequest {
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.debanshu777.compose_github.ui.feature_trending.state.DeveloperTrendingState
import com.debanshu777.compose_github.ui.feature_profile.state.ProfileState
import com.debanshu777.compose_github.ui.feature_search.state.SearchState
import com.debanshu777.compose_github.ui.feature_search.state.SearchWidgetState
import com.debanshu777.compose_github.ui.feature_trending.state.DeveloperTrendingState
import com.debanshu777.compose_github.ui.feature_trending.state.RepositoryTrendingState
import com.debanshu777.compose_github.utils.Resource
import dagger.hilt.android.lifecycle.HiltViewModel
Expand All @@ -17,86 +17,90 @@ import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class GitHubViewModel @Inject constructor(private val mainRepository: MainRepository) :ViewModel() {
class GitHubViewModel @Inject constructor(private val mainRepository: MainRepository) : ViewModel() {
val userDataState = MutableStateFlow(ProfileState())
val trendingRepositoryDataState = MutableStateFlow(RepositoryTrendingState())
val trendingDeveloperDataState = MutableStateFlow(DeveloperTrendingState())
val searchState= MutableStateFlow(SearchState())
val searchState = MutableStateFlow(SearchState())

init {
getTrendingRepository("monthly")
getTrendingDeveloper("monthly")
}
private val _searchWidgetState:MutableState<SearchWidgetState> =
private val _searchWidgetState: MutableState<SearchWidgetState> =
mutableStateOf(value = SearchWidgetState.CLOSED)
val searchWidgetState:State<SearchWidgetState> = _searchWidgetState
val searchWidgetState: State<SearchWidgetState> = _searchWidgetState

private val _searchTextState:MutableState<String> =
private val _searchTextState: MutableState<String> =
mutableStateOf(value = "")
val searchTextState:State<String> = _searchTextState
val searchTextState: State<String> = _searchTextState

fun updateSearchWidgetState(newValue: SearchWidgetState){
_searchWidgetState.value=newValue
fun updateSearchWidgetState(newValue: SearchWidgetState) {
_searchWidgetState.value = newValue
}

fun updateSearchTextState(newValue:String){
_searchTextState.value=newValue
fun updateSearchTextState(newValue: String) {
_searchTextState.value = newValue
}

fun getUserData(userName:String)=viewModelScope.launch {
when(val result =mainRepository.getUserData(userName)){
is Resource.Loading->{
userDataState.value=ProfileState(isLoading = true)
fun getUserData(userName: String) = viewModelScope.launch {
when (val result = mainRepository.getUserData(userName)) {
is Resource.Loading -> {
userDataState.value = ProfileState(isLoading = true)
}
is Resource.Success ->{
userDataState.value=ProfileState(data= result.data)
is Resource.Success -> {
userDataState.value = ProfileState(data = result.data)
}
is Resource.Error->{
searchState.value= SearchState(error=result.message)
is Resource.Error -> {
searchState.value = SearchState(error = result.message)
}
}
}
fun searchUser(searchText:String)=viewModelScope.launch {
when(val result =mainRepository.searchUser(searchText)){
is Resource.Loading->{
searchState.value=SearchState(isLoading = true)
fun searchUser(searchText: String) = viewModelScope.launch {
when (val result = mainRepository.searchUser(searchText)) {
is Resource.Loading -> {
searchState.value = SearchState(isLoading = true)
}
is Resource.Success ->{
searchState.value=SearchState(data= if(result.data ==null) emptyList() else result.data.items)
is Resource.Success -> {
searchState.value = SearchState(data = if (result.data == null) emptyList() else result.data.items)
}
is Resource.Error->{
searchState.value= SearchState(error=result.message)
is Resource.Error -> {
searchState.value = SearchState(error = result.message)
}
}
}

fun getTrendingRepository(timeline:String)=viewModelScope.launch {
when(val result = mainRepository.getTrendingRepository(timeline)){
is Resource.Loading->{
trendingRepositoryDataState.value=RepositoryTrendingState(isLoading = true)
fun getTrendingRepository(timeline: String) = viewModelScope.launch {
when (val result = mainRepository.getTrendingRepository(timeline)) {
is Resource.Loading -> {
trendingRepositoryDataState.value = RepositoryTrendingState(isLoading = true)
}
is Resource.Success ->{
trendingRepositoryDataState.value=RepositoryTrendingState(data= result.data
?: emptyList())
is Resource.Success -> {
trendingRepositoryDataState.value = RepositoryTrendingState(
data = result.data
?: emptyList()
)
}
is Resource.Error->{
trendingRepositoryDataState.value= RepositoryTrendingState(error=result.message)
is Resource.Error -> {
trendingRepositoryDataState.value = RepositoryTrendingState(error = result.message)
}
}
}

fun getTrendingDeveloper(timeline:String)=viewModelScope.launch {
when(val result = mainRepository.getTrendingDeveloper(timeline)){
is Resource.Loading->{
trendingDeveloperDataState.value= DeveloperTrendingState(isLoading = true)
fun getTrendingDeveloper(timeline: String) = viewModelScope.launch {
when (val result = mainRepository.getTrendingDeveloper(timeline)) {
is Resource.Loading -> {
trendingDeveloperDataState.value = DeveloperTrendingState(isLoading = true)
}
is Resource.Success ->{
trendingDeveloperDataState.value= DeveloperTrendingState(data= result.data
?: emptyList())
is Resource.Success -> {
trendingDeveloperDataState.value = DeveloperTrendingState(
data = result.data
?: emptyList()
)
}
is Resource.Error->{
trendingDeveloperDataState.value= DeveloperTrendingState(error=result.message)
is Resource.Error -> {
trendingDeveloperDataState.value = DeveloperTrendingState(error = result.message)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import javax.inject.Inject
class MainRepository @Inject constructor(
private val remoteRepository: RemoteRepository,
private val localRepository: LocalRepository
) {
suspend fun getRepositoryById(id: Long): RepositoryFollow?{
) {
suspend fun getRepositoryById(id: Long): RepositoryFollow? {
return localRepository.getRepositoryById(id)
}
suspend fun getDeveloperById(Id: Long): DeveloperFollow?{
return localRepository.getDeveloperById(Id);
suspend fun getDeveloperById(Id: Long): DeveloperFollow? {
return localRepository.getDeveloperById(Id)
}

suspend fun getRepositoryByName(name: String): RepositoryFollow?{
suspend fun getRepositoryByName(name: String): RepositoryFollow? {
return localRepository.getRepositoryByName(name)
}
suspend fun getDeveloperByName(name: String): DeveloperFollow? {
Expand Down Expand Up @@ -56,7 +56,7 @@ class MainRepository @Inject constructor(
stars: Long
) {
return localRepository.insertRepository(
id, authorName,name,avatar,description,language,languageColor,forks, stars
id, authorName, name, avatar, description, language, languageColor, forks, stars
)
}

Expand All @@ -67,23 +67,23 @@ class MainRepository @Inject constructor(
avatar: String
) {
return localRepository.insertDeveloper(
id, userName,name,avatar
id, userName, name, avatar
)
}

suspend fun getUserData(userId:String): Resource<GitHubUserResponse> {
suspend fun getUserData(userId: String): Resource<GitHubUserResponse> {
return remoteRepository.getUserData(userId)
}

suspend fun searchUser(searchText:String): Resource<GitHubSearchResponse> {
suspend fun searchUser(searchText: String): Resource<GitHubSearchResponse> {
return remoteRepository.searchUser(searchText)
}

suspend fun getTrendingRepository(timeline:String): Resource<List<TrendingRepositoryItem>> {
suspend fun getTrendingRepository(timeline: String): Resource<List<TrendingRepositoryItem>> {
return remoteRepository.getTrendingRepository(timeline)
}

suspend fun getTrendingDeveloper(timeline:String): Resource<List<TrendingDeveloperItem>> {
suspend fun getTrendingDeveloper(timeline: String): Resource<List<TrendingDeveloperItem>> {
return remoteRepository.getTrendingDeveloper(timeline)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package com.debanshu777.compose_github.network.dataSource.local

import composedb.githubDB.DeveloperFollow
import dagger.Component
import kotlinx.coroutines.flow.Flow

interface DeveloperDataSource {
suspend fun getDeveloperById(Id:Long):DeveloperFollow?
suspend fun getDeveloperById(Id: Long): DeveloperFollow?

suspend fun getDeveloperByName(name:String): DeveloperFollow?
suspend fun getDeveloperByName(name: String): DeveloperFollow?

fun getAllDeveloper(): Flow<List<DeveloperFollow>>

suspend fun deleteDeveloperById(id:Long)
suspend fun deleteDeveloperById(id: Long)

suspend fun insertDeveloper(
id:Long?=null,
userName:String,
name:String,
avatar:String,
id: Long? = null,
userName: String,
name: String,
avatar: String,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,22 @@ import com.debanshu777.compose_github.GithubDatatbase
import com.squareup.sqldelight.runtime.coroutines.asFlow
import com.squareup.sqldelight.runtime.coroutines.mapToList
import composedb.githubDB.DeveloperFollow
import dagger.Component
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.withContext
import javax.inject.Inject

class DeveloperDataSourceImpl(
db: GithubDatatbase
):DeveloperDataSource {
private val queries=db.developerFollowQueries
) : DeveloperDataSource {
private val queries = db.developerFollowQueries
override suspend fun getDeveloperById(Id: Long): DeveloperFollow? {
return withContext(Dispatchers.IO){
return withContext(Dispatchers.IO) {
queries.getDeveloperById(Id).executeAsOneOrNull()
}
}

override suspend fun getDeveloperByName(name: String): DeveloperFollow? {
return withContext(Dispatchers.IO){
return withContext(Dispatchers.IO) {
queries.getDeveloperByName(name).executeAsOneOrNull()
}
}
Expand All @@ -31,7 +29,7 @@ class DeveloperDataSourceImpl(
}

override suspend fun deleteDeveloperById(id: Long) {
return withContext(Dispatchers.IO){
return withContext(Dispatchers.IO) {
queries.deleteDeveloperById(id)
}
}
Expand All @@ -42,7 +40,7 @@ class DeveloperDataSourceImpl(
name: String,
avatar: String
) {
return withContext(Dispatchers.IO){
return withContext(Dispatchers.IO) {
queries.insertDeveloper(
id,
userName,
Expand All @@ -51,4 +49,4 @@ class DeveloperDataSourceImpl(
)
}
}
}
}
Loading

0 comments on commit a113732

Please sign in to comment.