Skip to content

Commit

Permalink
Merge pull request #19 from reskimulud/dev
Browse files Browse the repository at this point in the history
Merge from branch Dev to Main, and finishing for v1.0.
  • Loading branch information
reskimulud committed Apr 12, 2022
2 parents 6a021d2 + 361de81 commit 7799b97
Show file tree
Hide file tree
Showing 25 changed files with 824 additions and 39 deletions.
1 change: 1 addition & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Mankgram">
<activity
android:theme="@style/Theme.OnBoardingStyle"
android:name=".ui.OnBoardingActivity"
android:exported="false" />
<activity
android:name=".ui.mainmenu.newstory.CameraActivity"
android:exported="false" />
<activity
android:name="com.mankart.mankgram.ui.authentication.AuthenticationActivity"
android:name=".ui.authentication.AuthenticationActivity"
android:exported="false" />
<activity
android:name=".ui.SplashScreenActivity"
Expand All @@ -31,7 +35,7 @@
</intent-filter>
</activity>
<activity
android:name="com.mankart.mankgram.ui.mainmenu.MainActivity"
android:name=".ui.mainmenu.MainActivity"
android:exported="true" />
</application>

Expand Down
33 changes: 31 additions & 2 deletions app/src/main/java/com/mankart/mankgram/data/UserRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import androidx.lifecycle.asLiveData
import com.mankart.mankgram.data.datastore.SettingPreference
import com.mankart.mankgram.data.network.ApiService
import com.mankart.mankgram.data.network.UserResponse
import com.mankart.mankgram.utils.ApiInterceptor
import com.mankart.mankgram.utils.AppExecutors
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.RequestBody
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

class UserRepository(
private val pref: SettingPreference,
Expand All @@ -31,6 +35,9 @@ class UserRepository(
fun getUserEmail() : LiveData<String> = pref.getUserEmail().asLiveData()
suspend fun saveUserEmail(value: String) = pref.saveUserEmail(value)

fun getIsFirstTime() : LiveData<Boolean> = pref.isFirstTime().asLiveData()
suspend fun saveIsFirstTime(value: Boolean) = pref.saveIsFirstTime(value)

suspend fun clearCache() = pref.clearCache()

/**
Expand All @@ -56,9 +63,31 @@ class UserRepository(
return apiService.userRegister(user)
}

fun getUserStories() = apiService.getUserStories()
fun getUserStories(token: String): Call<UserResponse> {
val client = OkHttpClient.Builder()
.addInterceptor(ApiInterceptor(token))
.build()
val retrofit = Retrofit.Builder()
.baseUrl("https://story-api.dicoding.dev/v1/")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build()
val mApiService = retrofit.create(ApiService::class.java)
return mApiService.getUserStories()
}

fun uploadStory(photo: MultipartBody.Part, description: RequestBody) = apiService.postUserStory(photo, description)
fun uploadStory(photo: MultipartBody.Part, description: RequestBody, token: String): Call<UserResponse> {
val client = OkHttpClient.Builder()
.addInterceptor(ApiInterceptor(token))
.build()
val retrofit = Retrofit.Builder()
.baseUrl("https://story-api.dicoding.dev/v1/")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build()
val mApiService = retrofit.create(ApiService::class.java)
return mApiService.postUserStory(photo, description)
}

companion object {
@Volatile
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mankart.mankgram.data.datastore

import android.util.Log
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
Expand All @@ -13,6 +14,7 @@ class SettingPreference private constructor(private val dataStore: DataStore<Pre
private val USER_TOKEN_KEY = stringPreferencesKey("user_token")
private val USER_EMAIL_KEY = stringPreferencesKey("user_email")
private val USER_NAME_KEY = stringPreferencesKey("user_name")
private val FIRST_TIME_KEY = booleanPreferencesKey("first_time")

/**
* Dark Mode
Expand All @@ -39,6 +41,7 @@ class SettingPreference private constructor(private val dataStore: DataStore<Pre
suspend fun saveUserToken(token: String) {
dataStore.edit {
it[USER_TOKEN_KEY] = token
Log.e("SettingPreference", "Token saved! saveUserToken: $token")
}
}

Expand Down Expand Up @@ -70,6 +73,20 @@ class SettingPreference private constructor(private val dataStore: DataStore<Pre
}
}

/**
* First Time
*/

fun isFirstTime(): Flow<Boolean> = dataStore.data.map {
it[FIRST_TIME_KEY] ?: true
}

suspend fun saveIsFirstTime(firstTime: Boolean) {
dataStore.edit {
it[FIRST_TIME_KEY] = firstTime
}
}

suspend fun clearCache() {
dataStore.edit {
it.clear()
Expand Down
50 changes: 50 additions & 0 deletions app/src/main/java/com/mankart/mankgram/ui/OnBoardingActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.mankart.mankgram.ui

import android.content.Intent
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.WindowInsets
import android.view.WindowManager
import androidx.activity.viewModels
import com.mankart.mankgram.R
import com.mankart.mankgram.databinding.ActivityOnBoardingBinding
import com.mankart.mankgram.ui.authentication.AuthenticationActivity
import com.mankart.mankgram.ui.mainmenu.setting.SettingViewModel

class OnBoardingActivity : AppCompatActivity() {
private lateinit var binding: ActivityOnBoardingBinding
private lateinit var factory: ViewModelFactory
private val settingViewModel: SettingViewModel by viewModels { factory }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

binding = ActivityOnBoardingBinding.inflate(layoutInflater)
setContentView(binding.root)

factory = ViewModelFactory.getInstance(this)

binding.button.setOnClickListener {
settingViewModel.saveIsFirstTime(false)
startActivity(Intent(this, AuthenticationActivity::class.java))
finish()
}

setupView()
}

private fun setupView() {
@Suppress("DEPRECATION")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.insetsController?.hide(WindowInsets.Type.statusBars())
} else {
window.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
)
}
supportActionBar?.hide()
}

}
26 changes: 18 additions & 8 deletions app/src/main/java/com/mankart/mankgram/ui/SplashScreenActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,29 @@ class SplashScreenActivity : AppCompatActivity() {
}
}

authenticationViewModel.getUserToken().observe(this) { token ->
if (token.isNullOrEmpty() || token == "not_set_yet") {
settingViewModel.getIsFirstTime().observe(this) { isFirstTime ->
if (isFirstTime) {
Handler(Looper.getMainLooper()).postDelayed({
val intent = Intent(this@SplashScreenActivity, AuthenticationActivity::class.java)
val intent = Intent(this@SplashScreenActivity, OnBoardingActivity::class.java)
startActivity(intent)
finish()
}, delayMillis)
} else {
Handler(Looper.getMainLooper()).postDelayed({
val intent = Intent(this@SplashScreenActivity, MainActivity::class.java)
startActivity(intent)
finish()
}, delayMillis)
authenticationViewModel.getUserToken().observe(this) { token ->
if (token.isNullOrEmpty() || token == "not_set_yet") {
Handler(Looper.getMainLooper()).postDelayed({
val intent = Intent(this@SplashScreenActivity, AuthenticationActivity::class.java)
startActivity(intent)
finish()
}, delayMillis)
} else {
Handler(Looper.getMainLooper()).postDelayed({
val intent = Intent(this@SplashScreenActivity, MainActivity::class.java)
startActivity(intent)
finish()
}, delayMillis)
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ class AuthenticationViewModel(private val userRepository: UserRepository) : View
}
}

fun getUserName() = userRepository.getUserName()

fun saveUserName(name: String?) {
viewModelScope.launch {
if (name != null) {
Expand All @@ -112,8 +110,6 @@ class AuthenticationViewModel(private val userRepository: UserRepository) : View
}
}

fun getUserEmail() = userRepository.getUserEmail()

fun saveUserEmail(email: String) {
viewModelScope.launch {
userRepository.saveUserEmail(email)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,18 @@ class LoginFragment : Fragment() {
authenticationViewModel.error.observe(viewLifecycleOwner) { event ->
event.getContentIfNotHandled()?.let { error ->
if (!error) {
initObserve()
authenticationViewModel.user.observe(viewLifecycleOwner) { event ->
event.getContentIfNotHandled()?.let {
authenticationViewModel.saveUserToken(it.token)
authenticationViewModel.saveUserName(it.name)

authenticationViewModel.saveUserEmail(email)
val intent = Intent(activity, MainActivity::class.java)
startActivity(intent)
activity?.finish()
}
}
authenticationViewModel.saveUserEmail(email)
val intent = Intent(activity, MainActivity::class.java)
startActivity(intent)
activity?.finish()
} else {
val msg = getString(R.string.wrong_credential)
Toast.makeText(context, "$message: $msg", Toast.LENGTH_SHORT).show()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mankart.mankgram.ui.mainmenu.home

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand Down Expand Up @@ -37,19 +38,23 @@ class HomeFragment : Fragment() {

factory = ViewModelFactory.getInstance(requireActivity())

initObserve()

binding.refreshLayout.setOnRefreshListener {
binding.refreshLayout.isRefreshing = true
homeViewModel.getUserStories()
fetchUserStories()
}
fetchUserStories()

binding.refreshLayout.isRefreshing = true
homeViewModel.getUserStories()

initObserve()
initRecycler()
}

private fun fetchUserStories() {
homeViewModel.getUserToken().observe(viewLifecycleOwner) {
binding.refreshLayout.isRefreshing = true
homeViewModel.getUserStories(it)
Log.e("Home", "Token: $it")
}
}

private fun initObserve() {
homeViewModel.getName().observe(viewLifecycleOwner) {
binding.tvWelcomeName.text = it
Expand All @@ -63,8 +68,8 @@ class HomeFragment : Fragment() {
binding.rvStory.layoutManager = LinearLayoutManager(activity)
listStoryAdapter = ListStoryAdapter()
homeViewModel.userStories.observe(viewLifecycleOwner) {
listStoryAdapter.setData(it)
binding.refreshLayout.isRefreshing = false
listStoryAdapter.setData(it)
}
binding.rvStory.adapter = listStoryAdapter
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ class HomeViewModel(private val userRepository: UserRepository) : ViewModel() {
private var _message = MutableLiveData<String>()
val message: LiveData<String> = _message

fun getUserToken() = userRepository.getUserToken()

fun getName(): LiveData<String> {
return userRepository.getUserName()
}

fun getUserStories() {
val client = userRepository.getUserStories()
fun getUserStories(token: String) {
val client = userRepository.getUserStories(token)
client.enqueue(object : Callback<UserResponse>{
override fun onResponse(call: Call<UserResponse>, response: Response<UserResponse>) {
if (response.isSuccessful) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.fragment.app.activityViewModels
import com.mankart.mankgram.*
import com.mankart.mankgram.databinding.FragmentNewStoryBinding
import com.mankart.mankgram.ui.ViewModelFactory
import com.mankart.mankgram.ui.authentication.AuthenticationViewModel
import com.mankart.mankgram.ui.mainmenu.MainActivity
import com.mankart.mankgram.utils.reduceFileImage
import com.mankart.mankgram.utils.rotateBitmap
Expand All @@ -31,6 +32,7 @@ class NewStoryFragment : Fragment() {
private var _binding: FragmentNewStoryBinding? = null
private lateinit var factory: ViewModelFactory
private val newStoryViewModel: NewStoryViewModel by activityViewModels { factory }
private val authenticationViewModel: AuthenticationViewModel by activityViewModels { factory }
private lateinit var result: Bitmap
private lateinit var navView: View
private var getFile: File? = null
Expand Down Expand Up @@ -85,6 +87,7 @@ class NewStoryFragment : Fragment() {
if (!error) {
Toast.makeText(activity, getString(R.string.upload_success), Toast.LENGTH_LONG).show()
startActivity(Intent(activity, MainActivity::class.java))
activity?.finish()
}
}
}
Expand Down Expand Up @@ -112,7 +115,9 @@ class NewStoryFragment : Fragment() {
file.name,
requestImageFile
)
newStoryViewModel.uploadStory(imageMultipart, requestDescription)
authenticationViewModel.getUserToken().observe(viewLifecycleOwner) { token ->
newStoryViewModel.uploadStory(imageMultipart, requestDescription, token)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class NewStoryViewModel(private val userRepository: UserRepository): ViewModel()
private var _loading = MutableLiveData<Event<Boolean>>()
val loading: LiveData<Event<Boolean>> = _loading

fun uploadStory(photo: MultipartBody.Part, description: RequestBody) {
fun uploadStory(photo: MultipartBody.Part, description: RequestBody, token: String) {
_loading.value = Event(true)
val client = userRepository.uploadStory(photo, description)
val client = userRepository.uploadStory(photo, description, token)
client.enqueue(object: Callback<UserResponse> {
override fun onResponse(call: Call<UserResponse>, response: Response<UserResponse>) {
userRepository.appExecutors.networkIO.execute {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,11 @@ class SettingViewModel(private val userRepository: UserRepository) : ViewModel()

fun getUserEmail(): LiveData<String> = userRepository.getUserEmail()

fun getIsFirstTime(): LiveData<Boolean> = userRepository.getIsFirstTime()
fun saveIsFirstTime(value: Boolean) {
viewModelScope.launch {
userRepository.saveIsFirstTime(value)
}
}

}
Loading

0 comments on commit 7799b97

Please sign in to comment.