Skip to content

Commit

Permalink
Merge pull request #3 from apriantoa917/submission-2-revised
Browse files Browse the repository at this point in the history
Submission 2 revised
  • Loading branch information
apriantoa917 authored May 9, 2022
2 parents b8bb9c3 + fc33e79 commit 5d8bb41
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 28 deletions.
17 changes: 0 additions & 17 deletions .idea/deploymentTargetDropDown.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
Expand All @@ -15,8 +17,8 @@ import com.aprianto.dicostory.data.repository.remote.ApiConfig
import com.aprianto.dicostory.data.viewmodel.*
import com.aprianto.dicostory.databinding.ActivityMainBinding
import com.aprianto.dicostory.ui.auth.AuthActivity
import com.aprianto.dicostory.ui.dashboard.folder.FolderFragment
import com.aprianto.dicostory.ui.dashboard.explore.ExploreFragment
import com.aprianto.dicostory.ui.dashboard.folder.FolderFragment
import com.aprianto.dicostory.ui.dashboard.home.HomeFragment
import com.aprianto.dicostory.ui.dashboard.profile.ProfileFragment
import com.aprianto.dicostory.ui.dashboard.story.CameraActivity
Expand All @@ -37,6 +39,8 @@ class MainActivity : AppCompatActivity() {
private val pref = SettingPreferences.getInstance(dataStore)
private val settingViewModel: SettingViewModel by viewModels { ViewModelSettingFactory(pref) }
private var token = ""
private var fragmentHome: HomeFragment? = null
private lateinit var startNewStory: ActivityResultLauncher<Intent>

@RequiresApi(Build.VERSION_CODES.M)
@OptIn(DelicateCoroutinesApi::class)
Expand All @@ -45,27 +49,33 @@ class MainActivity : AppCompatActivity() {
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)


val fragmentProfile = ProfileFragment()
val fragmentHome = HomeFragment()
fragmentHome = HomeFragment()
val fragmentExplore = ExploreFragment()
val fragmentDownloaded = FolderFragment()

/* load latest story when new story successfully uploaded */
startNewStory =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK) {
fragmentHome?.onRefresh()
}
}

/* obtain token for reuse in child fragments */
settingViewModel.getUserPreferences(Constanta.UserPreferences.UserToken.name)
.observe(this) {
token = "Bearer $it"
/* start load data after token obtained */
switchFragment(fragmentHome)
switchFragment(fragmentHome!!)
}

binding.bottomNavigationView.background = null // hide abnormal layer in bottom nav

binding.bottomNavigationView.setOnNavigationItemSelectedListener {
when (it.itemId) {
R.id.navigation_home -> {
switchFragment(fragmentHome)
switchFragment(fragmentHome!!)
true
}
R.id.navigation_explore -> {
Expand Down Expand Up @@ -113,7 +123,8 @@ class MainActivity : AppCompatActivity() {
binding.fab.setOnClickListener {
/* ask permission for camera first before launch camera */
if (Helper.isPermissionGranted(this, Manifest.permission.CAMERA)) {
startActivity(Intent(this, CameraActivity::class.java))
val intent = Intent(this@MainActivity, CameraActivity::class.java)
startNewStory.launch(intent)
} else {
ActivityCompat.requestPermissions(
this@MainActivity,
Expand Down Expand Up @@ -164,6 +175,8 @@ class MainActivity : AppCompatActivity() {

/* after return from other activity, reinitialize folder data (after download story i.e)*/
loadFolderData()


}

/* return current token from dataPreference to child fragment */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ class HomeFragment : Fragment(), SwipeRefreshLayout.OnRefreshListener {
rvAdapter.refresh()
Timer().schedule(2000) {
binding.swipeRefresh.isRefreshing = false
binding.rvStory.smoothScrollToPosition(0)
}
binding.rvStory.scrollToPosition(0)
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ class CameraActivity : AppCompatActivity() {
NewStoryActivity.EXTRA_CAMERA_MODE,
cameraSelector == CameraSelector.DEFAULT_BACK_CAMERA
)
this@CameraActivity.finish()
intent.flags = Intent.FLAG_ACTIVITY_FORWARD_RESULT
startActivity(intent)
this@CameraActivity.finish()
}
}
}
Expand Down Expand Up @@ -128,15 +129,15 @@ class CameraActivity : AppCompatActivity() {
}

override fun onImageSaved(output: ImageCapture.OutputFileResults) {

val intent = Intent(this@CameraActivity, NewStoryActivity::class.java)
intent.putExtra(NewStoryActivity.EXTRA_PHOTO_RESULT, photoFile)
intent.putExtra(
NewStoryActivity.EXTRA_CAMERA_MODE,
cameraSelector == CameraSelector.DEFAULT_BACK_CAMERA
)
this@CameraActivity.finish()
intent.flags = Intent.FLAG_ACTIVITY_FORWARD_RESULT
startActivity(intent)
this@CameraActivity.finish()
}
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ class NewStoryActivity : AppCompatActivity() {
)
val btnOk = dialog.findViewById<Button>(R.id.button_ok)
btnOk.setOnClickListener {
setResult(RESULT_OK)
finish()
}
dialog.show()
}

}
vm.loading.observe(this) {
binding.loading.root.visibility = it
Expand Down

0 comments on commit 5d8bb41

Please sign in to comment.