Skip to content

Fav_API ์—ฐ๊ฒฐ(2)_fragments, userdata ๐Ÿžย #14

Description

@honghyoeun
  1. fravments_BitmapCallback.java

๋น„ํŠธ๋งต์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์ฝœ๋ฐฑ ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ, ๋น„๋™๊ธฐ์ ์ธ ๋น„ํŠธ๋งต ๋กœ๋”ฉ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ณ , ๋กœ๋“œ๋œ ๋น„ํŠธ๋งต์„ ์ ์ ˆํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•˜๊ฑฐ๋‚˜ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ

package com.example.stylerent.fragments;

import android.graphics.Bitmap;

public interface BitmapCallback {
    void onBitmapLoaded(Bitmap bitmap);
    void onFailure(Throwable t);
}
  • void onBitmapLoaded(Bitmap bitmap) : ๋น„ํŠธ๋งต์ด ์„ฑ๊ณต์ ์œผ๋กœ ๋กœ๋“œ๋˜์—ˆ์„ ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ์ฝœ๋ฐฑ ๋ฉ”์„œ๋“œ
    ๋น„ํŠธ๋งต ๊ฐ์ฒด๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๋Š”๋‹ค.
    ๋กœ๋“œ๋œ ๋น„ํŠธ๋งต์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ํ•„์š”ํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋กœ์ง์„ ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ๊ตฌํ˜„ ๊ฐ€๋Šฅ
  • void onFailure(Throwable t) : ๋น„ํŠธ๋งต ๋กœ๋“œ ์ค‘ ๋ฐœ์ƒํ•œ ์˜ค๋ฅ˜๋‚˜ ์˜ˆ์™ธ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ํ˜ธ์ถœ๋˜๋Š” ์ฝœ๋ฐฑ ๋ฉ”์„œ๋“œ
    ๋ฐœ์ƒํ•œ ์˜ค๋ฅ˜๋‚˜ ์˜ˆ์™ธ๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๋Š”๋‹ค.
    ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๋กœ์ง์„ ์ด ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ๊ตฌํ˜„ํ•˜์—ฌ ์˜ค๋ฅ˜ ์ƒํ™ฉ์— ๋Œ€์‘

  1. FavoriteFragment.java

์‚ฌ์šฉ์ž์˜ ์ฆ๊ฒจ์ฐพ๊ธฐ ๋ชฉ๋ก์„ ๊ฐ€์ ธ์™€ ํ™”๋ฉด์— ํ‘œ์‹œ
๊ฐ ์ฆ๊ฒจ์ฐพ๊ธฐ ํ•ญ๋ชฉ์— ๋Œ€ํ•œ ์ด๋ฏธ์ง€๋ฅผ ๋น„๋™๊ธฐ์ ์œผ๋กœ ๊ฐ€์ ธ์™€ ํ‘œ์‹œ

package com.example.stylerent.fragments;

import static android.content.ContentValues.TAG;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.stylerent.R;
import com.example.stylerent.auth.ApiInterface;
import com.example.stylerent.auth.FavModel;
import com.example.stylerent.auth.ImagePathRequest;
import com.example.stylerent.auth.RetrofitClient;
import com.example.stylerent.userdata.CoordinateResponse;
import com.example.stylerent.userdata.CustomAdapter;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FavoriteFragment extends Fragment {
    private ListView favListView; // ์ฆ๊ฒจ์ฐพ๊ธฐ ๋ชฉ๋ก์„ ํ‘œ์‹œํ•  ๋ฆฌ์ŠคํŠธ
    private List<FavModel> fList = new ArrayList<>(); // ์ฆ๊ฒจ์ฐพ๊ธฐ ํ•ญ๋ชฉ ์ €์žฅ ๋ฆฌ์ŠคํŠธ
    private List<FavModel> changedVer = new ArrayList<>(); // ์ด๋ฏธ์ง€๊ฐ€ ๋กœ๋“œ๋œ ํ›„ ์ˆ˜์ •๋œ ์ฆ๊ฒจ์ฐพ๊ธฐ ๋ฆฌ์Šค
    private CustomAdapter customAdapter; // ๋ฆฌ์ŠคํŠธ๋ทฐ์— ์‚ฌ์šฉ๋œ ์ปค์Šคํ…€ ์–ด๋Œ‘ํ„ฐ ์ฒด
    public static final String MyPREFERENCES = "MyPrefs";
    SharedPreferences sharedPreferences; // ์•ฑ์— ์„ค์ •๋œ ์ •๋ณด ์ €์žฅ

    public FavoriteFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @SuppressLint("MissingInflatedId")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_favorite, container, false);
        sharedPreferences = this.getActivity().getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

        favListView = rootView.findViewById(R.id.favListV);
        // request
        getMyFav(rootView);

        // Inflate the layout for this fragment
        return rootView;

    }

    private void getImage(View view) {
        List<Bitmap> bitmapList = new ArrayList<>();

        for (FavModel f : fList) {
            getImageByPath(f.getProductImage(), new BitmapCallback() {
                @Override
                public void onBitmapLoaded(Bitmap bitmap) {
                    System.out.println("Bitmap --------->>" + bitmap.toString());
                    bitmapList.add(bitmap);

                    if (bitmapList.size() == fList.size()) {
                        // All bitmaps are loaded, add them to the FavModel objects
                        for (int i = 0; i < fList.size(); i++) {
                            fList.get(i).setImageBitmap(bitmapList.get(i));
                        }

                        // Create the custom adapter
                        customAdapter = new CustomAdapter(getContext(), fList);
                        favListView.setAdapter(customAdapter);
                    }
                }

                @Override
                public void onFailure(Throwable t) {
                    // Handle the failure here
                }
            });
        }
        System.out.println("finish get image request");

        System.out.println("finish get image request" + bitmapList.size());
//        for(int i =0; i<fList.size(); i++){
//            fList.get(i).setImageBitmap(bitmapList.get(i));
//            System.out.println(fList.get(i).getImageBitmap() == null);
//        }
    }

    private void getImageByPath(String imagePath, BitmapCallback callback){

        String TOKEN = sharedPreferences.getString("TOKEN", null);
        ApiInterface apiInterface = RetrofitClient.getRetrofitInstance(getContext()).create(ApiInterface.class);
        // Create a new ImagePathRequest with current image path
        ImagePathRequest imagePathRequest = new ImagePathRequest(imagePath);
        Call<ResponseBody> userProfileCall = apiInterface.getImageByPath("Bearer" + TOKEN, imagePathRequest);

        userProfileCall.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.isSuccessful() && response.body() != null) {
                    InputStream inputStream = response.body().byteStream();
                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    callback.onBitmapLoaded(bitmap); // Pass the bitmap to the callback
                } else {
                    callback.onFailure(new Exception("Failed to load image")); // Pass failure to the callback
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                callback.onFailure(t);
            }
        });
    }

    private void getMyFav(View view){
        //make Get request to get user's current location
        String TOKEN = sharedPreferences.getString("TOKEN", null);
        System.out.println("get current location ------>>>>>>>>>>>>>>>>>>>>>>");

        ApiInterface apiInterface = RetrofitClient.getRetrofitInstance(getContext()).create(ApiInterface.class);
        Call<List<FavModel>> userProfileCall = apiInterface.getMyFav("Bearer" + TOKEN);
        userProfileCall.enqueue(new Callback<List<FavModel>>() {
            @Override
            public void onResponse(Call<List<FavModel>> call, Response<List<FavModel>> response) {
                if (response.isSuccessful()) {
                    fList = response.body();
                    for(FavModel f : fList){
                        System.out.println("Items --------->" + f.getProductName());
                    }

                    //get image and change it bitmap
                    getImage(view);
                }
            }

            @Override
            public void onFailure(Call<List<FavModel>> call, Throwable t) {
                // Handle network failure
                Log.e(TAG, "Failed to get user current location: " + t.getMessage());

            }
        });
    }
}
  • onCreateView() ๋ฉ”์„œ๋“œ :

    ํ”„๋ž˜๊ทธ๋จผํŠธ์˜ ๋ทฐ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ดˆ๊ธฐํ™”

    R.layout.fragment_favorite๋ฅผ ์ธํ”Œ๋ ˆ์ดํŠธํ•˜์—ฌ ์ฆ๊ฒจ์ฐพ๊ธฐ ํ™”๋ฉด์˜ ๋ทฐ๋ฅผ ์ƒ์„ฑ

    favListView๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๊ณ , getMyFav() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ์‚ฌ์šฉ์ž์˜ ์ฆ๊ฒจ์ฐพ๊ธฐ ๋ชฉ๋ก ๊ฐ€์ ธ์˜ด

    ์ตœ์ข…์ ์œผ๋กœ ๋ทฐ๋ฅผ ๋ฐ˜ํ™˜

  • getMyFav() ๋ฉ”์„œ๋“œ :

    ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ์‚ฌ์šฉ์ž์˜ ์ฆ๊ฒจ์ฐพ๊ธฐ ๋ชฉ๋ก์„ ๊ฐ€์ ธ์˜ค๋Š” ๋ฉ”์„œ๋“œ

    SharedPreferences์—์„œ ์ธ์ฆ ํ† ํฐ ๊ฐ€์ ธ์˜ด

    Retrofit์„ ์‚ฌ์šฉํ•˜์—ฌ API ํ˜ธ์ถœ์„ ์œ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ

    API ํ˜ธ์ถœ์„ ์œ„ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , ์‘๋‹ต์„ ์ฒ˜๋ฆฌ

    ์„ฑ๊ณต์ ์ธ ์‘๋‹ต์ด๋ฉด fList์— ์ฆ๊ฒจ์ฐพ๊ธฐ ๋ชฉ๋ก์„ ์ €์žฅํ•˜๊ณ , getImage() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๊ฐ ํ•ญ๋ชฉ์˜ ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€์ ธ์˜ด

  • getImage() ๋ฉ”์„œ๋“œ :

    fList์— ์žˆ๋Š” ๊ฐ ์ฆ๊ฒจ์ฐพ๊ธฐ ํ•ญ๋ชฉ์˜ ์ด๋ฏธ์ง€๋ฅผ ๋น„๋™๊ธฐ์ ์œผ๋กœ ๊ฐ€์ ธ์˜ค๋Š” ๋ฉ”์„œ๋“œ

    getImageByPath() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€์ ธ์˜ด

    ๋น„๋™๊ธฐ์ ์œผ๋กœ ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ๋•Œ๋ฌธ์— BitmapCallback ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋น„ํŠธ๋งต ๋กœ๋“œ ์™„๋ฃŒ ๋˜๋Š” ์‹คํŒจ ์‹œ ์ฒ˜๋ฆฌ ๋กœ์ง์„ ๊ตฌํ˜„

    ๋ชจ๋“  ์ด๋ฏธ์ง€๊ฐ€ ๋กœ๋“œ๋˜๋ฉด customAdapter๋ฅผ ์ƒ์„ฑํ•˜์—ฌ favListView์— ์ ์šฉ

  • getImageByPath() ๋ฉ”์„œ๋“œ :

    ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฉ”์„œ๋“œ

    SharedPreferences์—์„œ ์ธ์ฆ ํ† ํฐ์„ ๊ฐ€์ ธ์˜ด

    Retrofit์„ ์‚ฌ์šฉํ•˜์—ฌ API ํ˜ธ์ถœ์„ ์œ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ

    API ํ˜ธ์ถœ์„ ์œ„ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , ์‘๋‹ต์„ ์ฒ˜๋ฆฌ

    ์„ฑ๊ณต์ ์ธ ์‘๋‹ต์ด๋ฉด ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ณ , Bitmap์œผ๋กœ ๋””์ฝ”๋”ฉํ•˜์—ฌ ์ฝœ๋ฐฑ์„ ํ†ตํ•ด ์ „๋‹ฌ

    ์‹คํŒจํ•œ ๊ฒฝ์šฐ ์˜ค๋ฅ˜๋ฅผ ์ฝœ๋ฐฑ์œผ๋กœ ์ „๋‹ฌ


    1. userdata_CustomAdapter

    FavoriteFragment์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์ปค์Šคํ…€ ์–ด๋Œ‘ํ„ฐ ํด๋ž˜์Šค, ListView์— ์ฆ๊ฒจ์ฐพ๊ธฐ ํ•ญ๋ชฉ์„ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ

    package com.example.stylerent.userdata;
    
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import com.example.stylerent.R;
    import com.example.stylerent.auth.FavModel;
    
    import java.util.List;
    
    public class CustomAdapter extends BaseAdapter {
        private List<FavModel> favList;
        private LayoutInflater inflater;
    
        public CustomAdapter(Context context, List<FavModel> favList) {
            this.favList = favList;
            inflater = LayoutInflater.from(context);
        }
    
        @Override
        public int getCount() {
            return favList.size();
        }
    
        @Override
        public FavModel getItem(int position) {
            return favList.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
    
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.fav_item, parent, false);
                holder = new ViewHolder();
    
                holder.productImage = convertView.findViewById(R.id.productImage);
                holder.productPrice = convertView.findViewById(R.id.productPrice);
                holder.productTitle = convertView.findViewById(R.id.productTitle);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
    
            FavModel fav = favList.get(position);
    
            holder.productImage.setImageBitmap(fav.getImageBitmap());
            holder.productTitle.setText(fav.getProductName());
            holder.productPrice.setText(fav.getProductPrice());
    //        holder.messageTextView.setText(user.getLastMessage());
    
            return convertView;
        }
    
        private static class ViewHolder {
            TextView productPrice;
            ImageView productImage;
            TextView productTitle;
        }
    
    }
  • CustomAdapter ํด๋ž˜์Šค ์„ ์–ธ :

List<FavModel> favList : ์ฆ๊ฒจ์ฐพ๊ธฐ ํ•ญ๋ชฉ์„ ์ €์žฅํ•˜๋Š” ๋ฆฌ์ŠคํŠธ

LayoutInflater inflater : ๋ ˆ์ด์•„์›ƒ ์ธํ”Œ๋ ˆ์ด์…˜์„ ์œ„ํ•œ LayoutInflater ๊ฐ์ฒด

  • CustomAdapter ์ƒ์„ฑ์ž :

context์™€ favlist๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์•„ ์–ด๋Œ‘ํ„ฐ๋ฅผ ์ดˆ๊ธฐํ™”

  • getCount() ๋ฉ”์„œ๋“œ : ์ฆ๊ฒจ์ฐพ๊ธฐ ํ•ญ๋ชฉ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜
  • getItem() ๋ฉ”์„œ๋“œ : ์ง€์ •๋œ ์œ„์น˜์˜ ์ฆ๊ฒจ์ฐพ๊ธฐ ํ•ญ๋ชฉ์„ ๋ฐ˜ํ™˜
  • getItemId ๋ฉ”์„œ๋“œ : ์ง€์ •๋œ ์œ„์น˜์˜ ํ•ญ๋ชฉ ID๋ฅผ ๋ฐ˜ํ™˜
  • getView ๋ฉ”์„œ๋“œ :

์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ทฐ(convertView)๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ ์ตœ์ ํ™”

๋ทฐ ํ™€๋” ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ทฐ์˜ ์ฐธ์กฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ์žฌ์‚ฌ์šฉ

ํ•ด๋‹น ์œ„์น˜์˜ ์ฆ๊ฒจ์ฐพ๊ธฐ ํ•ญ๋ชฉ์„ ๊ฐ€์ ธ์™€ ๋ทฐ์— ๋ฐ์ดํ„ฐ ์„ค์ •

  • ViewHolder ํด๋ž˜์Šค :

๋ทฐ์˜ ํ•˜์œ„ ํ•ญ๋ชฉ์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋‚ด๋ถ€ ํด๋ž˜์Šค


  1. userdata_lmageResponse

์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํด๋ž˜์Šค ,์ด๋ฏธ์ง€ ๋ฐ”์ดํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ

package com.example.stylerent.userdata;

public class ImageResponse {
    private String imageByte;

    public ImageResponse(String imageByte) {
        this.imageByte = imageByte;
    }

    public String getImageByte() {
        return imageByte;
    }

// constructor, getters and setters
}
  • ImageResponse ํด๋ž˜์Šค : String imageByte ์ด๋ฏธ์ง€ ๋ฐ”์ดํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ณ€์ˆ˜

  • ImageResponse ์ƒ์„ฑ์ž : ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ด๋ฏธ์ง€ ๋ฐ”์ดํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„ ๊ฐ์ฒด ์ดˆ๊ธฐํ™”

                                             ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์€ ๊ฐ’์„ `imageByte` ๋ณ€์ˆ˜์— ์„ค์ •
    
  • getImageByte ๋ฉ”์„œ๋“œ : imageByte ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions