1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-15 19:27:31 +00:00

Convert FavoritesFragment to kotlin

This commit is contained in:
Ammar Githam 2021-05-25 06:45:07 +09:00
parent 7f21eed5cf
commit d284a1901e

View File

@ -1,159 +1,134 @@
package awais.instagrabber.fragments; package awais.instagrabber.fragments
import android.content.Context; import android.content.DialogInterface
import android.os.Bundle; import android.os.Bundle
import android.util.Log; import android.util.Log
import android.view.LayoutInflater; import android.view.LayoutInflater
import android.view.View; import android.view.View
import android.view.ViewGroup; import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.NavHostFragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import awais.instagrabber.R
import awais.instagrabber.adapters.FavoritesAdapter
import awais.instagrabber.databinding.FragmentFavoritesBinding
import awais.instagrabber.db.datasources.FavoriteDataSource
import awais.instagrabber.db.entities.Favorite
import awais.instagrabber.db.repositories.FavoriteRepository
import awais.instagrabber.db.repositories.RepositoryCallback
import awais.instagrabber.models.enums.FavoriteType
import awais.instagrabber.utils.extensions.TAG
import awais.instagrabber.viewmodels.FavoritesViewModel
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import androidx.annotation.NonNull; class FavoritesFragment : Fragment() {
import androidx.annotation.Nullable; private var shouldRefresh = true
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.fragment.NavHostFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.dialog.MaterialAlertDialogBuilder; private lateinit var binding: FragmentFavoritesBinding
private lateinit var root: RecyclerView
private lateinit var favoritesViewModel: FavoritesViewModel
private lateinit var favoriteRepository: FavoriteRepository
private lateinit var adapter: FavoritesAdapter
import java.util.List; override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
import awais.instagrabber.R; val context = context ?: return
import awais.instagrabber.adapters.FavoritesAdapter; favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(context))
import awais.instagrabber.databinding.FragmentFavoritesBinding; favoritesViewModel = ViewModelProvider(this).get(FavoritesViewModel::class.java)
import awais.instagrabber.db.datasources.FavoriteDataSource;
import awais.instagrabber.db.entities.Favorite;
import awais.instagrabber.db.repositories.FavoriteRepository;
import awais.instagrabber.db.repositories.RepositoryCallback;
import awais.instagrabber.viewmodels.FavoritesViewModel;
public class FavoritesFragment extends Fragment {
private static final String TAG = "FavoritesFragment";
private boolean shouldRefresh = true;
private FragmentFavoritesBinding binding;
private RecyclerView root;
private FavoritesViewModel favoritesViewModel;
private FavoritesAdapter adapter;
private FavoriteRepository favoriteRepository;
@Override
public void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Context context = getContext();
if (context == null) return;
favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(context));
} }
@NonNull override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
@Override if (this::root.isInitialized) {
public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable final Bundle savedInstanceState) { shouldRefresh = false
if (root != null) { return root
shouldRefresh = false;
return root;
} }
binding = FragmentFavoritesBinding.inflate(getLayoutInflater()); binding = FragmentFavoritesBinding.inflate(layoutInflater)
root = binding.getRoot(); root = binding.root
binding.favoriteList.setLayoutManager(new LinearLayoutManager(getContext())); binding.favoriteList.layoutManager = LinearLayoutManager(context)
return root; return root
} }
@Override override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
public void onViewCreated(@NonNull final View view, @Nullable final Bundle savedInstanceState) { if (!shouldRefresh) return
if (!shouldRefresh) return; init()
init(); shouldRefresh = false
shouldRefresh = false;
} }
@Override override fun onResume() {
public void onResume() { super.onResume()
super.onResume(); if (!this::adapter.isInitialized) return
if (favoritesViewModel == null || adapter == null) return;
// refresh list every time in onViewStateRestored since it is cheaper than implementing pull down to refresh // refresh list every time in onViewStateRestored since it is cheaper than implementing pull down to refresh
favoritesViewModel.getList().observe(getViewLifecycleOwner(), adapter::submitList); favoritesViewModel.list.observe(viewLifecycleOwner, { list: List<Favorite?>? -> adapter.submitList(list) })
favoriteRepository.getAllFavorites(new RepositoryCallback<List<Favorite>>() { favoriteRepository.getAllFavorites(object : RepositoryCallback<List<Favorite>> {
@Override override fun onSuccess(favorites: List<Favorite>) {
public void onSuccess(final List<Favorite> favorites) { favoritesViewModel.list.postValue(favorites)
favoritesViewModel.getList().postValue(favorites);
} }
@Override override fun onDataNotAvailable() {}
public void onDataNotAvailable() {} })
});
} }
private void init() { private fun init() {
favoritesViewModel = new ViewModelProvider(this).get(FavoritesViewModel.class); adapter = FavoritesAdapter({ model: Favorite ->
adapter = new FavoritesAdapter(model -> { when (model.type) {
// navigate FavoriteType.USER -> {
switch (model.getType()) { val username = model.query
case USER: {
final String username = model.getQuery();
// Log.d(TAG, "username: " + username); // Log.d(TAG, "username: " + username);
final NavController navController = NavHostFragment.findNavController(this); val navController = NavHostFragment.findNavController(this)
final Bundle bundle = new Bundle(); val bundle = Bundle()
bundle.putString("username", "@" + username); bundle.putString("username", "@$username")
navController.navigate(R.id.action_global_profileFragment, bundle); navController.navigate(R.id.action_global_profileFragment, bundle)
break;
} }
case LOCATION: { FavoriteType.LOCATION -> {
final String locationId = model.getQuery(); val locationId = model.query
// Log.d(TAG, "locationId: " + locationId); // Log.d(TAG, "locationId: " + locationId);
final NavController navController = NavHostFragment.findNavController(this); val navController = NavHostFragment.findNavController(this)
final Bundle bundle = new Bundle(); val bundle = Bundle()
try { try {
bundle.putLong("locationId", Long.parseLong(locationId)); bundle.putLong("locationId", locationId.toLong())
navController.navigate(R.id.action_global_locationFragment, bundle); navController.navigate(R.id.action_global_locationFragment, bundle)
} catch (Exception e) { } catch (e: Exception) {
Log.e(TAG, "init: ", e); Log.e(TAG, "init: ", e)
return;
} }
break;
} }
case HASHTAG: { FavoriteType.HASHTAG -> {
final String hashtag = model.getQuery(); val hashtag = model.query
// Log.d(TAG, "hashtag: " + hashtag); // Log.d(TAG, "hashtag: " + hashtag);
final NavController navController = NavHostFragment.findNavController(this); val navController = NavHostFragment.findNavController(this)
final Bundle bundle = new Bundle(); val bundle = Bundle()
bundle.putString("hashtag", "#" + hashtag); bundle.putString("hashtag", "#$hashtag")
navController.navigate(R.id.action_global_hashTagFragment, bundle); navController.navigate(R.id.action_global_hashTagFragment, bundle)
break; }
else -> {
} }
default:
// do nothing
} }
}, model -> { }, { model: Favorite ->
// delete // delete
final Context context = getContext(); val context = context ?: return@FavoritesAdapter false
if (context == null) return false; MaterialAlertDialogBuilder(context)
new MaterialAlertDialogBuilder(context) .setMessage(getString(R.string.quick_access_confirm_delete, model.query))
.setMessage(getString(R.string.quick_access_confirm_delete, model.getQuery())) .setPositiveButton(R.string.yes) { d: DialogInterface, _: Int ->
.setPositiveButton(R.string.yes, (d, which) -> favoriteRepository favoriteRepository.deleteFavorite(model.query, model.type, object : RepositoryCallback<Void> {
.deleteFavorite(model.getQuery(), model.getType(), new RepositoryCallback<Void>() { override fun onSuccess(result: Void) {
@Override d.dismiss()
public void onSuccess(final Void result) { favoriteRepository.getAllFavorites(object : RepositoryCallback<List<Favorite>> {
d.dismiss(); override fun onSuccess(result: List<Favorite>) {
favoriteRepository.getAllFavorites(new RepositoryCallback<List<Favorite>>() { favoritesViewModel.list.postValue(result)
@Override
public void onSuccess(final List<Favorite> result) {
favoritesViewModel.getList().postValue(result);
}
@Override
public void onDataNotAvailable() {}
});
} }
@Override override fun onDataNotAvailable() {}
public void onDataNotAvailable() {} })
})) }
.setNegativeButton(R.string.no, null)
.show();
return true;
});
binding.favoriteList.setAdapter(adapter);
override fun onDataNotAvailable() {}
})
}
.setNegativeButton(R.string.no, null)
.show()
true
})
binding.favoriteList.adapter = adapter
} }
} }