จะแทรกมุมมองเข้าไปในผู้นำเสนอโดยใช้ Dagger2 สำหรับ Android ได้อย่างไร

ฉันต้องการ inject the view (Activity object) เข้าสู่ Presenter ตอนนี้ฉันกำลังตั้งค่ามุมมองด้วยตนเองโดยใช้ตัวตั้งค่าของคลาสผู้นำเสนอ ฉันจะบรรลุเป้าหมายนี้โดยใช้ Dagger ได้อย่างไร คุณช่วยแสดงโค้ดตัวอย่างเกี่ยวกับวิธีการทำได้ไหม

นี่คือวิธีที่กิจกรรมหลักสร้างผู้นำเสนอ ณ ขณะนี้

public class MainActivity extends AppCompatActivity implements CountPresenter.View, ToastPresenter.View {

    @Inject
    CountPresenter countPresenter;
    @Inject
    ToastPresenter toastPresenter;
    TextView countText;
    Toast toast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        countText = findViewById(R.id.text_count);

        DaggerToastPresenterComponent.create().inject(this);
        DaggerCountPresenterComponent.create().inject(this);
        countPresenter.setView(this);
        toastPresenter.setView(this) ;

        findViewById(R.id.button_count).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                countPresenter.incrementCount();
            }
        });

        findViewById(R.id.button_toast).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toastPresenter.handleToastButtonClick();
            }
        });
    }
}

**รหัสชั้นเรียนผู้นำเสนอ : **

public class CountPresenter {
    private Counter counter ;


    public void setView(View view) {
        this.view = view;
    }

    private View view ;

    @Inject
    public CountPresenter() {
        counter =  Counter.getInstance() ;
    }

    public void incrementCount(){
        counter.setCount(counter.getCount()+1);
        view.setCounterText(counter.toString());
    }

    public interface View {
        void setCounterText(String val) ;
    }
}

รหัสเต็มที่นี่ :

https://github.com/nateshmbhat/FresherAssignment2020/tree/nateshmbhat/Apps/CounterApp_MVP_Dagger/app/src/main/java/com/techy/nateshmbhat/contacto


person Natesh bhat    schedule 15.01.2020    source แหล่งที่มา
comment
โพสต์โมดูลผู้นำเสนอของคุณ !   -  person Santanu Sur    schedule 15.01.2020
comment
ฉันได้ให้ลิงค์ไปยังโครงการของฉัน ..   -  person Natesh bhat    schedule 16.01.2020


คำตอบ (2)


ฉันจะเขียนขั้นตอนต่างๆ

ขั้นแรก คุณต้องเพิ่มคลาส โมดูล

@Module
class CountPresenterModule {
    @Provides
    @Singleton
    fun providCountPresenter(): CountPresenter.View {
        return CountPresenter()
    }
}

ในทำนองเดียวกันเพิ่มคลาสโมดูลสำหรับ ToastPresenter

หลังจากนั้นให้เพิ่มการอ้างอิงของคลาสโมดูลที่เพิ่มใหม่ในคลาส AppComponent

@Singleton
@Component(modules = [AppModule::class, .....,ToastPresenterModule::class, CountPresenterModule::class]){
    fun inject(mainActivity: MainActivity)
}

และเพลิดเพลินไปกับความมหัศจรรย์ของกริช/การฉีดยา

ส่วนที่ 2 ในจาวา

@Singleton
@Component(modules = {AppModule.class, ToastPresenterModule.class, CountPresenterModule.class}) {
   void inject(MainActivity mainActivity);
}
person Attiq ur Rehman    schedule 15.01.2020
comment
คุณช่วยระบุรหัส java เพื่อเพิ่มการอ้างอิงส่วนโมดูลที่เพิ่มใหม่ของรหัส ... - person Natesh bhat; 15.01.2020

สวัสดี นี่คือวิธีแก้ปัญหาของฉันใน Kotlin ขั้นแรกคุณจะสร้างอินเทอร์เฟซ

สัญญาอินเทอร์เฟซ{

    interface Presenter{
    fun setView(view: View)
}

}

จากนั้นภายในผู้นำเสนอคุณจะนำไปใช้ (นอกเหนือจากวิธีการอื่น ๆ ที่ผู้นำเสนอของคุณควรมี (หลังจากที่คุณกำหนดไว้ล่วงหน้าในอินเทอร์เฟซนั้น))

: Contract.Presenter 

ภายในผู้นำเสนอ คุณจะสร้าง late init var view ของประเภท View

 private lateinit var view: View

จากนั้นภายในผู้นำเสนอในการใช้งานอินเทอร์เฟซผู้นำเสนอ คุณจะกำหนดสิ่งนี้

override fun setView(view: View) {
    this.view = view
}

ใน MainActivity คุณจะมี

@Inject lateinit var ผู้นำเสนอ: Contract.Presenter

และในเรซูเม่ คุณจะตั้งค่านี้เป็นมุมมอง

override fun onResume() {
    super.onResume()
    presenter.setView(this)
}

นั้นคือทั้งหมด,

มีความสุขในการเขียนโค้ด

person Nenad Štrbić    schedule 21.05.2021