From fc33e79b5854cb7f4fc990ee1db0905cbd403737 Mon Sep 17 00:00:00 2001 From: apriantoa917 Date: Thu, 5 May 2022 08:12:46 +0700 Subject: [PATCH] revising rejected submission : uploaded story show on top of list (refresh) --- .../dicostory/ui/dashboard/MainActivity.kt | 25 ++++++++++++++----- .../ui/dashboard/home/HomeFragment.kt | 2 +- .../ui/dashboard/story/CameraActivity.kt | 7 +++--- .../ui/dashboard/story/NewStoryActivity.kt | 2 +- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/aprianto/dicostory/ui/dashboard/MainActivity.kt b/app/src/main/java/com/aprianto/dicostory/ui/dashboard/MainActivity.kt index 19bd468..bd97561 100644 --- a/app/src/main/java/com/aprianto/dicostory/ui/dashboard/MainActivity.kt +++ b/app/src/main/java/com/aprianto/dicostory/ui/dashboard/MainActivity.kt @@ -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 @@ -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 @@ -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 @RequiresApi(Build.VERSION_CODES.M) @OptIn(DelicateCoroutinesApi::class) @@ -45,19 +49,25 @@ 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 @@ -65,7 +75,7 @@ class MainActivity : AppCompatActivity() { binding.bottomNavigationView.setOnNavigationItemSelectedListener { when (it.itemId) { R.id.navigation_home -> { - switchFragment(fragmentHome) + switchFragment(fragmentHome!!) true } R.id.navigation_explore -> { @@ -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, @@ -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 */ diff --git a/app/src/main/java/com/aprianto/dicostory/ui/dashboard/home/HomeFragment.kt b/app/src/main/java/com/aprianto/dicostory/ui/dashboard/home/HomeFragment.kt index 6537b20..e1f9240 100644 --- a/app/src/main/java/com/aprianto/dicostory/ui/dashboard/home/HomeFragment.kt +++ b/app/src/main/java/com/aprianto/dicostory/ui/dashboard/home/HomeFragment.kt @@ -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) { diff --git a/app/src/main/java/com/aprianto/dicostory/ui/dashboard/story/CameraActivity.kt b/app/src/main/java/com/aprianto/dicostory/ui/dashboard/story/CameraActivity.kt index 3bfbf51..2d7c871 100644 --- a/app/src/main/java/com/aprianto/dicostory/ui/dashboard/story/CameraActivity.kt +++ b/app/src/main/java/com/aprianto/dicostory/ui/dashboard/story/CameraActivity.kt @@ -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() } } } @@ -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() } } ) diff --git a/app/src/main/java/com/aprianto/dicostory/ui/dashboard/story/NewStoryActivity.kt b/app/src/main/java/com/aprianto/dicostory/ui/dashboard/story/NewStoryActivity.kt index 3ebab34..df94118 100644 --- a/app/src/main/java/com/aprianto/dicostory/ui/dashboard/story/NewStoryActivity.kt +++ b/app/src/main/java/com/aprianto/dicostory/ui/dashboard/story/NewStoryActivity.kt @@ -119,11 +119,11 @@ class NewStoryActivity : AppCompatActivity() { ) val btnOk = dialog.findViewById